如何在企业库中的DbCommand上传递MySQL null参数

时间:2008-11-13 17:25:08

标签: c# .net mysql enterprise-library

我正在尝试为下面的代码中的第一个参数传递一个空值,但MySQL抱怨

Incorrect number of arguments for PROCEDURE myProc; expected 2, got 1

当我手动调用第一个参数为null的过程时,它可以工作,但是当EmptyAsNullStartsWith(employeeNumberText.Text)返回null时,它会抱怨。

Database db = DatabaseFactory.CreateDatabase(
  ConfigurationManager.AppSettings["dbType"]
);
DbCommand cmd = db.GetStoredProcCommand("staff_listforinquiry");
db.AddeParameter(
  cmd, 
  "in_employeeNumber", 
  DbType.String, 
  EmptyAsNullStartsWith(employeeNumberText.Text)
);
db.AddeParameter(
  cmd, 
  "in_name", 
  DbType.String, 
  EmptyAsNullContains(employeeNameText.Text)
);

2 个答案:

答案 0 :(得分:2)

您是否尝试过EmptyAsNullContains(employeeNameText.Text)返回DBNull而不是常规null?

答案 1 :(得分:0)

if(employeeNumberText.Text != "")
      db.AddInParameter(dbCommand, "in_employeeNumber", DbType.String, employeeNumberText.Text);
else
      db.AddInParameter(dbCommand, "in_employeeNumber", DbType.String, DBNull.Value);