0

数据集和表适配器有什么区别,有人可以用有趣的例子来解释一下。

表适配器如何从 sql server 获取数据?

4

1 回答 1

2

您使用SqlServer 中的数据DataAdapter填充。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();
}
于 2013-08-19T15:07:36.630 回答