我有一個返回3個表的存儲過程。我所關心的與List
填充是存儲過程返回的最後一個表的結果集;其中有3列。我有以下代碼至今:從返回多個表的存儲過程的表中填充c#列表
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 ! ");
}
我需要分別填寫列表像列1,列2,欄3儘可能多行有這樣我就可以填充一個HTML表格。
可否告誡我做錯了什麼;我採取了正確的方法嗎?
謝謝(我開始獲得在C#中多一點進步,我做多分貝DEV)
編輯:
以下是第三表的存儲過程返回的例子:
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++;
*請*把更多的精力投入到格式化你的代碼時,你問一個問題。現在你應該有使用編輯器的竅門 - 不要讓其他人做你的問題可讀的工作。 –
我向社區道歉 –
接下來,你的問題確實不清楚 - 你有一個'List'......你如何期望每行有三個值?你的意思是你想要列表的總大小爲3 *行數?也不清楚爲什麼你要調用'ToString'或試圖將它們添加到'resultSet'(我們還沒有看到)如果你試圖填充一個列表... –