一、思维导图
二、知识点介绍
操作数据表,首先需要Datagridview控件,然后需要连接后端数据库。将数据导入数据表中。
1.连接数据库
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(Local);Database= OutpatientDatabase;Integrated Security=sspi"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
sqlConnection.Open(); //打开SQL连接;
sqlConnection.Close(); //关闭SQL连接;
2.导入数据表
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM Ywyz;";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器;
sqlDataAdapter.SelectCommand = sqlCommand;
DataTable ywTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(ywTable);
sqlConnection.Close();
this.dgv_ywyz.DataSource = ywTable;
3.执行数据表操作
SqlConnectionsqlConnection = newSqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
SqlCommandsqlCommand = newSqlCommand();
SqlCommandsqlCommand1 = newSqlCommand();//声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection;
sqlCommand1.Connection = sqlConnection;
sqlCommand1.CommandText = "SELECT *FROM Ks ;";
sqlCommand.CommandText = "SELECT * FROM Jxsqb;";
SqlDataAdaptersqlDataAdapter1 = newSqlDataAdapter();
SqlDataAdaptersqlDataAdapter = newSqlDataAdapter(); //声明并实例化SQL数据适配器;
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter1.SelectCommand = sqlCommand1;//将SQL数据适配器的查询命令属性指向SQL命令;
DataTablejxsqbTable = newDataTable();
DataTableksTable = newDataTable();
sqlConnection.Open();
sqlDataAdapter1.Fill(ksTable);//打开SQL连接;
sqlDataAdapter.Fill(jxsqbTable);
this.cmb_apply_sept.DataSource = ksTable;
this.cmb_apply_sept.DisplayMember = "depet_name";
this.cmb_apply_sept.ValueMember = "No"; //功能代码
sqlConnection.Close();
this.dgv_jxcqb.Columns.Clear(); //课程数据网格视图的列集合清空;
this.dgv_jxcqb.DataSource = jxsqbTable;
4.添加数据表
if(this.txb_jc_no.Text.Trim() == "") //若用户号文本框为空;
{
MessageBox.Show("检查申请号不能为空!"); //给出错误提示;
this.txb_jc_no.Focus(); //用户号文本框获得焦点;
return; //返回;
} if(this.txb_times.Text.Trim() == "") //若用户号文本框为空;
{
MessageBox.Show("就诊次数不能为空!"); //给出错误提示;
this.txb_times.Focus(); //用户号文本框获得焦点;
return; //返回;
} if(this.txb_jc_no.Text.Trim() == "") //若用户号文本框为空;
{
MessageBox.Show("检查申请号不能为空!"); //给出错误提示;
this.txb_jc_no.Focus(); //用户号文本框获得焦点;
return; //返回;
}
if(this.txb_apply_doctor.Text.Trim() == "") //若密码文本框为空;
{
MessageBox.Show("申请医生不能为空!"); //给出错误提示;
this.txb_apply_doctor.Focus(); //密码文本框获得焦点;
return; //返回;
}
SqlConnectionsqlConnection = newSqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
SqlCommandsqlCommand = sqlConnection.CreateCommand(); //调用SQL连接的方法CreateCommand来创建SQL命令;该命令将绑定SQL连接;
sqlCommand.CommandText =
"INSERT Jxsqb (jc_no,times,apply_dept,apply_doctor,apply_date,charge_sn,order_no,jc_location,exam_objective,exam_add_info,comment,patient_id) VALUES(@jc_no,@times,@apply_dept,@apply_doctor,@apply_date,@charge_sn,@order_no,@jc_location,@exam_objective,@exam_add_info,@comment,@patient_id);"; //指定SQL命令的命令文本;命令文本包含参数;
sqlCommand.Parameters.AddWithValue("@times",this.txb_times.Text.Trim());
sqlCommand.Parameters.AddWithValue("@jc_no",this.txb_jc_no.Text.Trim());
sqlCommand.Parameters.AddWithValue("@apply_dept", this.cmb_apply_sept .Text .Trim ()); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@apply_doctor", this.txb_apply_doctor.Text.Trim());
sqlCommand.Parameters.AddWithValue("@apply_date", this.dateTimePicker2.Value);
sqlCommand.Parameters.AddWithValue("@charge_sn", this.txb_cahrge_sn.Text.Trim());
sqlCommand.Parameters.AddWithValue("@order_no", this.txb_order_no.Text.Trim());
sqlCommand.Parameters.AddWithValue("@jc_location", this.txb_jc_location.Text.Trim());
sqlCommand.Parameters.AddWithValue("@exam_objective", txb_exam_objective.Text.Trim());
sqlCommand.Parameters.AddWithValue("@exam_add_info", this.txb_exam_add_info.Text.Trim());
sqlCommand.Parameters.AddWithValue("@comment", this.txb_comment.Text.Trim());
sqlCommand.Parameters.AddWithValue("@patient_id", this.txb_jcsqb_pid .Text.Trim());
sqlConnection.Open(); //打开SQL连接;
introwAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
sqlConnection.Close(); //关闭SQL连接;
if(rowAffected == 1) //若成功写入1行记录;
{
MessageBox.Show("添加成功。");
//给出正确提示;
}
else //否则;
{
MessageBox.Show("添加失败!"); //给出错误提示;
}
5.修改数据
if(dgv_jxcqb.SelectedCells.Count != 0)
{
intindex = dgv_jxcqb.CurrentRow.Index; //获取选中行的行号
txb_jc_no.Text = dgv_jxcqb.Rows[index].Cells[0].Value.ToString();
}
SqlConnectionsqlConnection = newSqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
SqlCommandsqlCommand = newSqlCommand(); //声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection;
sqlCommand.Parameters.AddWithValue("@jc_no", txb_jc_no.Text);//将SQL命令的连接属性指向SQL连接;
sqlCommand.CommandText = //指定SQL命令的命令文本;
"UPDATE Jxsqb"
+ " SET times=@times,apply_dept=@apply_dept,apply_doctor=@apply_doctor,apply_date=@apply_date,charge_sn=@charge_sn,order_no=@order_no,jc_location=@jc_location,exam_objective=@exam_objective,exam_add_info=@exam_add_info,comment=@comment,patient_id=@patient_id"
+ " WHERE jc_no=@jc_no;";
sqlCommand.Parameters.AddWithValue("@times", this.txb_times.Text.Trim());
sqlCommand.Parameters.AddWithValue("@apply_dept", this.cmb_apply_sept .Text .Trim ()); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@apply_doctor", this.txb_apply_doctor.Text.Trim());
sqlCommand.Parameters.AddWithValue("@apply_date", this.dateTimePicker2.Value);
sqlCommand.Parameters.AddWithValue("@charge_sn", this.txb_cahrge_sn.Text.Trim());
sqlCommand.Parameters.AddWithValue("@order_no", this.txb_order_no.Text.Trim());
sqlCommand.Parameters.AddWithValue("@jc_location", this.txb_jc_location.Text.Trim());
sqlCommand.Parameters.AddWithValue("@exam_objective", txb_exam_objective.Text.Trim());
sqlCommand.Parameters.AddWithValue("@exam_add_info", this.txb_exam_add_info.Text.Trim());
sqlCommand.Parameters.AddWithValue("@comment", this.txb_comment.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@patient_id", this.txb_jcsqb_pid.Text.Trim());
sqlConnection.Open(); //打开SQL连接;
introwAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
sqlConnection.Close(); //关闭SQL连接;
MessageBox.Show("更新"+ rowAffected.ToString() + "行。");
}
5.下拉框的设定
SqlConnectionsqlConnection = newSqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
"Server=(local);Database=EduBaseDemo;Integrated Security=sspi"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证);
SqlCommandsqlCommand = newSqlCommand(); //声明并实例化SQL命令;
SqlCommandsqlCommand2 = newSqlCommand(); //声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
sqlCommand2.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
sqlCommand.CommandText = "SELECT * FROM tb_Class;"; //指定SQL命令的命令文本;该命令查询所有班级,以用作下拉框数据源;
sqlCommand2.CommandText = "SELECT * FROM tb_Student;"; //指定SQL命令的命令文本;该命令查询所有学生;
SqlDataAdaptersqlDataAdapter = newSqlDataAdapter(); //声明并实例化SQL数据适配器;
sqlDataAdapter.SelectCommand = sqlCommand; //将SQL数据适配器的查询命令属性指向SQL命令;
DataTableclassTable = newDataTable(); //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源;
SqlDataAdaptersqlDataAdapter2 = newSqlDataAdapter(); //声明并实例化SQL数据适配器;
sqlDataAdapter2.SelectCommand = sqlCommand2; //将SQL数据适配器的查询命令属性指向SQL命令;
DataTablestudentTable = newDataTable(); //声明并实例化数据表,用于保存所有学生,以用作数据网格视图的数据源;
sqlConnection.Open(); //打开SQL连接;
sqlDataAdapter.Fill(classTable); //SQL数据适配器读取数据,并填充班级数据表;
sqlDataAdapter2.Fill(studentTable); //SQL数据适配器读取数据,并填充学生数据表;
sqlConnection.Close(); //关闭SQL连接;
this.dgv_Score.Columns.Clear(); //数据网格视图的列集合清空;
this.dgv_Score.DataSource = studentTable; //将数据网格视图的数据源设为学生数据表;
this.dgv_Score.Columns["No"].HeaderText = "学号"; //将数据网格视图的指定列的表头文本设为中文;
this.dgv_Score.Columns["Name"].HeaderText = "姓名";
this.dgv_Score.Columns["Gender"].HeaderText = "性别";
this.dgv_Score.Columns["BirthDate"].HeaderText = "生日";
this.dgv_Score.Columns["Speciality"].HeaderText = "特长";
this.dgv_Score.Columns["Photo"].HeaderText = "照片";
this.dgv_Score.Columns["ClassNo"].Visible = false; //将数据网格视图的指定列设为不可见;
this.dgv_Score.Columns[this.dgv_Score.Columns.Count - 1].AutoSizeMode = //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘);
DataGridViewAutoSizeColumnMode.Fill;
DataGridViewComboBoxColumnclassColumn = newDataGridViewComboBoxColumn(); //声明并实例化数据网格视图下拉框列,用于设置学生的班级;
classColumn.Name = "Class"; //设置下拉框列的名称;
classColumn.HeaderText = "班级"; //设置下拉框列的表头文本;
classColumn.DataSource = classTable; //设置下拉框列的数据源为班级数据表;
classColumn.DisplayMember = "Name"; //设置下拉框列的显示成员为(班级数据表的)名称(列);
classColumn.ValueMember = "No"; //设置下拉框列的值成员为(班级数据表的)编号(列);
classColumn.DataPropertyName = "ClassNo"; //设置下拉框列的数据属性名称为(学生数据表的)班级编号(列);
classColumn.DisplayIndex = 4; //设置下拉框列的显示顺序;
classColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //设置下拉框列的自动调整列宽模式为填充;
this.dgv_Score.Columns.Add(classColumn);