在sql中插入和更新映像

时间:2013-07-25 11:23:58

标签: 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;*.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();
}

它有效,但我想知道如何在这里使用更新命令。

4 个答案:

答案 0 :(得分:2)

private void update_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);

        // this is a smaple query for update statement and update where id=@id
        string Query = "update test set name=@name,image=@img where id=@id ";

        CN.Open();
        cmd = new SqlCommand(Query, CN);
        cmd.Parameters.Add(new SqlParameter("@img", img));
        cmd.Parameters.Add(new SqlParameter("@id", txtid.Text));
        cmd.Parameters.Add(new SqlParameter("@name", txtname.Text));
        cmd.ExecuteNonQuery();
        CN.Close();
    }

答案 1 :(得分:0)

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

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();

答案 2 :(得分:0)

如果难以更新,只需使用您的Id字段删除该特定记录并再次触发保存查询。

答案 3 :(得分:0)

 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();