【发布时间】:2014-01-17 05:12:21
【问题描述】:
我正在从 C# 代码执行存储过程
private void DataToGrid()
{
DataTable dt = new DataTable();
string[,] aryPara = new string[,]
{{ "@pClassId", "10" } ,
{ "@pMediumId", "11" } ,
{ "@pStreamId", "12" } ,
{ "@pSessionId","13" } ,
{ "@pSectionId", "15" } ,
{ "@pShiftId", "16" } ,
{ "@pDateId", "17" } };
dt = CallStoredProcedureUsingAdapterFillDataTabel("ssspAtdDailyattendance", aryPara);
DatagridView1.DataSource = dt;
}
public DataTable CallStoredProcedureUsingAdapterFillDataTabel(string StoredProcedureName, [Optional] string[,] aryParameters)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=AIS-OCTACORE\SQLserver2008r2;Initial Catalog= SchoolSoulDataTest; integrated security=SSPI";
con.open();
SqlCommand lSQLCmd = new SqlCommand();
SqlDataAdapter adp = new SqlDataAdapter();
DataTable dt = new DataTable();
lSQLCmd.CommandType = CommandType.StoredProcedure;
lSQLCmd.CommandText = StoredProcedureName;
lSQLCmd.CommandTimeout = 300;
try
{
for (int i = 0; i < aryParameters.GetLength(0); i++)
{
lSQLCmd.Parameters.Add(new SqlParameter(aryParameters[i, 0], aryParameters[i, 1]));
}
}
catch (Exception ex)
{
}
lSQLCmd.Connection = con;
adp.SelectCommand = lSQLCmd;
adp.Fill(dt);
clsConnectionClose();
return dt;
}
其中ssspAtdDailyattendance 是一个动态存储过程,其中存储过程返回的数据具有可变数量的列。
现在我想将 DataTable dt 转换为 List<T> 但由于 dt 返回可变数量的列,因此 T 的类型不是固定的
所以我的问题是如何将dt 转换为List?
【问题讨论】: