2013-03-22 32 views
2

我有一个存储过程返回一个变量@result设置为1或0(数据类型位)。我使用下面的代码在C#中访问它。它抛出一个错误,说太多的参数。在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 
+2

它是一个实际的输出参数或存储过程返回值?请张贴存储过程的缩写定义。 – 2013-03-22 12:05:01

+1

也发布您的Sp签名。 – 2013-03-22 12:05:34

+0

那是哪个CommandText? 'usp_CheckEmailMobile'或'Registration',当前一个正在注视'Registration'这是不你的意思[从使用位存储过程返回布尔值]的 – V4Vendetta 2013-03-22 12:08:18

回答

14

你可以试试这个代码:

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

这段代码什么也不做......没有数据被插入.. – adityawho 2013-03-22 12:43:30

+0

我刚才叫你的SP与参数一起,检查数据库和storeprocedure – Arshad 2013-03-22 12:47:23

+0

手段? 顺便说一句,仍然一无所获。我希望你读的代码为我的SP ...它返回1或0 ... – adityawho 2013-03-22 12:50:32

2

你为什么设置:

Cmd.CommandText = "Registration"; 

这将取代您的存储过程名称,因此它不会调用您在以下指示的存储过程:

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

使用SQL事件探查器来调试SQL“正在通过线路”如预期的那样可能很有用。

+0

我删除了Cmd.CommandText =“注册”; 现在抛出“错误转换为nvarchar成int” – adityawho 2013-03-22 12:36:25