【问题标题】:Cannot implicitly convert type 'int' to 'System.Data.Dataset' - returned result from SQL required无法将类型“int”隐式转换为“System.Data.Dataset” - 需要从 SQL 返回的结果
【发布时间】:2013-10-20 17:45:16
【问题描述】:

我正在通过一个带有各种参数的对象向 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'”

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

【问题讨论】:

    标签: c# sql


    【解决方案1】:

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

    问题是线

     return result; // an integer
    

    当方法声明时

     public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)
    

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

     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);
    

    【讨论】:

    • 非常感谢您的解释。是的,我需要输入和输出参数。我已经进行了必要的更改,但现在触发了一个新错误 - “方法 'spe_ISUpgradePossible' 没有重载需要 1 个参数”。然后我将“out int result”添加到 WebMethod,但这导致了更多错误。我在正确的轨道上吗?
    • 您如何调用该方法有问题。我将尝试在答案中添加一个示例
    • 非常感谢@Steve,这对我帮助很大!
    【解决方案2】:

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

    改变这个

    public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)
    

    进入这个

    public int spe_ISUpgradePossible(dbObject  currentDBObj)
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多