第七周学习笔记之如何在前端界面操作数据表
一、知识点描述
1、利用数据绑定控件DataGridView和数据库连接,实现数据的插入,可以减少频繁的与数据库交互,缓解数据库的工作压力。
2、SqlDataReader对象是一个简单的数据集,用于从数据源中检索只读数据集,可以用于大量数据的检索。SqlDataReader只允许以只读、顺向的方式查看数据库中存储的数据,提供了一个有效率的数据查看模式,同时还非常的节省资源,因为它支持的功能比较有限。
3、可以使用SqlCommand类来实现数据的删除,但是要保证Delete语句是合法有效的,在制定Delect语句时,最重要的就是Where语句的书写。
4、水晶报表是一个很实用的报表开发工具,可以将数据表中的数据逐一显示出来,使结果更直观。(目前还不够了解,之后会继续学习)。
二、思维导图
三、效果截图
- 向数据库增加一条数据:
- 查询数据库中的记录:
- 显示数据库中的记录,并进行数据的增删改查:
将
- Combobox与数据库的一个属性绑定,实现自查询功能:
- 增加一条患者记录:
- 显示新添加的记录:
- 删除数据库中的一条记录:
- 修改数据库中的记录:
- 忘记密码涉及到数据库的修改、查询和删除:
- 通过子窗体刷新父窗体(增加、删除):
- 修改:
- 在DataGridView中直接进行多天记录的增、删、改、查:
- 打印数据库中的记录:
四、示例代码
修改:
1 private void btn_Update_Click(object sender, EventArgs e)
2 {
3 SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
4 sqlConnection.ConnectionString =
5 ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
6 SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
7 sqlCommand.CommandText =
8 "update tb_patient set P_birthday= '" + dtp_BirthDate.Value + "' ,P_nation = '" + cmb_Nation.Text + "',Feetype = '" + cmb_FeeType.Text + "',P_tel = '" + txb_Tel.Text + "',P_pym = '" + txb_PYM.Text + "' ,P_address = '" + txb_Address.Text + "' ,P_career = '" + cmb_Career .Text + "' ,P_bloodtype = '" + cmb_Blood.Text + "' ,P_allergic = '" + txb_Allergic.Text + "' where P_id = " + txb_Id.Text;
9
10 sqlConnection.Open();
11 sqlCommand.ExecuteNonQuery();
12 sqlConnection.Close();
13
14 MessageBox.Show("成功修改!");
15
16 }
删除:
1 private void btn_Delect_Click(object sender, EventArgs e)
2 {
3 SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
4 sqlConnection.ConnectionString =
5 ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
6 SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
7 sqlCommand.CommandText =
8 "delete from tb_patient where P_id=" + txb_Id.Text;
9
10 bool dd = true ;
11 try
12 {
13
14 sqlConnection.Open();
15 sqlCommand.ExecuteNonQuery();
16 }
17 catch (Exception ex)
18 {
19 dd = false;
20 Console.WriteLine("{0} Exception caught.", ex);
21 }
22 finally
23 {
24 sqlConnection.Close();
25 }
26 if (dd)
27 {
28 MessageBox.Show("成功删除!");
29 txb_Id.Text = "";
30 txb_PatientName.Text = "";
31 txb_Tel.Text = "";
32 txb_PYM.Text = "";
33 txb_Address.Text = "";
34 cmb_Career.Text = "";
35 txb_Allergic.Text = "";
36 txb_IdCard.Text = "";
37 ptb_Photo.Image = null;
38 cmb_Blood.Text = "";
39 cmb_Nation.Text = "";
40 cmb_FeeType.Text = "";
41 rdb_SexBoy.Checked = false;
42 rdb_SexGirl.Checked = false;
43 cmb_SearchId.Text = "";
44 cmb_SearchPYM.Text = "";
45 }
46 }
增加:
1 private void btn_Add_Click(object sender, EventArgs e)
2 {
3 MemoryStream memoryStream = new MemoryStream(); //声明并实例化内存流,用于读取照片的字节数据;
4 this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Bmp); //调用图像框的图像的静态方法Save,将图像保存至内存流;
5 byte[] photoBytes = new byte[memoryStream.Length]; //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
6 memoryStream.Seek(0, SeekOrigin.Begin); //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
7 memoryStream.Read(photoBytes, 0, photoBytes.Length);
8 SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
9 sqlConnection.ConnectionString =
10 ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
11 SqlCommand sqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
12 sqlCommand.CommandText =
13 "INSERT tb_patient (P_id, P_name,P_sex,P_birthday, P_nation,Feetype,P_tel,P_pym,P_address,P_career,P_bloodtype,P_allergic,P_idcard,P_Photo) VALUES(@P_id,@P_name,@P_sex,@P_birthday,@P_nation,@Feetype,@P_tel,@P_pym,@P_address,@P_career,@P_bloodtype,@P_allergic,@P_idcard,@P_Photo);"; //指定SQL命令的命令文本;命令文本包含参数;
14 sqlCommand.Parameters.AddWithValue("@P_id", this.txb_Id.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
15 sqlCommand.Parameters.AddWithValue("@P_name", this.txb_PatientName.Text.Trim());
16 sqlCommand.Parameters.AddWithValue("@P_sex", rdb_SexBoy.Checked ? "男" : "女"); //向SQL命令的参数集合添加参数的名称、值;
17 sqlCommand.Parameters.AddWithValue("@P_birthday", this.dtp_BirthDate.Value);
18 sqlCommand.Parameters.AddWithValue("@P_nation", this.cmb_Nation.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
19 sqlCommand.Parameters.AddWithValue("@Feetype", this.com_FeeType.Text.Trim());
20
21 sqlCommand.Parameters.AddWithValue("@P_tel", this.txb_Tel.Text.Trim());
22 //向SQL命令的参数集合添加参数的名称、值;
23 sqlCommand.Parameters.AddWithValue("@P_pym", this.txb_PYM.Text.Trim());
24
25 sqlCommand.Parameters.AddWithValue("@P_address", this.txb_Address.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
26
27 sqlCommand.Parameters.AddWithValue("@P_career", this.cmb_Career.Text.Trim());
28 sqlCommand.Parameters.AddWithValue("@P_bloodtype", this.cmb_Blood.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
29 sqlCommand.Parameters.AddWithValue("@P_allergic", this.txb_Allergic.Text.Trim());
30 sqlCommand.Parameters.AddWithValue("@P_idcard", this.txb_IdCard.Text.Trim());
31 sqlCommand.Parameters.AddWithValue("@P_Photo", photoBytes);
32 sqlConnection.Open(); //打开SQL连接;
33 int rowAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
34 sqlConnection.Close(); //关闭SQL连接;
35 if (rowAffected == 1) //若成功写入1行记录;
36 {
37 MessageBox.Show("添加成功!");
38 frm_PatientInformationManage PatientInformationManageForm = new frm_PatientInformationManage();
39 PatientInformationManageForm.ShowDialog(this); //给出正确提示;
40
41 }
42 else //否则;
43 {
44 MessageBox.Show("添加失败!"); //给出错误提示;
45 }
46
47
48 }
显示:
1 private void frm_PatientInformationManage_Load(object sender, EventArgs e)
2 {
3
4 SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
5 sqlConnection.ConnectionString =
6 ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
7 SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令;
8 sqlCommand.Connection = sqlConnection;
9 sqlCommand.CommandText = "select P_id as 医疗卡号,P_name as 姓名 ,P_sex as 性别,P_birthday as 生日,P_nation as 民族,Feetype as 付费类型,P_tel as 电话,P_pym as 拼音码,P_address as 地址,P_career as 职业,P_bloodtype as 血型,P_allergic as 过敏药物,P_idcard as 身份证号,P_Photo from tb_patient";
10
11 // SqlCommand sqlCommand1 = new SqlCommand(); //声明并实例化SQL命令;
12 // sqlCommand1.Connection = sqlConnection;
13 // sqlCommand1.CommandText = "select * from tb_user where U_type='3';";
14
15 sqlConnection.Open();
16 SqlDataAdapter da = new SqlDataAdapter();
17 da.SelectCommand = sqlCommand;
18 DataSet ds = new DataSet();
19 da.Fill(ds, "tb_patient");
20 dataGridView1.DataSource = ds.Tables["tb_patient"];
21 //sqlConnection.Close();
22 this.dataGridView1.Columns["P_Photo"].Visible = false; //隐藏照片列
23 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
24 sqlDataAdapter.SelectCommand = sqlCommand; //将SQL数据适配器的查询命令属性指向SQL命令;
25 DataTable tb_patient = new DataTable(); //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源;
26 //打开SQL连接;
27 sqlDataAdapter.Fill(tb_patient); //SQL数据适配器读取数据,并填充班级数据表;
28 this.cmb_SearchPYM .DataSource = tb_patient; //将班级下拉框的数据源设为班级数据表;
29 // this.cmb_SearchPYM.DisplayMember = "P_pym"; //将班级下拉框的显示成员设为班级数据表的名称列;
30 this.cmb_SearchId .DataSource = tb_patient;
31 // this.cmb_SearchId.DisplayMember = "P_id";
32 this.cmb_SearchId.DisplayMember = "医疗卡号";
33 this.cmb_SearchPYM.DisplayMember = "拼音码";
34 //this.cmb_SearchPYM.ValueMember = "U_id";
35
36 }
五、待解决的问题
1、实现各窗口之间的有序连接,以及多个窗体共用多个值的问题。
2、打印相应的数据报表,如挂号单、收费单。
3、如何将图片或GDI+绘制的图形添加到水晶报表里。
4、如何将打印出来的报表以一定的逻辑整合到一起,方便随时调取。