简体   繁体   English

将参数传递给C#中的存储过程

[英]Passing parameter to stored procedure in C#

I have a stored procedure that returns a variable @result set to 1 or 0 (datatype bit). 我有一个存储过程,它返回一个变量@result设置为1或0(数据类型位)。 I am accessing it in my C# with the following code. 我使用以下代码在我的C#中访问它。 Its throwing an error saying too many parameters. 它抛出错误说太多参数。

 protected void btnRegister_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
        con.Open();



        SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.CommandText = "Registration";
        Cmd.Parameters.AddWithValue("@Name", txtName.Text);
        Cmd.Parameters.AddWithValue("@Email", txtEmailAddress.Text);
        Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        Cmd.Parameters.AddWithValue("@CountryCode", ddlCountryCode.Text);
        Cmd.Parameters.AddWithValue("@Mobile", txtMobileNumber.Text);
        //Cmd.Parameters.Add("@Result", DbType.Boolean);
        SqlParameter sqlParam = new SqlParameter("@Result", DbType.Boolean);
        //sqlParam.ParameterName = "@Result";
        //sqlParam.DbType = DbType.Boolean;
        sqlParam.Direction = ParameterDirection.Output;
        Cmd.Parameters.Add(sqlParam);
        Cmd.ExecuteNonQuery();
        con.Close();
        Response.Write(Cmd.Parameters["@Result"].Value); 



    }

the stored procedure: (this I think is fine...) And please correct my CS code... 存储过程:(我觉得很好......)请更正我的CS代码...

ALTER PROCEDURE [dbo].[usp_CheckEmailMobile]
  ( @Name VARCHAR(50), 
@Email NVARCHAR(50), 
@Password NVARCHAR(50), 
@CountryCode INT, 
@Mobile VARCHAR(50), 
@Result BIT OUTPUT)
 AS 
BEGIN 

IF EXISTS (SELECT COUNT (*) FROM AUser WHERE  [Email] = @Email AND [Mobile] = @Mobile) 
 Begin 
 Set @Result=0; --Email &/or Mobile does not exist in database
End
ELSE
Begin
  --Insert the record & register the user 
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)  
Set @Result=1;
 End

END

you can try this code : 你可以试试这段代码:

bool result=false;
SqlCommand scCommand = new SqlCommand("usp_CheckEmailMobile", sqlCon);
scCommand.CommandType = CommandType.StoredProcedure;
scCommand.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text;
scCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = txtEmailAddress.Text;
scCommand.Parameters.Add("@Password ", SqlDbType.NVarChar, 50).Value = txtPassword.Text;
scCommand.Parameters.Add("@CountryCode", SqlDbType.VarChar.50).Value =ddlCountryCode.SelectedText;
scCommand.Parameters.Add("@Mobile", SqlDbType.NVarChar, 50).Value = txtMobileNumber.Text;
scCommand.Parameters.Add("@Result ", SqlDbType.Bit).Direction = ParameterDirection.Output;
try
{
    if (scCommand.Connection.State == ConnectionState.Closed)
    {
        scCommand.Connection.Open();
    }
    scCommand.ExecuteNonQuery();
    result = Convert.ToBoolean(scCommand.Parameters["@Result"].Value);


}
catch (Exception)
{

}
finally
{                
    scCommand.Connection.Close();
    Response.Write(result); 
}

Why do you set: 为什么设置:

Cmd.CommandText = "Registration";

this will replace your stored procedure name, so it won't call the stored procedure you indicated in: 这将替换您的存储过程名称,因此它不会调用您指示的存储过程:

SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);

It can be useful to use a SQL profiler to debug that the SQL going "over the wire" is as expected. 使用SQL事件探查器来调试“通过网络连接”的SQL是否符合预期会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM