Winfrom开发笔记(Winfrom Development Notes)-其他
Winfrom开发笔记(Winfrom Development Notes)
Winfrom开发
第一单元
第二单元
2.1、连接数据库
1.连接数据库,查询数据
1.1获取数据库连接字符串
string connString = "Data Source=.;Initial Catalog=studentInfo;Integrated Security=True";
1.2创建连接对象
~~~c#
SqlConnection conn = new SqlConnection(connString); ~~~
1.3断开式查询技术
数据库(相当于京东华中总仓) 创建数据集对象(相当于京东在南昌本地的仓库)
DataSet ds = new DataSet();
查询命令文本(T-SQL语句)
~~~c#
string sql=”select * from classes”; ~~~
创建数据适配器对象(相当于京东的物流卡车)
~~~c#
SqlDataAdapter sda = new SqlDataAdapter(sql, conn); ~~~
用适配器填充数据集
~~~c#
sda.Fill(ds); ~~~
2.将查询到的数据绑定到控件上
设置ComboBox控件的数据源属性
cmbClass.DataSource = ds.Tables[0];
设置ComboBox控件的列表项的值所绑定的字段
cmbClass.ValueMember = "id";
//设置ComboBox控件的列表项的文本所绑定的字段
~~~c#
cmbClass.DisplayMember = “className”; ~~~
案例代码:
private void Form1_Load(object sender, EventArgs e) { //加载所有班级 GetAllClasses(); } private void GetAllClasses() { //1.获取连接数据库字符串 //Data Source=.;Initial Catalog=studentInfo;Integrated Security=True string connString = "Data Source =.; Initial Catalog = studentInfo; Integrated Security = True"; //2.创建连接对象 SqlConnection conn = new SqlConnection(connString); //3.连接数据库,查询数据 //数据库(相对于京东华中总仓) //创建数据库集对象(相对于京东在南昌在本地的仓库) DataSet ds = new DataSet(); //查询命令文本 String sql = "select * from classes"; //创建数据库适配器对象(相对于京东的物流卡车) SqlDataAdapter sda = new SqlDataAdapter(sql,conn); //用适配器填充数据集 sda.Fill(ds); //4.将数据库绑定到控件上 //设置comboBx控件的数据源属性 cmbClass.DataSource = ds.Tables[0]; //设置comboBx控件的列表项的文本所绑定的值 cmbClass.ValueMember = "id"; //设置comboBx控件的列表项的文本所绑定的值 cmbClass.DisplayMember = "className"; }
Winfrom开发
Unit 1
Unit 2
2.1. Connect to the database
1. Connect to database and query data
1.1 get database connection string
string connString = "Data Source=.;Initial Catalog=studentInfo;Integrated Security=True";
1.2 create connection object
~~~c#
SqlConnection conn = new SqlConnection(connString); ~~~
1.3 disconnected query technology
Database (equivalent to JD central China general warehouse) creates dataset objects (equivalent to JD local warehouse in Nanchang)
DataSet ds = new DataSet();
Query command text (T-SQL statement)
~~~c#
string sql=”select * from classes”; ~~~
Create a data adapter object (equivalent to JD’s logistics truck)
~~~c#
SqlDataAdapter sda = new SqlDataAdapter(sql, conn); ~~~
Populate dataset with adapter
~~~c#
sda.Fill(ds); ~~~
2. Bind the queried data to the control
Set the data source properties of the ComboBox control
cmbClass.DataSource = ds.Tables[0];
Sets the field to which the value of the list item of the ComboBox control is bound
cmbClass.ValueMember = "id";
//Sets the field to which the text of the list item of the ComboBox control is bound
~~~c#
cmbClass.DisplayMember = “className”; ~~~
Case code:
private void Form1_Load(object sender, EventArgs e) { //加载所有班级 GetAllClasses(); } private void GetAllClasses() { //1.获取连接数据库字符串 //Data Source=.;Initial Catalog=studentInfo;Integrated Security=True string connString = "Data Source =.; Initial Catalog = studentInfo; Integrated Security = True"; //2.创建连接对象 SqlConnection conn = new SqlConnection(connString); //3.连接数据库,查询数据 //数据库(相对于京东华中总仓) //创建数据库集对象(相对于京东在南昌在本地的仓库) DataSet ds = new DataSet(); //查询命令文本 String sql = "select * from classes"; //创建数据库适配器对象(相对于京东的物流卡车) SqlDataAdapter sda = new SqlDataAdapter(sql,conn); //用适配器填充数据集 sda.Fill(ds); //4.将数据库绑定到控件上 //设置comboBx控件的数据源属性 cmbClass.DataSource = ds.Tables[0]; //设置comboBx控件的列表项的文本所绑定的值 cmbClass.ValueMember = "id"; //设置comboBx控件的列表项的文本所绑定的值 cmbClass.DisplayMember = "className"; }