如何将布尔值发送到SQL Server中的插入存储过程?

时间:2014-03-05 19:24:14

标签: c# stored-procedures sql-server-2008-r2

我正在尝试将一些值发送到Insert存储过程,在我的代码中:

    private void btnEnter_Click(object sender, EventArgs e)
    {
        string[] parName = new string[6];
        parName[0] = "@username";
        parName[1] = "@password";
        parName[2] = "@fName";
        parName[3] = "@lName";
        parName[4] = "@gender";
        parName[5] = "@post";
        string[] parValue = new string[6];
        string gender = (rbWoman.Checked) ? "0" : "1";
        parValue[0] = txtUserName.Text;
        parValue[1] = txtPassword.Text;
        parValue[2] = txtFName.Text;
        parValue[3] = txtLName.Text;
        parValue[4] = gender;
        parValue[5] = (cb.SelectedIndex + 1).ToString();
        int affect = _clsT.Insert_Data("sp_Insert_User", parName, parValue);
        if (affect > 0)
            MessageBox.Show("ok");
        else
            MessageBox.Show(affect.ToString());
    }

这里是调用存储过程并发送参数的Insert_Data函数...

protected int executeCommand(string query, Param[] p)
{
        try
        {
            connect();
            int affect = 0;
            SqlCommand cmd = new SqlCommand(query, _con);
            cmd.CommandType = CommandType.StoredProcedure;
            int count = p.GetUpperBound(0) + 1;
            for (int i = 0; i < count; i++)
                cmd.Parameters.AddWithValue(p[i].parName, p[i].parValue);
            affect = cmd.ExecuteNonQuery();
            return affect;
        }
        catch(SqlException ex)
        {
            return ex.Number;
        }
    }

最后这里是存储过程代码:

ALTER PROCEDURE [dbo].[sp_Insert_User]
     @username varchar(20), 
     @password varchar(20),
     @fName nvarchar(20),
     @lName nvarchar(20),
     @gender bit,
     @post tinyint
AS
BEGIN
    INSERT INTO admin(username, password, fName, lName, gender, post)
    VALUES (@username, @password, @fName, @lName, @gender, @post)
END

当我使用这些值(j123, s123, john, smith, true, 1)执行此存储过程时,存储过程正常执行且受影响的值,但是当我尝试执行我的C#代码时,我收到8114错误。

所以我搜索并发现原因错误,因为性别价值,它的类型是bool。

之后我尝试发送0或1而不是false或true但错误仍然存​​在。

那你能告诉我一些消除这个错误的指南吗?

1 个答案:

答案 0 :(得分:2)

如果您read the documentation,CLR将Sql Server数据类型bit映射到C#boolNullable<bool>(又名bool?)或{{1} }。

所以......

给出一张表,

SqlBoolean

并给出了存储过程,

create table dbo.foo
(
  id         int          not null identity(1,1) primary key clustered ,
  name       varchar(200) not null ,
  gender     bit              null ,
  send_email bit          not null ,
)

你应该只能说出神奇的话语:

[编辑注意您必须跳过视图箍以使可空的bool正常工作并使用空值。一致性是小脑袋的大人物。]

create procedure dbo.InsertFoo

  @name       varchar(200) ,
  @gender     bit ,
  @send_email bit 

as

  insert dbo.foo ( name , gender , send_email )
  values ( @name , @gender , @send_email )

go

得到你想要的东西。这是一个测试用例:

static int InsertFoo( string name , bool? gender , bool sendEmail )
{
  int rowcount ;

  using ( SqlConnection conn = new SqlConnection("your-connect-string-here"))
  using ( SqlCommand cmd = conn.CreateCommand() )
  {

    cmd.CommandText = @"dbo.InsertFoo"                       ;
    cmd.CommandType = CommandType.StoredProcedure            ;
    cmd.Parameters.AddWithValue( "@name"       , name      ) ;
    cmd.Parameters.AddWithValue( "@gender"     , gender.HasValue ? (object) gender : (object)DBNull.Value    ) ;
    cmd.Parameters.AddWithValue( "@send_email" , sendEmail ) ;

    conn.Open() ;
    rowcount = cmd.ExecuteNonQuery() ;
    conn.Close() ;

  }
  return rowcount ;
}

哪个产生

int rc1 = InsertFoo( "john"    , null  , false ) ;
int rc2 = InsertFoo( "jane"    , null  , true  ) ;
int rc3 = InsertFoo( "sally"   , false , false ) ;
int rc4 = InsertFoo( "rebecca" , false , true  ) ;
int rc5 = InsertFoo( "emily"   , true  , false ) ;
int rc6 = InsertFoo( "zoe"     , true  , true  ) ;

正如您所料。