2013-08-19 41 views
0

數據集和表格適配器之間有什麼區別可以請有人解釋一下有趣的例子。數據集與表格適配器?

表適配器如何從sql server獲取數據?

+0

這對於本網站來說過於模糊和過於寬泛。你需要找一個教程。 –

+0

什麼樣的教程先生 – Mathematics

+1

一個'TableAdapter'只是一個強類型的類,它包含一個'DataAdapter',它包含select-,insert,update-和delete命令。因此,您可以使用它來填充(強類型)DataTable/DataSet或更新數據。 http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx –

回答

1

您使用DataAdapter填充來自SqlServer的數據DataSet

SqlConnection conn = new SqlConnection(connString); 
SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_tblname", conn); 

try 
{ 

    conn.Open(); 
    DataSet ds = new DataSet(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.SelectCommand = cmd; // Set the select command for the DataAdapter 
    da.Fill(ds); // Fill the DataSet with the DataAdapter 
    DataGridView1.DataSource = ds.Tables[0]; // I just displayed the results in a grid view for simplicity. If Asp.Net you will have to call a DataBind of course. 

catch (Exception ex) 
{ 
    conn.Close(); 
    conn.Dispose(); 
} 
+1

表[0]可能不是tbl_tblname,正如您所設想的那樣。 –