Hi christing,
首先为了确保 'Picture' 允许为空, 需要在Access数据库中将 OLE Object 类型下的 Required 属性改为 No:

然后当 PictureBox 中没有值时,我们可以不插入值:
If PictureBox1.Image IsNot Nothing Then
Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
"values (@picture, @F1, @F2, @F3)"
cmd.Parameters.AddWithValue("@picture", DataPic_Update)
Else
cmd.CommandText = "insert into Table1(F1, F2, F3)" &
"values (@F1, @F2, @F3)"
完整的代码是:
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
If PictureBox1.Image IsNot Nothing Then
Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
"values (@picture, @F1, @F2, @F3)"
cmd.Parameters.AddWithValue("@picture", DataPic_Update)
Else
cmd.CommandText = "insert into Table1(F1, F2, F3)" &
"values (@F1, @F2, @F3)"
End If
cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Record Is Added")
End Sub
考虑到为了正常显示数据库中没有值的 'Picture' 列,请在相应的Show方法(根据你之前的帖子,在 RefreshData 中)插入:
For Each row As DataGridViewRow In DataGridView1.Rows
If DBNull.Value.Equals(row.Cells("Picture").Value) Then
row.Cells("Picture").Value = New Bitmap(1, 1)
End If
Next
希望帮助你解决问题。
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.