![](/img/trans.png)
[英]Inheriting from System.Data.Common.DbCommand, Parameters property is null
[英]eExecuteDataSet for system.data.common.dbCommand?
我正在使用 system.data.common.dbcommand 進行數據庫通信,但我在其中找不到 ExecuteDataSet。 可能它不被支持。 請建議有什么方法可以讓我在一個 go 中讀取多個數據表(我的存儲過程將返回多個選擇)。
謝謝
將數據加載到數據集中實際上是數據適配器的工作,因此啟動適當的數據適配器並使用它來加載數據。 您還可以使用dataSet.Load(reader)
。
但是,請考慮一下:數據集真的是您所做工作的最佳隱喻嗎?
public DataSet ExecuteDataSet(string procName,
params IDataParameter[] procParams)
{
SqlCommand cmd;
return ExecuteDataSet(out cmd, procName, procParams);
}
public DataSet ExecuteDataSet(out SqlCommand cmd, string procName,
params IDataParameter[] procParams)
{
SqlConnection cnx = null;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
cmd = null;
try
{
//Setup command object
cmd = new SqlCommand(procName);
cmd.CommandType = CommandType.StoredProcedure;
if (procParams != null)
{
for (int index = 0; index < procParams.Length; index++)
{
cmd.Parameters.Add(procParams[index]);
}
}
da.SelectCommand = (SqlCommand)cmd;
System.Diagnostics.Trace.Write(da.SelectCommand);
//Determine the transaction owner and process accordingly
if (_isOwner)
{
cnx = new SqlConnection(GetConnectionString());
cmd.Connection = cnx;
cnx.Open();
}
else
{
cmd.Connection = _txn.Connection;
cmd.Transaction = _txn;
}
//Fill the dataset
da.Fill(ds);
}
catch
{
throw;
}
finally
{
if (da != null) da.Dispose();
if (cmd != null) cmd.Dispose();
if (_isOwner)
{
cnx.Dispose(); //Implicitly calls cnx.Close()
}`enter code here`
}
return ds;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.