如何使用 C# 和 SQL 将存储在数据库中的图像加载到图片框对象中
How to load an image that is stored in a database into a picturebox object with C# and SQL
在以下 windows 表单中双击数据网格对象中的一行时,相关信息会正确显示在辅助表单中。
但是我不确定如何使图像也显示在学生表格的图片框中。
目前的代码如下:
public bool IsUpdate { get; set; }
private void StudentForm_Load(object sender, EventArgs e)
{
//For Update Process
if (this.IsUpdate == true)
{
using (SqlConnection con = new SqlConnection(AppConnection.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("usp_Student_ReloadDataForUpdate", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentName", this.StudentName);
if (con.State != ConnectionState.Open)
con.Open();
DataTable dtStudent = new DataTable();
SqlDataReader sdr = cmd.ExecuteReader();
dtStudent.Load(sdr);
DataRow row = dtStudent.Rows[0];
StudentNameTextBox.Text = row["StudentName"].ToString();
AgeTextBox.Text = row["Age"].ToString();
GenderTextBox.Text = row["Gender"].ToString();
DescriptionTextBox.Text = row["Description"].ToString();
//IdPictureBox.Image = row["Image"].???
SaveButton.Text = "Update Student Information";
DeleteButton.Enabled = true;
}
}
}
以下是上述方法的存储过程:
CREATE PROCEDURE usp_Student_ReloadDataForUpdate
(
@StudentName NVARCHAR(200)
)
AS
BEGIN
SELECT [StudentId]
,[StudentName]
,[Age]
,[Gender]
,[Description]
,[Image]
FROM [dbo].[Students]
WHERE StudentName = @StudentName
END
数据库中的数据是这样保存的
private void SaveButton_Click(object sender, EventArgs e)
{
if(IsFormValid())
{
//Do Update Process
using (SqlConnection con = new SqlConnection(AppConnection.GetConnectionString())) //connect to database using AppConnection class and GetConnectionString method
{
using (SqlCommand cmd = new SqlCommand("usp_Student_InsertNewStudent", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentName", StudentNameTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Age", AgeTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Gender", GenderTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Description", DescriptionTextBox.Text.Trim());
var image = IdPictureBox.Image;
using (var ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = ms.ToArray();
}
cmd.Parameters.AddWithValue("@CreatedBy", LoggedInUser.UserName);
if (con.State != ConnectionState.Open)
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Student is successfully updated in the database.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
ResetFormControl();
}
}
}
存储的数据是一个byte[](Varbinary)。您应该将其转换为图像:
var pic = (byte[])row["Image"];
if (pic != null)
{
using (MemoryStream ms = new MemoryStream(pic))
{
IdPictureBox.Image = Image.FromStream(ms);
}
}
PS:我不会评论您的其余代码,就像您不应该使用 AddWithValue 而应该使用 Add。
在以下 windows 表单中双击数据网格对象中的一行时,相关信息会正确显示在辅助表单中。
但是我不确定如何使图像也显示在学生表格的图片框中。
目前的代码如下:
public bool IsUpdate { get; set; }
private void StudentForm_Load(object sender, EventArgs e)
{
//For Update Process
if (this.IsUpdate == true)
{
using (SqlConnection con = new SqlConnection(AppConnection.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("usp_Student_ReloadDataForUpdate", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentName", this.StudentName);
if (con.State != ConnectionState.Open)
con.Open();
DataTable dtStudent = new DataTable();
SqlDataReader sdr = cmd.ExecuteReader();
dtStudent.Load(sdr);
DataRow row = dtStudent.Rows[0];
StudentNameTextBox.Text = row["StudentName"].ToString();
AgeTextBox.Text = row["Age"].ToString();
GenderTextBox.Text = row["Gender"].ToString();
DescriptionTextBox.Text = row["Description"].ToString();
//IdPictureBox.Image = row["Image"].???
SaveButton.Text = "Update Student Information";
DeleteButton.Enabled = true;
}
}
}
以下是上述方法的存储过程:
CREATE PROCEDURE usp_Student_ReloadDataForUpdate
(
@StudentName NVARCHAR(200)
)
AS
BEGIN
SELECT [StudentId]
,[StudentName]
,[Age]
,[Gender]
,[Description]
,[Image]
FROM [dbo].[Students]
WHERE StudentName = @StudentName
END
数据库中的数据是这样保存的
private void SaveButton_Click(object sender, EventArgs e)
{
if(IsFormValid())
{
//Do Update Process
using (SqlConnection con = new SqlConnection(AppConnection.GetConnectionString())) //connect to database using AppConnection class and GetConnectionString method
{
using (SqlCommand cmd = new SqlCommand("usp_Student_InsertNewStudent", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StudentName", StudentNameTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Age", AgeTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Gender", GenderTextBox.Text.Trim());
cmd.Parameters.AddWithValue("@Description", DescriptionTextBox.Text.Trim());
var image = IdPictureBox.Image;
using (var ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = ms.ToArray();
}
cmd.Parameters.AddWithValue("@CreatedBy", LoggedInUser.UserName);
if (con.State != ConnectionState.Open)
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Student is successfully updated in the database.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
ResetFormControl();
}
}
}
存储的数据是一个byte[](Varbinary)。您应该将其转换为图像:
var pic = (byte[])row["Image"];
if (pic != null)
{
using (MemoryStream ms = new MemoryStream(pic))
{
IdPictureBox.Image = Image.FromStream(ms);
}
}
PS:我不会评论您的其余代码,就像您不应该使用 AddWithValue 而应该使用 Add。