一、思维导图
二、知识点描述
以下主要介绍C#常用的各类输入输出控件在连接数据库的情况下,对数据库进行读取数据和写入数据。
1、TextBox控件
此控件为文本框控件,输入输出主要由TextBox的.text属性来完成。
输入
sqlCommand.Parameters.AddWithValue("@No", this.txt_no.Text.Trim());
输出
this.txt_no.Text = sqlDataReader["No"].ToString();
2、RadioButton 控件
此控件为单选按钮控件,当单击 RadioButton 控件时,其 Checked 属性设置为 true,并且调用 Click 事件处理程序。当 Checked 属性的值更改时,将引发 CheckedChanged 事件。如果AutoCheck 属性设置为 true(默认值),则当选择单选按钮时,将自动清除该组中的所有其他单选按钮。
输入
sqlCommand.Parameters.AddWithValue("@Sex", (bool)(this.rdb_male.Checked));
输出
this.rdb_male.Checked = (bool)sqlDataReader["Sex"];
this.rdb_remale.Checked = !(bool)sqlDataReader["Sex"];
3、ComBobox控件
此控件为组合框控件,通过一些属性的设置可以实现自动查询功能:
设置如下:
cmb_SearchId.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb_SearchId.AutoCompleteSource= AutoCompleteSource.ListItems;
再将ComboBox与数据库相应列的值进行绑定,可以大大简化查询过程。
输入
sqlCommand.Parameters.AddWithValue("@Ksno", this.cmb_ks.Text);
输出
this.cmb_ks.Text = sqlDataReader["Ksno"].ToString() ;
4 、DropDownList
此控件为下拉列表控件
输入
this.cmb_Class.SelectedValue = (int)sqlDataReader["stockNo"];
输出sqlCommand1.Parameters.AddWithValue("@stockNo",this.cmb_Class.SelectedValue.toString());
5、dateTimePicker日期控件
主要依赖于Value属性:该控件当前的日期、时间值
(1) 输入代码如下
sqlCommand.Parameters.AddWithValue("@Birthday", this.dtp_birthday.Value);
(2) 输出
this.dtp_birthday.Value = (DateTime)sqlDataReader["Birthday"];
6、datagridview
数据表控件
运行效果如下:
代码:
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand1 = new SqlCommand();
sqlCommand1.Connection = sqlConnection;
sqlCommand1.CommandText = "SELECT * FROM tb_equip;";
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = sqlCommand1;
SqlCommand sqlCommand2 = new SqlCommand();
sqlCommand2.Connection = sqlConnection;
sqlCommand2.CommandText = "SELECT * FROM tb_Department;";
SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter();
sqlDataAdapter2.SelectCommand = sqlCommand2;
sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
this.equipTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter1.Fill(this.equipTable);
sqlConnection.Close();
this.DepartmentTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter2.Fill(this.DepartmentTable);
sqlConnection.Close();
this.equipViewByName = new DataView();
this.equipViewByName.Table = this.equipTable;
this.equipViewByName.Sort = "ENO ASC";
this.dgv_Score.Columns.Clear();
this.dgv_Score.DataSource = equipTable;
this.dgv_Score.Columns["ENO"].HeaderText = "设备编码";
this.dgv_Score.Columns["E_name"].HeaderText = "设备名称";
this.dgv_Score.Columns["E_norm"].HeaderText = "规格";
this.dgv_Score.Columns["FNO"].HeaderText = "生产产商";
this.dgv_Score.Columns["E_use"].Visible = false;
this.dgv_Score.Columns["DNO"].Visible = false;
this.dgv_Score.Columns["remark"].Visible = false;
DataGridViewComboBoxColumn equipColumn = new DataGridViewComboBoxColumn();
equipColumn.Name = "Department";
equipColumn.HeaderText = " 所在科室 ";
equipColumn.DataSource = this.DepartmentTable;
equipColumn.DisplayMember = "D_name";
equipColumn.ValueMember = "DNO";
equipColumn.DataPropertyName = "DNO";
equipColumn.DisplayIndex = 3;
//equipColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
this.dgv_Score.Columns.Add(equipColumn);