一、思维导图
二、相关知识点
数据输入/输出主要相关控件
①下拉框(ComboBox)
显示一个可编辑的文本框,其中包含一个允许值下拉列表。
②图片框(PictureBox)
指定支持事务处理初始化,允许用户加载图片。
③日历框(DateTimePick)
允许用户选择日期和时间,并以指定的格式显示该日期和时间。
④文本框(TextBox)
允许用户输入文本,并提供多行编辑和密码字符掩码功能。
⑤单选框(RadioButton)
当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。
⑥标签(Label)
为控件提供运行时信息或说明性文字。
三、代码
1.下拉框(ComboBox)连接数据库主要代码
sqlCommand.CommandText = "SELECT * FROM tb_Identity;";
//指定SQL命令的命令文本;该命令查询所有用户身份,以用作下拉框数据源;
sqlDataAdapter.Fill(identityTable);
//SQL数据适配器读取数据,并填充用户身份数据表;
this.cmb_Identity.DataSource = identityTable;
//将用户身份下拉框的数据源设为用户身份数据表;
this.cmb_Identity.DisplayMember = "Name";
//将用户身份下拉框的显示成员设为用户身份数据表的名称列;
this.cmb_Identity.ValueMember = "No";
//将用户身份下拉框的值成员设为用户身份数据表的编号列;
SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
//调用SQL命令的方法ExecuteReader来执行命令,并获取数据阅读器;
if (sqlDataReader.Read())
//若数据阅读器成功读取到下一条记录(首次查询则表示第一条记录);
{
this.cmb_Identity.SelectedValue = (int)sqlDataReader["IdentityNo"];
}
2.图片框(PictureBox)主要代码
photoBytes =
(sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]);
//根据照片是否为数据库空值,分别将空值、转为字节数组的照片数据赋予事先声明的字节数组;
MemoryStream memoryStream = new MemoryStream();
//声明并实例化内存流,用于读取照片的字节数据;
this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Jpeg); //调用图像框的图像的静态方法Save,将图像保存至内存流;
byte[] photoBytes = new byte[memoryStream.Length];
//声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
memoryStream.Seek(0, SeekOrigin.Begin);
//保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
memoryStream.Read(photoBytes, 0, photoBytes.Length);
//将内存流读入字节数组;
if (photoBytes != null)
//若用户的照片非空;
{
MemoryStream memoryStream = new MemoryStream(photoBytes);
//声明并实例化内存流,用于读取照片的字节数据;
this.ptb_Photo.Image = Image.FromStream(memoryStream);
//调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
}
private void btn_OpenPhoto_Click(object sender, EventArgs e)
{
OpenFileDialog openPhotoDialog = new OpenFileDialog()
//声明并实例化打开文件对话框;
{
//在初始化器中,设置打开文件对话框的各属性;
Title = "打开照片文件"
//对话框标题;
,
Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.png"
//文件格式过滤器;
,
InitialDirectory = @"C:\"
//初始目录;
};
if (openPhotoDialog.ShowDialog() == DialogResult.OK)
//显示打开文件对话框,若打开文件对话框的对话结果为点击OK键;
{
this.PhotoFileName = openPhotoDialog.FileName;
//将对话框获得的文件名,存入本窗体的私有字段中;
this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);
//调用图像的静态方法FromFile从指定文件中读取图像,并赋予图像框;
}
}
3.其余控件主要实现代码
private void btn_Load_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令;
SqlCommand sqlCommand2 = new SqlCommand(); //声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
sqlCommand2.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
sqlCommand.CommandText = "SELECT * FROM tb_Identity;"; //指定SQL命令的命令文本;该命令查询所有用户身份,以用作下拉框数据源;
sqlCommand2.CommandText = "SELECT * FROM tb_User WHERE No=@No;"; //指定SQL命令的命令文本;该命令查询指定用户;
sqlCommand2.Parameters.AddWithValue("@No", this.txb_No.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
sqlDataAdapter.SelectCommand = sqlCommand; //将SQL数据适配器的查询命令属性指向SQL命令;
DataTable identityTable = new DataTable(); //声明并实例化数据表,用于保存所有用户身份,以用作下拉框数据源;
sqlConnection.Open(); //打开SQL连接;
sqlDataAdapter.Fill(identityTable); //SQL数据适配器读取数据,并填充用户身份数据表;
this.cmb_Identity.DataSource = identityTable; //将用户身份下拉框的数据源设为用户身份数据表;
this.cmb_Identity.DisplayMember = "Name"; //将用户身份下拉框的显示成员设为用户身份数据表的名称列;
this.cmb_Identity.ValueMember = "No"; //将用户身份下拉框的值成员设为用户身份数据表的编号列;
SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); //调用SQL命令的方法ExecuteReader来执行命令,并获取数据阅读器;
byte[] photoBytes = null; //声明字节数组,用于保存照片数据;但先赋予空值;
if (sqlDataReader.Read()) //若数据阅读器成功读取到下一条记录(首次查询则表示第一条记录);
{
this.txb_No.Text = sqlDataReader["No"].ToString(); //在数据阅读器的索引器中指定列名,从而访问当前记录的指定列的值,并赋予相应控件;
this.txb_Name.Text = sqlDataReader["Name"].ToString();
this.rdb_Male.Checked = (bool)sqlDataReader["Gender"];
this.rdb_Female.Checked = !(bool)sqlDataReader["Gender"];
this.dtp_BirthDate.Value = (DateTime)sqlDataReader["BirthDate"];
this.dtp_AdmitDate.Value = (DateTime)sqlDataReader["AdmitDate"];
this.cmb_Identity.SelectedValue = (int)sqlDataReader["IdentityNo"];
this.txb_Intro.Text = sqlDataReader["Intro"].ToString();
photoBytes =
(sqlDataReader["Photo"] == DBNull.Value ? null : (byte[])sqlDataReader["Photo"]); //根据照片是否为数据库空值,分别将空值、转为字节数组的照片数据赋予事先声明的字节数组;
}
sqlDataReader.Close(); //关闭数据阅读器(同时关闭连接);
if (photoBytes != null) //若用户的照片非空;
{
MemoryStream memoryStream = new MemoryStream(photoBytes); //声明并实例化内存流,用于读取照片的字节数据;
this.ptb_Photo.Image = Image.FromStream(memoryStream); //调用图像的静态方法FromStream从内存流中读取图像,并赋予图像框;
}
}
private void btn_Update_Click(object sender, EventArgs e)
{
MemoryStream memoryStream = new MemoryStream(); //声明并实例化内存流,用于读取照片的字节数据;
this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Jpeg); //调用图像框的图像的静态方法Save,将图像保存至内存流;
byte[] photoBytes = new byte[memoryStream.Length]; //声明并实例化字节数组,用于保存照片数据;数组长度对应内存流长度;
memoryStream.Seek(0, SeekOrigin.Begin); //保存后的内存流的偏移位置在末尾,需通过查找来将偏移位置设为起始;
memoryStream.Read(photoBytes, 0, photoBytes.Length); //将内存流读入字节数组;
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Sql"].ConnectionString; //配置管理器从配置文件读取连接字符串,并将之赋予SQL连接的连接字符串属性;
SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令;
sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接;
sqlCommand.CommandText = //指定SQL命令的命令文本;
"UPDATE tb_User"
+ " SET Name=@Name,Gender=@Gender,BirthDate=@BirthDate,IdentityNo=@IdentityNo,Intro=@Intro,Photo=@Photo"
+ " WHERE No=@No;";
sqlCommand.Parameters.AddWithValue("@Name", this.txb_Name.Text.Trim()); //向SQL命令的参数集合添加参数的名称、值;
sqlCommand.Parameters.AddWithValue("@Gender", this.rdb_Male.Checked);
sqlCommand.Parameters.AddWithValue("@BirthDate", this.dtp_BirthDate.Value);
sqlCommand.Parameters.AddWithValue("@IdentityNo", (int)this.cmb_Identity.SelectedValue);
sqlCommand.Parameters.AddWithValue("@Intro", this.txb_Intro.Text.Trim());
sqlCommand.Parameters.AddWithValue("@Photo", photoBytes);
sqlCommand.Parameters.AddWithValue("@No", this.txb_No.Text.Trim());
sqlConnection.Open(); //打开SQL连接;
int rowAffected = sqlCommand.ExecuteNonQuery(); //调用SQL命令的方法ExecuteNonQuery来执行命令,向数据库写入数据,并返回受影响行数;
sqlConnection.Close(); //关闭SQL连接;
MessageBox.Show("更新" + rowAffected.ToString() + "行。"); //在消息框显示受影响行数;
}
四、运行实例(截图)
点击按钮载入前
点击载入按钮后
日历框显示效果