第七周学习笔记
本周笔记主要围绕的是适配器、数据表、数据网络视图的综合应用。
一、数据适配器和数据表的应用
利用数据网格视图来显示信息,实现载入,提交功能。数据库端数据也同步更新。
主要代码如下:
private void btn_Load_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_doctor;";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataTable doctorTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(doctorTable);
sqlConnection.Close();
this.dgv_Score.DataSource = doctorTable;
}
private void btn_Submit_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand insertCommand = new SqlCommand();
insertCommand.Connection = sqlConnection;
insertCommand.CommandText =
"INSERT tb_doctor"
+ "(no,name,sex,post,department,phone,birthdate)"
+ " VALUES(@no,@name,@sex,@post,@department,,@phone,@birthdate);";
insertCommand.Parameters.Add("@no", SqlDbType.Char, 10, "no");
insertCommand.Parameters.Add("@name", SqlDbType.VarChar, 0, "name");
insertCommand.Parameters.Add("@sex", SqlDbType.VarChar, 0, "sex");
insertCommand.Parameters.Add("@post", SqlDbType.VarChar, 0, "post");
insertCommand.Parameters.Add("@department", SqlDbType.VarChar, 0, "department");
insertCommand.Parameters.Add("@phone", SqlDbType.VarChar, 0, "phone");
insertCommand.Parameters.Add("@birthdate", SqlDbType.VarChar, 0, "birthdate");
SqlCommand updateCommand = new SqlCommand();
updateCommand.Connection = sqlConnection;
updateCommand.CommandText =
"UPDATE tb_doctor"
+ " SET [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]"
+ " WHERE [email protected];";
updateCommand.Parameters.Add("@NewNo", SqlDbType.Char, 10, "no");
updateCommand.Parameters.Add("@name", SqlDbType.VarChar, 0, "name");
updateCommand.Parameters.Add("@sex", SqlDbType.VarChar, 0, "sex");
updateCommand.Parameters.Add("@post", SqlDbType.VarChar, 0, "post");
updateCommand.Parameters.Add("@department", SqlDbType.VarChar, 0, "department");
updateCommand.Parameters.Add("@phone", SqlDbType.VarChar, 0, "phone");
updateCommand.Parameters.Add("@birthdate", SqlDbType.VarChar, 0, "birthdate");
updateCommand.Parameters.Add("@OldNo", SqlDbType.Char, 10, "no");
updateCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
SqlCommand deleteCommand = new SqlCommand(); //声明并实例化SQL命令;该命令用于删除;
deleteCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
deleteCommand.CommandText = //指定SQL命令的命令文本;
"DELETE tb_doctor"
+ " WHERE [email protected];";
deleteCommand.Parameters.Add("@no", SqlDbType.Char, 10, "no");
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
//声明并实例化SQL数据适配器;
sqlDataAdapter.InsertCommand = insertCommand; //将SQL数据适配器的属性InsertCommand指向用于插入记录的SQL命令;
sqlDataAdapter.UpdateCommand = updateCommand; //将SQL数据适配器的属性UpdateCommand指向用于更新记录的SQL命令;
sqlDataAdapter.DeleteCommand = deleteCommand; //将SQL数据适配器的属性DeleteCommand指向用于删除记录的SQL命令;
DataTable doctorTable1 = (DataTable)this.dgv_Score.DataSource;
//声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型;
sqlConnection.Open(); //打开SQL连接;
int rowAffected = sqlDataAdapter.Update(doctorTable1);
//SQL数据适配器根据学生数据表提交所有更新,并返回受影响行数;
sqlConnection.Close(); //关闭SQL连接;
MessageBox.Show("更新" + rowAffected.ToString() + "行。");
}
二、网络视图的应用
C#界面设置载入、根据工号搜索、根据姓名搜索、根据拼音缩写搜索四个按钮。载入按钮作用是载入医生信息表的信息。
C#主要代码如下:
public partial class frm_Doctor : Form
{
private DataTable doctorTable;
private DataView doctorViewByName;
public frm_Doctor()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.dgv_Doctor.AllowUserToAddRows = false; //数据网格视图不允许用户添加行
this.dgv_Doctor.RowHeadersVisible = false; //数据网格视图不允许用户添加行
this.dgv_Doctor.BackgroundColor = Color.White;//数据网格视图的背景色设为白色
this.dgv_Doctor.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells; //数据网格视图的自动调整列宽模式设为显示所有单元格
}
private void btn_Load_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=HISDatabase;uid=sa;[email protected]";
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_doctor;";
//指定SQL命令的命令文本;该命令查询所有医生信息,以用作数据网格视图数据源
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
//设置SQL数据适配器在缺少架构时的动作为追加主键,从而获取数据库中定义的主键
this.doctorTable = new DataTable();
//实例化本窗体的医生数据表,用于保存所有医生信息,以用作数据网格视图数据源
sqlConnection.Open();
sqlDataAdapter.Fill(this.doctorTable); //SQL数据适配器读取数据,并填充医生数据表
sqlConnection.Close();
this.doctorViewByName = new DataView();
//实例化本窗体的医生数据视图,用于按照名称进行快速查询
this.doctorViewByName.Table = this.doctorTable;//设置医生数据视图对应的数据表
this.doctorViewByName.Sort = "name ASC";//设置课程数据视图对应的数据表
this.dgv_Doctor.Columns.Clear();//数据网格视图的列集合清空
this.dgv_Doctor.DataSource = this.doctorTable;//将数据网格视图的数据源设为医生数据表
this.dgv_Doctor.Columns["no"].HeaderText = "工号";//设置医生数据视图的排序条件,即查询所覆盖的列this.dgv_Doctor.Columns["name"].HeaderText = "姓名";
this.dgv_Doctor.Columns["post"].HeaderText = "职称";
this.dgv_Doctor.Columns["department"].HeaderText = "科室";
this.dgv_Doctor.Columns["Pinyin"].Visible = false;
//数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘)
this.dgv_Doctor.Columns[this.dgv_Doctor.Columns.Count - 1].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}
private void btn_SearchByNo_Click(object sender, EventArgs e)
{
DataRow searchResultRow = this.doctorTable.Rows.Find(this.txb_DoctorNo.Text.Trim());
DataTable searchResultTable = this.doctorTable.Clone();
searchResultTable.ImportRow(searchResultRow);
this.dgv_Doctor.DataSource = searchResultTable;
}
private void btn_SearchByName_Click(object sender, EventArgs e)
{
DataRowView[] searchResultRowViews =
this.doctorViewByName.FindRows(this.txb_DoctorName.Text.Trim());
DataTable searchResultTable = this.doctorTable.Clone();
foreach (DataRowView dataRowView1 in searchResultRowViews)
{
searchResultTable.ImportRow(dataRowView1.Row);
}
this.dgv_Doctor.DataSource = searchResultTable;
}
private void txb_Pinyin_TextChanged(object sender, EventArgs e)
{
DataRow[] searchResultRows =
this.doctorTable.Select("Pinyin LIKE ‘%" + this.txb_Pinyin.Text.Trim() + "%‘");
//借助本窗体的课程数据表的方法Select,并提供与SQL类似的谓词表达式作为查询条件,根据拼音缩写进行模糊查询(仅支持%通配符);查询将返回数据行数组
DataTable searchResultTable = this.doctorTable.Clone();
foreach (DataRow row in searchResultRows)
{
searchResultTable.ImportRow(row);
}
this.dgv_Doctor.DataSource = searchResultTable;
}
}
运行结果如下:
载入:
根据工号搜索:
根据姓名搜索:
根据拼音缩写搜索: