一、思维导图

 

 

六周学习笔记

 

二、 相关知识点

 

数据输入/输出主要相关控件

 

1.下拉框(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();

2.数据表控件(DataGridView):

 

使用 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();                                                     

3.日期控件(DateTimePicker):主要依赖于Value属性:该控件当前的日期、时间值

(1) 输入代码如下

 

sqlCommand.Parameters.AddWithValue("@Birthday", this.dtp_birthday.Value);

(2)输出

 

this.dtp_birthday.Value = (DateTime)sqlDataReader["Birthday"];

4.下拉列表控件(DropDownList)

(1)输入:

 

this.cmb_Class.SelectedValue = (int)sqlDataReader["stockNo"];

(2)输出:

 

sqlCommand1.Parameters.AddWithValue("@stockNo",this.cmb_Class.SelectedValue.toString());

5.label 控件

 

输出:可以直接在label控件的text属性赋值,也可以用代码实现

 

label.text="这里输入要输出的值";

 6.PictureBox 控件

 

(1)输入:可以通过pictureBox控件的Image属性选择需要加载的图片

 

(2)输出:需要先通过代码打开选择图片界面

 

OpenFileDialog openPhotoDialog = new OpenFileDialog() //声明并实例化打开文件对话框;

{ //在初始化器中,设置打开文件对话框的各属性;

Title = “打开照片文件” //对话框标题;

,

Filter = “图片文件|.bmp;.jpg;.jpeg;.png” //文件格式过滤器;

,

InitialDirectory = @“C:” //初始目录;

};

选定图片后,需要用到pictureBox 控件的save方法将图像保存到MemoryStream格式的属性中,再声明一个字节数组用于保存MemoryStream属性中保存的图像,代码如下:

 

MemoryStream memoryStream = new MemoryStream(); //声明并实例化内存流,用于读取照片的字节数据;

this.pictureBox.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); //调用图像框的图像的静态方法Save,将图像保存至内存流;

byte[] photoBytes = new byte[memoryStream.Length]; //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;

memoryStream.Seek(0, SeekOrigin.Begin); //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;

memoryStream.Read(photoBytes, 0, photoBytes.Length);

 然后通过对PictureBox控件的Image属性赋值输出图像

 

this.pictureBox.Image = Image.FromStream(memoryStream);

7.单选按钮控件(RadioButton)

 

当单击 RadioButton 控件时,其 Checked 属性设置为 true,并且调用 Click 事件处理程序。当 Checked 属性的值更改时,将引发 CheckedChanged 事件。如果AutoCheck 属性设置为 true(默认值),则当选择单选按钮时,将自动清除该组中的所有其他单选按钮。

 

(1)输入:

 

sqlCommand.Parameters.AddWithValue("@Sex", (bool)(this.rdb_male.Checked));

(2)输出:

 

this.rdb_male.Checked = (bool)sqlDataReader["Sex"];

this.rdb_remale.Checked = !(bool)sqlDataReader["Sex"];

8.文本框控件(TextBox)

 

输入输出主要由TextBox的.text属性来完成。

 

(1)输入:

 

sqlCommand.Parameters.AddWithValue("@No", this.txt_no.Text.Trim());

(2)输出:

 

this.txt_no.Text = sqlDataReader["No"].ToString();

相关文章: