学习笔记(三)如何在各类控件中输入输出语句
知识点描述:
数据输入/输出主要相关控件
①文本框(TextBox)
允许用户输入文本,并提供多行编辑和密码字符掩码功能,输入输出主要由TextBox的.text属性来完成。
输入:sqlCommand.Parameters.AddWithValue("@No", this.txt_no.Text.Trim());
输出:this.txt_no.Text = sqlDataReader["No"].ToString();
②DropDownList
此控件为下拉列表控件
输入:
this.cmb_Class.SelectedValue = (int)sqlDataReader["stockNo"];
输出:sqlCommand1.Parameters.AddWithValue("@stockNo",this.cmb_Class.SelectedValue.toString());
③下拉框(ComboBox)
此控件为组合框控件,通过一些属性的设置可以实现自动查询功能:
cmb_SearchId.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cmb_SearchId.AutoCompleteSource= AutoCompleteSource.ListItems;
输入:sqlCommand.Parameters.AddWithValue("@Ksno",this.cmb_ks.Text);
输出:
this.cmb_ks.Text = sqlDataReader["Ksno"].ToString() ;
④图片框(PictureBox)
指定支持事务处理初始化,允许用户加载图片。
⑤日历框(DateTimerPick)
允许用户选择日期和时间,并以指定的格式显示该日期和时间。
输入:
sqlCommand.Parameters.AddWithValue("@Birthday",this.dtp_birthday.Value);
输出:
this.dtp_birthday.Value = (DateTime)sqlDataReader["Birthday"];
⑥单选框(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"];
思维导图:
示例代码:
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=LAPTOP-TGDPSLHN;Database=MMS;Integrated Security=sspi";
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_user WHERE [email protected];";
sqlCommand.Parameters.AddWithValue("@No", textBox1.Text);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.Read())
{
this.textBox2.Text = sqlDataReader["Uname"].ToString();
this.radioButton1.Checked = (bool)sqlDataReader["Usex"];
this.radioButton2.Checked = !(bool)sqlDataReader["Usex"];
this.dateTimePicker1.Text = sqlDataReader["Udate"].ToString();
this.textBox3.Text = sqlDataReader["Ucontact"].ToString();
}
sqlDataReader.Close();
示例截图: