无法将类型“int”隐式转换为“System.Data.Dataset” - 需要从SQL返回的结果

本文关键字:Dataset SQL 结果 返回 Data 类型 int 转换 System | 更新日期: 2023-09-27 18:36:10

我正在通过一个带有各种参数的对象发送到 SQL,并且需要接收 0 或 1 的值,以指示成功或失败。

我已经确认我的所有参数都已成功发送到 SQL,但我遇到了需要返回到 C# 的"@Result"值的问题。

以下是我在 Data.cs 文件中的代码摘录:

    public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)
    {
        using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString))
        {
            using (System.Data.SqlClient.SqlCommand command = GetCommand("spe_ISUpgradePossible", connection))
            {
                command.CommandType = System.Data.CommandType.StoredProcedure;
                System.Data.SqlClient.SqlParameter parameter;
                parameter = new System.Data.SqlClient.SqlParameter("@ServicePack", System.Data.SqlDbType.NVarChar);
                parameter.Direction = System.Data.ParameterDirection.Input;
                parameter.Value = currentDBObj.servicePack;
                command.Parameters.Add(parameter);
                parameter = new System.Data.SqlClient.SqlParameter("@ServicePackFolder", System.Data.SqlDbType.NVarChar);
                parameter.Direction = System.Data.ParameterDirection.Input;
                parameter.Value = currentDBObj.servicePackFolder;
                command.Parameters.Add(parameter);
                parameter = new System.Data.SqlClient.SqlParameter("@Result", System.Data.SqlDbType.Int);
                parameter.Direction = System.Data.ParameterDirection.Output;
                command.Parameters.Add(parameter);
                System.Data.DataSet dataSet = new System.Data.DataSet();
                System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);
                adapter.Fill(dataSet);
                return dataSet;
                //int result = Convert.ToInt16(command.Parameters["@Result"].Value.ToString());
                //return result;
            }
        }
    }    

我添加了以下两行以迎合"@Result":

                        //int result = Convert.ToInt16(command.Parameters["@Result"].Value.ToString());
                //return result;

这导致显示以下错误:"无法将类型'int'隐式转换为'System.Data.Dataset'"

有人可以协助我解决上述问题吗?任何协助将不胜感激。

无法将类型“int”隐式转换为“System.Data.Dataset” - 需要从SQL返回的结果

您的方法要求返回数据集而不是整数

问题是线

 return result; // an integer

而该方法声明

 public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)

因此,您需要决定返回到调用代码的内容。
我觉得您需要这两个信息,如果是这种情况,那么您可以使用 out 参数更改您的方法签名

 public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj, out int result)
 {
     ......
     // Assign the output parameter to the output variable passed in the method call
     result = Convert.ToInt32(command.Parameters["@Result"].Value.ToString());
     return dataSet;
 }

另请注意,您将输出参数声明为整数类型,但随后将其转换为短整型 ( Convert.ToInt16 )。这是没有意义的(为什么要失去精度?),因此最好坚持使用输出参数的原始数据类型(Convert.ToInt32

完成这些更改后,您需要替换调用代码以反映新的方法签名,并绝对确保您的spe_ISUpgradePossible不会在不初始化输出参数的情况下返回到调用代码

 // No intialization here is needed, the out keyword forces the compiler to check
 // for every possible code path in the called method that return without `result` initialized
 int result;  
 Dataset ds = spe_ISUpgradePossible(yourObject, out result);

你需要声明返回int而不是dataset的函数

更改此内容

public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)

进入这个

public int spe_ISUpgradePossible(dbObject  currentDBObj)