2015-10-06 38 views
0

我有一個返回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++; 
+0

*請*把更多的精力投入到格式化你的代碼時,你問一個問題。現在你應該有使用編輯器的竅門 - 不要讓其他人做你的問題可讀的工作。 –

+0

我向社區道歉 –

+1

接下來,你的問題確實不清楚 - 你有一個'List '......你如何期望每行有三個值?你的意思是你想要列表的總大小爲3 *行數?也不清楚爲什麼你要調用'ToString'或試圖將它們添加到'resultSet'(我們還沒有看到)如果你試圖填充一個列表... –

回答

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 ! "); 
} 
+0

我正在建立一個類(不是在ASP.NET中),另一個程序員打算調用我的方法generateTable()...他使用js ....我可以仍然使用這種方法?這種類型的編程對我來說是新的。我喜歡你的方法。 –

+1

我編輯了上面的代碼,以便它可以被ajax調用使用。它是未經測試的代碼。但是,如果它不起作用,它應該是接近的。我會說,通常當我做ajax調用時,我只想以json格式輸出數據,然後我可以根據需要在javascript中選擇和使用數據。但是,如果你只是注入一個div的HTML這應該工作。 –

0

As JonSkeet suggested,什麼是最有意義的是三位小數聲明一個類爲性能(最好用理智的名字,比如下圖):

public class Volume 
{ 
    public decimal Length { get; set; } 
    public decimal Width { get; set; } 
    public decimal Height { get; set; } 
} 

然後您將創建一個的對象:

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 }); 
}; 
+0

注意:我隨機選擇了'Volume'。我只是想要三個十進制值有意義的東西。 –

+0

我正在給它一個去吧 –