代码
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];