如何在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