添加没有值的参数

时间:2013-08-13 14:54:40

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

我有这个存储过程:

CREATE PROCEDURE SearchEmployee
@EmployeeID int,
@EmployeeName nvarchar(256),
@Address nvarchar(256),

AS

Select EmployeeId, EmployeeName, Address 
From Employee
Where
EmployeeID = @EmployeeID OR
EmployeeName LIKE '%' + @EmployeeName+ '%' OR
Address LIKE '%' + @Address+ '%' 

然后在我的Winforms中,我在comboBox1.Text = ALL

时调用它
using (SqlCommand mySqlCommand1 = new SqlCommand("dbo.SearchEmployee", myDatabaseConnection))
  {
  mySqlCommand1.CommandType = CommandType.StoredProcedure;
  if (comboBox1.Text == "ALL")
        {
         mySqlCommand1.Parameters.AddWithValue("@EmployeeID", textBox1.Text);
         mySqlCommand1.Parameters.AddWithValue("@EmployeeName", textBox1.Text);
         mySqlCommand1.Parameters.AddWithValue("@Address", textBox1.Text);
        }
  }

现在,comboBox1.Text = EmployeeID

时我将如何处理

这样的事情:?

      if (comboBox1.Text == "ALL")
        {
         mySqlCommand1.Parameters.AddWithValue("@EmployeeID", textBox1.Text);
         mySqlCommand1.Parameters.AddWithValue("@EmployeeName", novalue);
         mySqlCommand1.Parameters.AddWithValue("@Address", novalue);
        }

2 个答案:

答案 0 :(得分:0)

像这样使用System.DBNull.Value

mySqlCommand1.Parameters.AddWithValue("@EmployeeName", System.DBNull.Value);

答案 1 :(得分:0)

我认为您正在寻找DbNull.Value(名称空间系统)。

if (comboBox1.Text == "EmploueeID")
{
   mySqlCommand1.Parameters.AddWithValue("@EmployeeID", textBox1.Text);
   mySqlCommand1.Parameters.AddWithValue("@EmployeeName",DbNull.Value);
   mySqlCommand1.Parameters.AddWithValue("@Address", DbNull.Value);
}