一、思维导图
二、相关知识点
数据输入/输出主要相关控件
1、下拉框(ComboBox)
显示一个可编辑的文本框,其中包含一个允许值下拉列表。
2、图片框(PictureBox)
指定支持事务处理初始化,允许用户加载图片。
3、日历框(DateTimePick)
允许用户选择日期和时间,并以指定的格式显示该日期和时间。
4、文本框(TextBox)
允许用户输入文本,并提供多行编辑和密码字符掩码功能。
5、单选框(RadioButton)
当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。
6、标签(Label)
为控件提供运行时信息或说明性文字。
三、代码
1.下拉框(ComboBox)连接数据库主要代码
sqlCommand.CommandText = "SELECT * FROM tb_Identity;";
sqlDataAdapter.Fill(identityTable);
this.cmb_Identity.DataSource = identityTable;
this.cmb_Identity.DisplayMember = "Name";
this.cmb_Identity.ValueMember = "No";
SqlDataReader sqlDataReader = sqlCommand2.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);
byte[] photoBytes = new byte[memoryStream.Length];
memoryStream.Read(photoBytes, 0, photoBytes.Length);
if (photoBytes != null) {
MemoryStream memoryStream = new MemoryStream(photoBytes);
this.ptb_Photo.Image = Image.FromStream(memoryStream);
}
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){
this.PhotoFileName = openPhotoDialog.FileName;
this.ptb_Photo.Image = Image.FromFile(this.PhotoFileName);
}3.其余控件主要实现代码
private void btn_Load_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand = new SqlCommand();
SqlCommand sqlCommand2 = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand2.Connection = sqlConnection;
sqlCommand.CommandText = "SELECT * FROM tb_Identity;";
sqlCommand2.CommandText = "SELECT * FROM tb_User WHERE [email protected];";
sqlCommand2.Parameters.AddWithValue("@No", this.txb_No.Text.Trim());
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataTable identityTable = new DataTable();
sqlConnection.Open();
sqlDataAdapter.Fill(identityTable);
this.cmb_Identity.DataSource = identityTable;
this.cmb_Identity.DisplayMember = "Name";
this.cmb_Identity.ValueMember = "No";
SqlDataReader sqlDataReader = sqlCommand2.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);
}
}
private void btn_Update_Click(object sender, EventArgs e)
{
MemoryStream memoryStream = new MemoryStream();
this.ptb_Photo.Image.Save(memoryStream, ImageFormat.Jpeg);
byte[] photoBytes = new byte[memoryStream.Length];
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.Read(photoBytes, 0, photoBytes.Length);
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "UPDATE tb_User"+ " SET [email protected],[email protected],[email protected],[email protected],[email protected],[email protected]"+ " WHERE [email protected];";
sqlCommand.Parameters.AddWithValue("@Name", this.txb_Name.Text.Trim());
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();
int rowAffected = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
MessageBox.Show("更新" + rowAffected.ToString() + "行。");
}
四、运行实例(截图)
点击按钮载入前
点击载入按钮后
日历框显示效果