两种方法访问基于 SQL 的数据 :
1)使用SqlDataAdapter
SqlConnection myConnection = new SqlConnection("server=(local)//NetSDK;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "作者");
MyDataGrid.DataSource=ds.Tables["作者"].DefaultView;
MyDataGrid.DataBind();
2)使用SqlDataReader
SqlConnection myConnection = new SqlConnection("server=(local)//NetSDK;database=pubs;Trusted_Connection=yes");
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
MyDataGrid.DataSource = dr;
MyDataGrid.DataBind();
myConnection.Close();