ADO.NET: 发送具有空值的过程参数
ADO.NET: Send procedure parameter with null value
我需要向过程发送一个空值参数,但出现错误:
System.Data.SqlClient.SqlException: 'Procedure or function 'sel_mizanyeni' expects parameter '@subeno', which was not supplied.'
当我在运行服务器上用相同的参数在运行服务器上运行时,不会报错。当我在 C# 中使用 ADO.NET 运行 时,我得到了那个错误。
运行 SQL 服务器中的状态:
exec sel_mizanyeni
@subeno=null,
@tarih='2021-12-28 00:00:00',
@kirilimlevel=8,
@detaylevel=3,
@hesap1=N'342',
@hesap2=N'343',
@dovizcinsi=0,
@bakiyeverenler=1,
@optnetbakiye=1,
@optdisticsir=0,
@opteval=0,
@kurtarihi='2021-12-28 00:00:00',
@yilsonu=0,
@firmano=NULL,
@ekno=NULL,
@ikitariharasi=0,
@bittarih='2021-12-28 00:00:00',
@migration=1
此代码在 C# 中不起作用:
SqlCommand cmd = new SqlCommand("sel_mizanyeni", baglanti);
cmd.CommandType = CommandType.StoredProcedure;
// I need to pass NULL here, but I get the mentioned error
cmd.Parameters.AddWithValue("@subeno", null);
cmd.Parameters.AddWithValue("@tarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@kirilimlevel", 8);
cmd.Parameters.AddWithValue("@detaylevel", 3);
cmd.Parameters.AddWithValue("@hesap1", "342");
cmd.Parameters.AddWithValue("@hesap2", "343");
cmd.Parameters.AddWithValue("@dovizcinsi", 0);
cmd.Parameters.AddWithValue("@bakiyeverenler", 1);
cmd.Parameters.AddWithValue("@optnetbakiye", 1);
cmd.Parameters.AddWithValue("@optdisticsir", 0);
cmd.Parameters.AddWithValue("@opteval", 0);
cmd.Parameters.AddWithValue("@kurtarihi", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@yilsonu", 0);
cmd.Parameters.AddWithValue("@firmano", null);
cmd.Parameters.AddWithValue("@ekno", null);
cmd.Parameters.AddWithValue("@ikitariharasi", 0);
cmd.Parameters.AddWithValue("@bittarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("migration", 1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
来自 Specifying parameter data types - ADO.NET documentation,
When you send a null parameter value to the server, you must specify DBNull, not null (Nothing in Visual Basic). The null value in the system is an empty object that has no value. DBNull is used to represent null values.
因此,使用 DBNull.Value
而不是 null
作为参数值。
我需要向过程发送一个空值参数,但出现错误:
System.Data.SqlClient.SqlException: 'Procedure or function 'sel_mizanyeni' expects parameter '@subeno', which was not supplied.'
当我在运行服务器上用相同的参数在运行服务器上运行时,不会报错。当我在 C# 中使用 ADO.NET 运行 时,我得到了那个错误。
运行 SQL 服务器中的状态:
exec sel_mizanyeni
@subeno=null,
@tarih='2021-12-28 00:00:00',
@kirilimlevel=8,
@detaylevel=3,
@hesap1=N'342',
@hesap2=N'343',
@dovizcinsi=0,
@bakiyeverenler=1,
@optnetbakiye=1,
@optdisticsir=0,
@opteval=0,
@kurtarihi='2021-12-28 00:00:00',
@yilsonu=0,
@firmano=NULL,
@ekno=NULL,
@ikitariharasi=0,
@bittarih='2021-12-28 00:00:00',
@migration=1
此代码在 C# 中不起作用:
SqlCommand cmd = new SqlCommand("sel_mizanyeni", baglanti);
cmd.CommandType = CommandType.StoredProcedure;
// I need to pass NULL here, but I get the mentioned error
cmd.Parameters.AddWithValue("@subeno", null);
cmd.Parameters.AddWithValue("@tarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@kirilimlevel", 8);
cmd.Parameters.AddWithValue("@detaylevel", 3);
cmd.Parameters.AddWithValue("@hesap1", "342");
cmd.Parameters.AddWithValue("@hesap2", "343");
cmd.Parameters.AddWithValue("@dovizcinsi", 0);
cmd.Parameters.AddWithValue("@bakiyeverenler", 1);
cmd.Parameters.AddWithValue("@optnetbakiye", 1);
cmd.Parameters.AddWithValue("@optdisticsir", 0);
cmd.Parameters.AddWithValue("@opteval", 0);
cmd.Parameters.AddWithValue("@kurtarihi", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("@yilsonu", 0);
cmd.Parameters.AddWithValue("@firmano", null);
cmd.Parameters.AddWithValue("@ekno", null);
cmd.Parameters.AddWithValue("@ikitariharasi", 0);
cmd.Parameters.AddWithValue("@bittarih", "2021-12-28 00:00:00");
cmd.Parameters.AddWithValue("migration", 1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
来自 Specifying parameter data types - ADO.NET documentation,
When you send a null parameter value to the server, you must specify DBNull, not null (Nothing in Visual Basic). The null value in the system is an empty object that has no value. DBNull is used to represent null values.
因此,使用 DBNull.Value
而不是 null
作为参数值。