C# 如何使用ADO.NET将ref变量传递给SQL存储过程?

C# 如何使用ADO.NET将ref变量传递给SQL存储过程?,c#,asp.net,sql-server,ado.net,C#,Asp.net,Sql Server,Ado.net,我使用LINQ调用存储过程将一些数据保存到数据库中,然后从存储过程返回两个变量 [ASP.NET代码] dbDataContext dbo = new dbDataContext(); dbo.AddNewDoctor(doctorName, email, password, ref DocId, ref result); [SQL] 这段代码工作得很好,我现在想使用ADO而不是LINQ,问题是我不能像LINQ那样传递ref变量,所以我如何使用ADO来做呢?你必须这样做。使用 在本例中,必须使

我使用LINQ调用存储过程将一些数据保存到数据库中,然后从存储过程返回两个变量

[ASP.NET代码]

dbDataContext dbo = new dbDataContext();
dbo.AddNewDoctor(doctorName, email, password, ref DocId, ref result);
[SQL]


这段代码工作得很好,我现在想使用ADO而不是LINQ,问题是我不能像LINQ那样传递ref变量,所以我如何使用ADO来做呢?你必须这样做。使用

在本例中,必须使用
SqlDbType.Int
。使用Value属性读取返回值

SqlParameter output = new SqlParameter(paramName, SqlDbType.Int);
output.Direction = ParameterDirection.Output;
command.Parameters.Add(output);

int Result = (int) output.Value; or int? Result = (int?) output.Value;
试试这个

    using (SqlConnection con = new SqlConnection("Your connection string"))
    {
        con.Open();
        SqlCommand mycommand = new SqlCommand();
        mycommand.Connection = con;
        mycommand.CommandText = "dbo.AddNewDoctor";
        mycommand.CommandType = CommandType.StoredProcedure;

        mycommand.Parameters.AddWithValue(doctorName);
        mycommand.Parameters.AddWithValue(email);
        mycommand.Parameters.AddWithValue(password);
        mycommand.Parameters.AddWithValue(ref DocId);
        mycommand.Parameters.AddWithValue(ref result);


        mycommand.ExecuteNonQuery();
    }

希望这有帮助,谢谢。

参考本文,有一个工作示例:


不要在代码中使用set@docId=(@idCounter+1)之类的东西,因为这会使您的程序无法多用户使用。谢谢您BendEg的建议,但您能否详细说明一下,因为我不明白为什么我会将您的ID列设置为标识列,然后使用@标识获取新标识,因为存储过程可以同时执行,然后系统会尝试为两名医生设置相同的ID@BendEg可以用一个小例子来说明吗?
AddWithValue
,一个参数有多个!!使用
ref
您需要设置
ParameterDirection
@BendEg我不知道的是,它与Linq一起工作,但他现在需要帮助ADO@BendEg他说他想在ADO中执行该操作。@SriramSakthivel我已经尝试过该代码,它给出了异常字符串[3]:Size属性的大小无效,为0。'int?结果=空;智力?DocId=null;SqlParameter输出=新的SqlParameter(“@docId”,docId){Direction=ParameterDirection.output};cmd.Parameters.Add(输出);SqlParameter output2=新的SqlParameter(“@Result”,Result){Direction=ParameterDirection.Output};cmd.Parameters.Add(output2);“”cmd.ExecuteNonQuery();'@AhmedAbdel hameed更新了我的答案我想使用ADO而不是LINQ!好吧,我错了,对不起!
SqlParameter output = new SqlParameter(paramName, SqlDbType.Int);
output.Direction = ParameterDirection.Output;
command.Parameters.Add(output);

int Result = (int) output.Value; or int? Result = (int?) output.Value;
    using (SqlConnection con = new SqlConnection("Your connection string"))
    {
        con.Open();
        SqlCommand mycommand = new SqlCommand();
        mycommand.Connection = con;
        mycommand.CommandText = "dbo.AddNewDoctor";
        mycommand.CommandType = CommandType.StoredProcedure;

        mycommand.Parameters.AddWithValue(doctorName);
        mycommand.Parameters.AddWithValue(email);
        mycommand.Parameters.AddWithValue(password);
        mycommand.Parameters.AddWithValue(ref DocId);
        mycommand.Parameters.AddWithValue(ref result);


        mycommand.ExecuteNonQuery();
    }