C# 在sql中插入和更新图像

C# 在sql中插入和更新图像,c#,sql,C#,Sql,通过从图片框中读取图像,我将图像存储到表test(id,name,image)中的数据库中 这是我的代码: private void browse_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "(*.BMP;*.JPG;*.GIF;*.JPEG;*.PNG)|*.BMP;*.JPG;*.GIF;

通过从图片框中读取图像,我将图像存储到表
test(id,name,image)
中的数据库中

这是我的代码:

private void browse_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "(*.BMP;*.JPG;*.GIF;*.JPEG;*.PNG)|*.BMP;*.JPG;*.GIF;*.JPEG;*.PNG";
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        imgloc = openFileDialog1.FileName.ToString();
        pictureBox1.ImageLocation = imgloc;
    }
}

private void save_Click(object sender, EventArgs e)
{
    byte[] img = null;
    FileStream fs = new FileStream(imgloc, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    img = br.ReadBytes((int)fs.Length);
    SqlConnection CN = new SqlConnection(constring);
    string Query = "insert into test (id,name,image) values('" + txtid.Text + "','" + txtname.Text + "',@img)";
    CN.Open();
    cmd = new SqlCommand(Query, CN);
    cmd.Parameters.Add(new SqlParameter("@img", img)); 
    cmd.ExecuteNonQuery();
    CN.Close();
}

它可以工作,但我想知道如何在这里使用update命令。

您的代码和查询应该如下所示:

SqlConnection CN = new SqlConnection(constring);
string Query = "Update test Set name=@Name,image=@Image where id=@id"
CN.Open();
cmd = new SqlCommand(Query, CN);
cmd.Parameters.Add(new SqlParameter("@Image", img));
cmd.Parameters.Add(new SqlParameter("@Name",txtname.Text));
cmd.Parameters.Add(new SqlParameter("@id",txtid.Text));
cmd.ExecuteNonQuery();
CN.Close();

您的代码和查询应如下所示:

SqlConnection CN = new SqlConnection(constring);
string Query = "Update test Set name=@Name,image=@Image where id=@id"
CN.Open();
cmd = new SqlCommand(Query, CN);
cmd.Parameters.Add(new SqlParameter("@Image", img));
cmd.Parameters.Add(new SqlParameter("@Name",txtname.Text));
cmd.Parameters.Add(new SqlParameter("@id",txtid.Text));
cmd.ExecuteNonQuery();
CN.Close();

如果更新困难,只需使用Id字段删除该特定记录,然后再次启动保存查询

如果难以更新,只需使用您的Id字段删除该特定记录,然后再次启动保存查询

代替
insert
使用
UPDATE
查询“更新命令”是什么意思?代替
insert
使用
UPDATE
查询“更新命令”是什么意思?您好,谢谢您的回答。请尝试用您解决问题所遵循的步骤更好地解释您的解决方案。此外,粘贴代码时,请记住使用Ctrl+K或backticks设置格式。您好,谢谢您的回答。请尝试用您解决问题所遵循的步骤更好地解释您的解决方案。此外,粘贴代码时,请记住使用Ctrl+K或反勾号对其进行格式化。
 SqlConnection con = Connectionclass.SQLCONNECTION();
 SqlDataAdapter da = new SqlDataAdapter();
 string query = ("Update Doctor set ID ='" + idtxt.Text + "',Name='" + nametxt.Text + "',Contact='" + contactxt.Text + "',CNIC='" + cnictxt.Text + "',Address='" + addresstxt.Text + "',Qualification='" + qualitxt.Text + "',specialization='" + specialtxt.Text + "',Gender='" + gendertxt.Text + "',DOB='" + dobtxt.Text + "', Fee='"+textBox1.Text+"',Date='" + System.DateTime.Today.ToString("dd-MM-yyyy") + "', Picture= @image  where ID='" + idtxt.Text + "'");
 da.UpdateCommand = new SqlCommand(query, con);
 con.Open();
 da.UpdateCommand.Parameters.Add("image", SqlDbType.VarBinary).Value = binaryphoto;
 int RowsEffected = da.UpdateCommand.ExecuteNonQuery();
 con.Close();