【问题标题】:how to pass ref variable to a SQL stored Procedure using ADO.NET?如何使用 ADO.NET 将 ref 变量传递给 SQL 存储过程?
【发布时间】:2013-08-28 06:48:38
【问题描述】:

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

[ASP.NET 代码]

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

[SQL]

create PROCEDURE [dbo].[AddNewDoctor]
    @doctorname nvarchar(100),
    @email nvarchar(100),
    @password nvarchar(MAX),
    @docId int out,
    @Result int out

AS
BEGIN
    SET NOCOUNT ON;
        declare @idCounter int
    select @idCounter = count(*) from dbo.doctors
    if EXISTS (select * from dbo.doctors where e_mail = @email) 
    begin
        SET @Result = -1
        set @docId= 0 
    end
    else
    begin
    INSERT INTO [dbo].[doctors]
           ([doctor_id]
           ,[doctorname]
           ,[e_mail]
           ,[password]           
     VALUES
           ((@idCounter +1)
           ,@docotorname
           ,@email
           ,@password
           )
            SET @Result = 1
            set @docId= (@idCounter + 1) 
    end 
END

这段代码工作得很好,我现在想使用 ADO 而不是 LINQ,我的问题是我不能像在 LINQ 中那样传递 ref 变量,所以我如何使用 ADO 来完成它

【问题讨论】:

  • 永远不要在你的代码中使用像 set @docId= (@idCounter + 1) 这样的东西,因为它会让你的程序不能多用户使用。
  • 感谢您 BendEg 的建议,但您能否说明更多,因为我不明白为什么
  • 我会将您的 ID-Column 设置为 Identity-Column,然后使用 @@IDENTITY 获取新的 Identity,因为可以同时执行存储过程,而不是系统会尝试为两位医生设置相同的 ID。 technet.microsoft.com/de-de/library/ms174639.aspx
  • @BendEg 你能举个小例子吗?

标签: c# asp.net sql-server ado.net


【解决方案1】:

你必须做这样的事情。使用ParameterDirection

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

在您的情况下,您必须使用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;

【讨论】:

  • @BendEg 我理解的是,它适用于 Linq,但他现在需要 ADO 的帮助
  • @BendEg 他说他想在 ADO 中做。
  • @SriramSakthivel 我已经尝试过该代码,但它给出了异常字符串 [3]:Size 属性的大小为 0 无效。
  • ' 整数?结果=空;诠释? DocId = null; SqlParameter output = new SqlParameter("@docId", DocId) {Direction = ParameterDirection.Output}; cmd.Parameters.Add(输出); SqlParameter output2 = new SqlParameter("@Result", result) {Direction = ParameterDirection.Output}; cmd.Parameters.Add(output2);' 'cmd.ExecuteNonQuery();'
  • @AhmedAbdel-hameed 更新了我的答案
【解决方案2】:

试试这个

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

希望对您有所帮助,谢谢。

【讨论】:

  • AddWithValue 有多个参数!!和ref
  • 你需要设置ParameterDirection
【解决方案3】:

【讨论】:

  • 我想使用 ADO 而不是 LINQ!
  • 好吧,我错了,对不起!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
相关资源
最近更新 更多