0

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

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

4

2 回答 2

0

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

改变这个

public System.Data.DataSet spe_ISUpgradePossible(dbObject  currentDBObj)

进入这个

public int spe_ISUpgradePossible(dbObject  currentDBObj)
于 2013-10-20T09:03:17.057 回答
0

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

问题是线路

 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);
于 2013-10-20T09:03:48.057 回答