如何在 C# 中执行带有可空参数的存储过程?
编辑:
实际上,我已经写了下面的代码。如您所见,status 参数是一个可以为 null 的值类型。 这是对的吗?还是不是?
public void LoadAll(DataTable tb, int? status=null)
{
try
{
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = this.connectionString;
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "USP_OrganizationChartSelect";
SqlCommandBuilder.DeriveParameters(command);
command.Parameters["@Status"].Value = status.HasValue ? status : null;
if (connection.State != ConnectionState.Open)
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
tb.Clear();
adapter.Fill(tb);
adapter.Dispose();
adapter = null;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
谢谢
最佳答案
你可以试试 DBNull.Value
而不是 null
或在 nullable 完全没有值时省略参数。
关于.net:如何在 C# 中执行带有可为空参数的存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2947540/