文本框 数据库绑定 代码

private void DataBinding(string name)
{
//设置查询字符串
string commString="SELECT * From Customers WHERE "+ "ContactName="+"'"+name+"'";
//新建一个SqlCommand对象
SqlCommand comm=new SqlCommand() ;
//设置SqlCommand对象的查询语句
comm.CommandText=commString;
//设置SqlCommand对象的连接
comm.Connection=this.sqlConnection1;

//新建一个数据集存贮查询结果
DataSet dataSet2=new DataSet();
//改变数据适配器对象的查询语句
this.sqlDataAdapter1.SelectCommand=comm;
//填充数据集
this.sqlDataAdapter1.Fill(dataSet2,"Customers");

//具体对每个控件进行数据绑定
//绑定前要清空以前的绑定
this.textBox1.DataBindings.Clear();
this.textBox1.DataBindings.Add("Text",dataSet2,"Customers.CustomerID");
this.textBox2.DataBindings.Clear();
this.textBox2.DataBindings.Add("Text",dataSet2,"Customers.CompanyName");
this.textBox3.DataBindings.Clear();
this.textBox3.DataBindings.Add("Text",dataSet2,"Customers.ContactName");
this.textBox4.DataBindings.Clear();
this.textBox4.DataBindings.Add("Text",dataSet2,"Customers.ContactTitle");
this.textBox5.DataBindings.Clear();
this.textBox5.DataBindings.Add("Text",dataSet2,"Customers.Address");
this.textBox6.DataBindings.Clear();
this.textBox6.DataBindings.Add("Text",dataSet2,"Customers.City");
this.textBox7.DataBindings.Clear();
this.textBox7.DataBindings.Add("Text",dataSet2,"Customers.Region");
this.textBox8.DataBindings.Clear();
this.textBox8.DataBindings.Add("Text",dataSet2,"Customers.PostalCode");
this.textBox9.DataBindings.Clear();
this.textBox9.DataBindings.Add("Text",dataSet2,"Customers.Country");
this.textBox10.DataBindings.Clear();
this.textBox10.DataBindings.Add("Text",dataSet2,"Customers.Phone");
this.textBox11.DataBindings.Clear();
this.textBox11.DataBindings.Add("Text",dataSet2,"Customers.Fax");
}

你可能感兴趣的:(数据库)