如何在c#中使用可为空的参数执行存储过程?.net:如何在c#中使用可为空的参数执行存储过程?
编辑:
其实,我写了下面的代码。如你所见,status参数是一个可为空的值类型。 这是正确的吗?或不?
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;
}
}
由于
我们在这里需要更多的细节:哪个数据库服务器? – 2010-06-01 05:58:03
@jeremy:好的,SQL Server – odiseh 2010-06-01 06:02:38
你应该在你的数据适配器上放一个'use()'。 – 2012-08-17 17:06:29