0

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

4 回答 4

3
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();
    }
于 2013-07-25T11:31:42.363 回答
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();
于 2013-07-25T11:31:32.037 回答
0

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

于 2013-07-25T12:03:42.163 回答
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();
于 2020-03-30T11:23:31.697 回答