我有一个返回3个表的存储过程。我关心填充List
的所有内容都是存储过程返回的最后一个表的结果集;它有3列。到目前为止,我有以下代码:
string connStr = null;
SqlConnection scnn;
SqlCommand sCmd;
string sql = null;
connStr = "Data Source=server;Initial Catalog=dbName;Integrated Security=SSPI";
sql = @"DECLARE @udt1 userDefTblType1;" +
"INSERT INTO @one (uid) VALUES (0), (1), (2), (3);" +
"DECLARE @udt2 userDefTblType2;" +
"INSERT INTO @two (uid) VALUES (0);" +
"DECLARE @udt3 userDefTblType3;" +
"INSERT INTO @three (uid) VALUES (0),(1);" +
"EXEC [dbo].[storedProcedure] @one, @two, @three;";
sqlCnn = new SqlConnection(connStr);
try
{
sCnn.Open();
sCmd = new SqlCommand(sql, sCnn);
SqlDataReader sReader = sCmd.ExecuteReader();
sReader.Read();
sReader.NextResult(); //move to next table
sReader.Read();
sReader.NextResult(); //move to next table
sReader.Read(); //table of interest
List<decimal> results = new List<decimal>();
while (sReader.Read())
{
results.Items.Add(sqlReader["column1"].ToString()); //my problem is here
results.Items.Add(sqlReader["column2"].ToString()); //my problem is here
results.Items.Add(sqlReader["column2"].ToString()); //my problem is here
};
sqlReader.Close();
sqlCmd.Dispose();
sqlCnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
我需要分别填写list1,column2,column3这样的列表,因为我可以填充HTML表格。
请你告诉我我做错了什么;我采取了正确的方法吗?
谢谢(我开始在C#中取得更多进展,我做了更多的db dev)
修改
以下是存储过程返回的第3个表的示例:
Column1 | Column2 | Column3
---------------------------
5.6 | 5.1 | 7.4 |
5.7 | 5.4 | 7.7 |
5.8 | 5.6 | 7.9 |
5.9 | 5.8 | 7.0 |
5.1 | 5.6 | 7.7 |
我已经为html表动态编写了代码。我只需要将这些结果存储在枚举它们的位置,这样就可以将值添加到我的代码中的相对html选项卡中。
修改
最后,我希望我的代码看起来像这样:
html.WriteLine("<tr>");
while (colCount <= numCol)
{
html.WriteLine("<td>" POSITION IN <LIST> + "</td>");
cFinalColCount++;
}
html.WriteLine("</tr>");
rowCount++;
答案 0 :(得分:2)
使用gridview在html中制作数据。它比构建自己的表容易得多。
C#代码:
DataTable dt = new DataTable();
try
{
sCnn.Open();
sCmd = new SqlCommand(sql, sCnn);
SqlDataReader sReader = sCmd.ExecuteReader();
sReader.Read();
sReader.NextResult(); //move to next table
sReader.Read();
sReader.NextResult(); //move to next table
dt.Load(sReader); // Convert your data reader to a DataTable
sqlReader.Close();
sqlCmd.Dispose();
sqlCnn.Close();
// UNTESTED CODE - but it should be close.
GridView gv = new GridView(); // create gridview
gv.DataSource = dt; // Set the DataTable as the DataSource for the GridView
gv.DataBind(); // Bind the Data to the GridView
StringWriter sWriter = new StringWriter(); //stringwriter needed for html writer
HtmlTextWriter htWriter = new HtmlTextWriter(sWriter); // create HthmWriter
gv.RenderControl(htWriter); //render the gridview in the htmlwriter
htWriter.Write(true); // write the html writer to the output stream
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
答案 1 :(得分:0)
As JonSkeet suggested,最有意义的是声明一个带有三位小数的类作为属性(最好有合理的名称,如下所示):
public class Volume
{
public decimal Length { get; set; }
public decimal Width { get; set; }
public decimal Height { get; set; }
}
然后,您将创建List
个Volume
个对象:
List<Volume> results = new List<Volume>();
while (sReader.Read())
{
// Error checking elided
var length = decimal.Parse(sqlReader["column1"]);
var width = decimal.Parse(sqlReader["column2"]);
var height = decimal.Parse(sqlReader["column3"]);
results.Add(new Volume { Length = length, Width = width, Height = height });
};