【发布时间】:2021-08-09 23:38:15
【问题描述】:
我现在完全糊涂了。善良的女士们,善良的先生们;请帮忙!
这就是我正在做的事情:
我有一个 WinForms 应用程序连接到一个带有 varbinary(max) 列用于存储图片的 SQL 数据库。 在我的父表单 (form1) 中,我有一个 DataGridView (dgv1)。双击任意一行会弹出一个新表单 (form2),我可以在其中编辑行数据,其中还有一个 PictureBox (pb1)。
如果我添加图片,然后返回并刷新 DataGridView,并尝试打开同一行,它会给我这个错误消息:
如果我进入数据库并将 varbinary(max) 列的值设置回 NULL(来自 ),并尝试再次打开同一行,它会正常工作。
让我给你看我的代码:
// this runs when I double click a row in dgv1
ChildForm myChildForm = new ChildForm();
myChildForm.txt1.Text = dgv1.CurrentRow.Cells[0].Value.ToString();
myChildForm.txt2.Text = dgv1.CurrentRow.Cells[1].Value.ToString();
myChildForm.txt3.Text = dgv1.CurrentRow.Cells[2].Value.ToString();
if (DBNull.Value != dgv1.CurrentRow.Cells[3].Value) {
MemoryStream mStream = new MemoryStream((byte[])dgv1.CurrentRow.Cells[3].Value);
myChildForm.pb1.Image = Image.FromStream(mStream);
myChildForm.ShowDialog();
// this is on a button, and how I send the edited info from *form2* to the database
byte[] imageData;
using (SqlConnection sqlCon = new SqlConnection(connectionString))
using (SqlCommand sqlCmd = new SqlCommand())
using (MemoryStream mStream = new MemoryStream()) {
sqlCon.Open();
sqlCmd.Connection = sqlCon;
sqlCmd.Parameters.AddWithValue("@Parameter1", txt1.Text.ToString());
sqlCmd.Parameters.AddWithValue("@Parameter2", txt2.Text.ToString());
sqlCmd.Parameters.AddWithValue("@Parameter2", txt2.Text.ToString());
imageData = mStream.ToArray();
sqlCmd.Parameters.AddWithValue("@Parameter3", SqlDbType.VarBinary).Value = imageData;
pb1.Image.Save(mStream, ImageFormat.Jpeg);
SqlCmd.ExecuteNonQuery();
如果有人能给我任何关于我做错了什么的提示,我将不胜感激!谢谢!
【问题讨论】:
-
你为什么要插入一个空数组?
标签: c# sql-server datagridview picturebox