如何使用C#和SQL将存储在数据库中的图像加载到picturebox对象中
在以下windows窗体中双击datagrid对象中的行时,相关信息将正确显示在辅助窗体中 但是,我不知道如何使图像也显示在学生表单的picturebox中 以下是迄今为止的代码:如何使用C#和SQL将存储在数据库中的图像加载到picturebox对象中,c#,sql,visual-studio,winforms,picturebox,C#,Sql,Visual Studio,Winforms,Picturebox,在以下windows窗体中双击datagrid对象中的行时,相关信息将正确显示在辅助窗体中 但是,我不知道如何使图像也显示在学生表单的picturebox中 以下是迄今为止的代码: public bool IsUpdate { get; set; } private void StudentForm_Load(object sender, EventArgs e) { //For Update Process
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();
}
}
}
存储的数据是一个字节[](Varbinary)。您应该将其转换为图像:
var pic = (byte[])row["Image"];
if (pic != null)
{
using (MemoryStream ms = new MemoryStream(pic))
{
IdPictureBox.Image = Image.FromStream(ms);
}
}
PS:我不是在评论代码的其余部分,就像你不应该使用AddWithValue而是Add。好的,谢谢你的建议,我会相应地更新代码。