数据集有更多时间来填充数据

时间:2016-11-01 12:21:23

标签: c# sql sql-server-2008 tsql

存储过程返回多个列并填充ds获得更多时间。

SQL += "exec EquityDealerMapping_TestDJ";
SQL += " @status_id = '" + StrStatusID + "'";
SQL += ", @status_name = '" + StrStatusName + "'";

SqlCommand Cmd = new SqlCommand(SQL, Con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
Cmd.CommandTimeout = 6000;
Da.Fill(_ds);

存储过程代码:

select  
    ROW_NUMBER() OVER(ORDER BY Client_code) AS 'SrNo',
    Client_code, Client_name,
    Branch_cd, Trading_Mode,
    Dealer_code_Existing, Dealer_code_New,
    ColorCode
from
    @mytable 
order by 
    Client_code

1 个答案:

答案 0 :(得分:0)

嗯,获得更多时间的方法是设置CommandTimeout,就像你正在做的那样。也许问题出在其他地方?

您可以尝试检查连接状态:

if (cmd.Connection.State == ConnectionState.Broken 
      || cmd.Connection.State == ConnectionState.Closed)
{
    cmd.Connection.Open();
}

处理返回大量数据的查询的另一种方法是一次处理一行,而不是首先将它全部加载到DataSet或DataTable中。

DataTable dt = new DataTable();
SqlDataReader reader = cmd.ExecuteReader();
foreach (DataRow dr in reader.GetSchemaTable().Rows)
{
    /*this was originally written to handle an arbitrary query, which means
      we might end up with multiple columns with the same name, which isn't allowed.
      use extrachars and n to make names like "ID1", "ID2", etc... */
    string colName = dr["ColumnName"].ToString();
    string extrachars = "";
    int n = 1;
    while (dt.Columns.Contains(colName + extrachars))
    {
        extrachars = n.ToString();
        n++;
    }

    dt.Columns.Add(colName + extrachars, Type.GetType(dr["DataType"].ToString()));
}

while (reader.Read())
{
    DataRow dr = dt.NewRow();
    object[] tempArray = new object[dt.Columns.Count];
    reader.GetValues(tempArray);
    dr.ItemArray = tempArray;

    //either add it to a table
    dt.Rows.Add(dr);

    //or call a function with it
    //SomeFunc(dr);
}