将列名和行从 DbDataReader 获取到列表
Get the column names and rows from a DbDataReader to a List
我正在尝试执行存储过程。存储过程名称是动态给出的。基本上它用于报告套件。当用户select report name from dropdown时,它会将sp的name传递给action result里面returns一组数据,我设法从sp中获取结果到List中数据列表。现在我正在尝试获取要添加到列表数据列表中的列名。任何帮助将不胜感激
var cmd = _ctx.Database.GetDbConnection().CreateCommand();
cmd.CommandText = uspName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@inparam_Fromdate", DateTime.Now.AddDays(1));
cmd.Parameters.Add(new SqlParameter("@inparam_Todate", "DateTime.Now.AddDays(2)));
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
List<string[]> dataList = new List<string[]>();
while (reader.Read())
{
string[] tempRow = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempRow[i] = Convert.ToString(reader.GetValue(i));
}
dataList.Add(tempRow);
}
}
cmd.CommandText = uspName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@inparam_Fromdate", "2019-05-05"));
cmd.Parameters.Add(new SqlParameter("@inparam_Todate", "2019-05-22"));
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
var dataList = new List<string[]>();
var tempCol = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempCol[i] = reader.GetName(i);
}
dataList.Add(tempCol);
while (reader.Read())
{
var tempRow = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempRow[i] = Convert.ToString(reader.GetValue(i));
}
dataList.Add(tempRow);
}
}
我正在尝试执行存储过程。存储过程名称是动态给出的。基本上它用于报告套件。当用户select report name from dropdown时,它会将sp的name传递给action result里面returns一组数据,我设法从sp中获取结果到List中数据列表。现在我正在尝试获取要添加到列表数据列表中的列名。任何帮助将不胜感激
var cmd = _ctx.Database.GetDbConnection().CreateCommand();
cmd.CommandText = uspName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@inparam_Fromdate", DateTime.Now.AddDays(1));
cmd.Parameters.Add(new SqlParameter("@inparam_Todate", "DateTime.Now.AddDays(2)));
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
List<string[]> dataList = new List<string[]>();
while (reader.Read())
{
string[] tempRow = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempRow[i] = Convert.ToString(reader.GetValue(i));
}
dataList.Add(tempRow);
}
}
cmd.CommandText = uspName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@inparam_Fromdate", "2019-05-05"));
cmd.Parameters.Add(new SqlParameter("@inparam_Todate", "2019-05-22"));
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
var dataList = new List<string[]>();
var tempCol = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempCol[i] = reader.GetName(i);
}
dataList.Add(tempCol);
while (reader.Read())
{
var tempRow = new string[reader.FieldCount];
for (var i = 0; i < reader.FieldCount; i++)
{
tempRow[i] = Convert.ToString(reader.GetValue(i));
}
dataList.Add(tempRow);
}
}