ADO.NET 第三课 DataSet (一)

 

代码
using(SqlConnection conn=new SqlConnection)
{
conn.ConnectionString
="...";
conn.Open();
DataSet ds
=new DataSet();
DataTable dt
=new DataTable("student");
DataColumn dc1
=new DataColumn("s_id",typeof(int));
DataColumn dc2
=new DataColumn("s_name",typeof(string));
dt,Columns.add(dc1);
dt.Columns.add(dc2);
ds.Tables.add(dt);
DataRow dr
=dt.NewRow();
dr[
"s_id"]=1;
dr[
"s_name"]="金凤蝶";
dt.Rows.add(dr);
foreach(DataRow dr1 in ds.Table[0].Rows)
{
MessageBox.Show(dr1[
"s_id"]+": "+dr1["s_name"]);
}
this.TextBox1.DataBindings.add("Text",ds.Table[0],"s_id");
this.DataGridView1.Columns.add("s_id","编号");
this.DataGridView1.Columns.add("s_name","姓名");
this.DataGridView1.Columns[0].DataPropertyName="s_id";
this.DataGridView1.Columns[1].DataPropertyName="s_name";
 this.DataGridView1.DataSource=ds.Table[0];

 

posted @ 2010-08-01 11:14  WIN8新人  阅读(118)  评论(0编辑  收藏  举报