none
想请问如果picture box 没有insert照片可以成功的save其他data进database RRS feed

  • 问题

  •  Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click

                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
            End If

            Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
            If CStr(Me.ID.Tag) & "" = "" Then

                cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                                    "values (@picture, @F1, @F2, @F3)"
                cmd.Connection = cnn
                cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                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")
                RefreshData()
            End If
        End Sub

        Private Function ImageToBytes(ByVal image As Image, ByVal format As ImageFormat) As Byte()
            Try
                Using image_stream As MemoryStream = New MemoryStream()
                    image.Save(image_stream, format)
                    Return image_stream.ToArray()
                End Using
            Catch ex As Exception
            End Try
        End Function

    目前我遇到的问题是当picture box 为空时我无法save到其他data进database。


    2020年5月26日 8:46

答案

  • 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.

    • 已标记为答案 christing 2020年5月28日 3:18
    2020年5月27日 2:14
    版主