-1

我有一个注册表单,我想检查用户之前没有注册过。

我的代码在下面。我认为存在两个问题:(1)调用方法和(2)将参数传递给存储过程。我的症状是这会导致输入参数未初始化的异常。

create procedure fakeuser @username nvarchar(250),@codemeli nchar(10),@email     nvarchar(50), @user nvarchar(250) output,@code nchar(10)output,@mail nvarchar(50)output
 as
if exists(select username,email,codemeli from karbar where username=@username)
set @user=@username
else if exists(select username,email,codemeli from karbar where codemeli=@codemeli)
set @code=@codemeli
else if exists(select username,email,codemeli from karbar where email=@email)
set @mail= @email

这是c#代码:

  public static string confirm(string username, string email, string codemeli)
    {
        string constring = "data source=.;database=site;integrated security=true;";
        SqlConnection connection = new SqlConnection(constring);

        // Command - specify as StoredProcedure
        SqlCommand command = new SqlCommand("fakeuser", connection);
        command.CommandType = CommandType.StoredProcedure;

        SqlParameter param = new SqlParameter("@username", SqlDbType.NVarChar);
        param.Direction = ParameterDirection.Input;
        param.Value = username;
        command.Parameters.Add(param);

        SqlParameter param2 = new SqlParameter("@email", SqlDbType.NVarChar);
        param2.Direction = ParameterDirection.Input;
        param2.Value = username;
        command.Parameters.Add(param2);

        SqlParameter param3 = new SqlParameter("@codemeli", SqlDbType.NChar);
        param3.Direction = ParameterDirection.Input;
        param3.Value = username;
        command.Parameters.Add(param3);

        // Return value as parameter
        SqlParameter returnuser = new SqlParameter("@user", SqlDbType.NVarChar);
        returnuser.Direction = ParameterDirection.ReturnValue;
        command.Parameters.Add(returnuser);

        SqlParameter returncode = new SqlParameter("@code", SqlDbType.NChar);

        returncode.Direction = ParameterDirection.ReturnValue;

        command.Parameters.Add(returncode);
        SqlParameter returnmail = new SqlParameter("@mail", SqlDbType.NVarChar);
        returnmail.Direction = ParameterDirection.ReturnValue;
        command.Parameters.Add(returnmail);

        // Execute the stored procedure
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();

谢谢你。

4

1 回答 1

1

为@user、@code 和@mail设置ParameterDirection=Output并指定参数的宽度/大小/长度。

 SqlParameter returnuser = new SqlParameter("@user", SqlDbType.NVarChar,100);
 returnuser.Direction = ParameterDirection.Output;
 command.Parameters.Add(returnuser);
于 2012-07-09T14:47:29.313 回答