簡體   English   中英

將參數傳遞給C#中的存儲過程

[英]Passing parameter to stored procedure in C#

我有一個存儲過程,它返回一個變量@result設置為1或0(數據類型位)。 我使用以下代碼在我的C#中訪問它。 它拋出錯誤說太多參數。

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



    }

存儲過程:(我覺得很好......)請更正我的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

你可以試試這段代碼:

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

為什么設置:

Cmd.CommandText = "Registration";

這將替換您的存儲過程名稱,因此它不會調用您指示的存儲過程:

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

使用SQL事件探查器來調試“通過網絡連接”的SQL是否符合預期會很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM