2010-11-03 128 views
2

我正在使用c#連接到sql server。如何在Winform中顯示以下查詢的結果?我想在控件中顯示這個數據集。我相信它應該是一個數據庫,但對我來說並不重要。c#在winform上從sql server顯示錶

// Initialize a connection string  
string myConnectionString = "Provider=SQLOLEDB;Data Source=hermes;" + 
    "Initial Catalog=qcvaluestest;Integrated Security=SSPI;"; 

// Define the database query  
string mySelectQuery = "select top 500 name, finalconc " + 
    "from qvalues where rowid between 0 and 25000"; 

什麼是在winform上顯示此查詢的結果的最佳方式是什麼?

回答

6

放下你的窗體上的DataGridView,並使用此代碼來填充它

using(var connection = new SqlConnection(myConnectionString)) 
using(var adapter = new SqlDataAdapter(mySelectQuery, connection)) 
{ 
    var table = new DataTable(); 
    adapter.Fill(table); 
    this.dataGridView.DataSource = table; 
} 
+0

其命名空間,我應該按順序輸入到u sqldataadpter – 2010-11-03 16:00:54

+3

@i是一個女孩:System.Data.SqlClient的; – BFree 2010-11-03 16:02:44

+0

SqlClient是您想要導入的命名空間。 – Kogitsune 2010-11-03 16:03:15