文本框 数据库绑定 代码

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");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值