如何用C#中的照片更新数据?

时间:2017-08-21 07:20:34

标签: c# .net winforms sqlclient

如何使用照片更新我的数据而不会出错?我的数据库名称是表格,表格内容是

  • ID
  • 姓,
  • 中间名,
  • 名字,
  • 性别,
  • 地址,
  • 出生日期,
  • PlaceofBirth,
  • FathersName,
  • FathersOccupation,
  • MothersName,
  • MothersOccupation,
  • 监护人,
  • 关系,
  • GuardianOccupation,
  • 图像,

我编辑了一些数据,但是当我更改/编辑它时会出现这种情况;我该怎么解决这个问题?

enter image description here

private void button3_Click(object sender, EventArgs e)
{
    byte[] images = null;
    FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    images = br.ReadBytes((int)fs.Length);

    con.Open();

    string sql = "update table1 set id=@id,FirstName=@FirstName,MiddleName=@MiddleName,LastName=@LastName,Gender=@Gender,DateofBirth=@DateofBirth,PlaceofBirth=@PlaceofBirth,Address=@Address,FathersName=@FathersName,FathersOccupation=@FathersOccupation,MothersName=@MothersName,MothersOccupation=@MothersOccupation,Guardian=@Guardian,Relation=@Relation,GuardianOccupation=@GuardianOccupation,@images=images where id=@id";

    cmd = new SqlCommand(sql, con);
    cmd.Parameters.Add(new SqlParameter("@id", textBox1.Text));
    cmd.Parameters.Add(new SqlParameter("@FirstName", textBox2.Text));
    cmd.Parameters.Add(new SqlParameter("@MiddleName", textBox3.Text));
    cmd.Parameters.Add(new SqlParameter("@LastName", textBox4.Text));
    cmd.Parameters.Add(new SqlParameter("@Gender", comboBox1.Text));
    cmd.Parameters.Add(new SqlParameter("@DateofBirth", dateTimePicker1.Value.ToString()));
    cmd.Parameters.Add(new SqlParameter("@PlaceofBirth", textBox5.Text));
    cmd.Parameters.Add(new SqlParameter("@Address", textBox6.Text));
    cmd.Parameters.Add(new SqlParameter("@FathersName", textBox7.Text));
    cmd.Parameters.Add(new SqlParameter("@FathersOccupation", textBox8.Text));
    cmd.Parameters.Add(new SqlParameter("@MothersName", textBox9.Text));
    cmd.Parameters.Add(new SqlParameter("@MothersOccupation", textBox10.Text));
    cmd.Parameters.Add(new SqlParameter("@Guardian", textBox11.Text));
    cmd.Parameters.Add(new SqlParameter("@Relation", textBox12.Text));
    cmd.Parameters.Add(new SqlParameter("@GuardianOccupation", textBox13.Text));
    cmd.Parameters.Add(new SqlParameter("@images", images));

    int n = cmd.ExecuteNonQuery();
    con.Close();

    viewlist();
    MessageBox.Show(n.ToString() + "Update Successfull");
}

当我没有浏览并显示照片时出现第二个错误:

The second error when I didn't browse and display photo

1 个答案:

答案 0 :(得分:1)

您的更新声明不正确!您正尝试将列分配给参数..(@images=images

string sql = "update table1 set id=@id,FirstName=@FirstName,MiddleName=@MiddleName,LastName=@LastName,Gender=@Gender,DateofBirth=@DateofBirth,PlaceofBirth=@PlaceofBirth,Address=@Address,FathersName=@FathersName,FathersOccupation=@FathersOccupation,MothersName=@MothersName,MothersOccupation=@MothersOccupation,Guardian=@Guardian,Relation=@Relation,GuardianOccupation=@GuardianOccupation,@images=images where id=@id";

试试这个:

string sql = "update table1 set id=@id,FirstName=@FirstName,MiddleName=@MiddleName,LastName=@LastName,Gender=@Gender,DateofBirth=@DateofBirth,PlaceofBirth=@PlaceofBirth,Address=@Address,FathersName=@FathersName,FathersOccupation=@FathersOccupation,MothersName=@MothersName,MothersOccupation=@MothersOccupation,Guardian=@Guardian,Relation=@Relation,GuardianOccupation=@GuardianOccupation,image=@images where id=@id";