none
想请指点有关一次add2张 picture的代码 RRS feed

  • 问题

  • 大家好,我正在尝试篇写一次用两种方式insert image的代码。

    第一种的是同过picture box的,第二种是without picture box的

    以下是我的代码

    我遇到的问题是如果没有照片可以insert数据成功如果有照片就会出现No value given for one or more required parameters. error

     cmd.Parameters.Clear()
            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,Picture1)" &
                      "values (@picture, @F1, @F2, @F3,@picture1)"
                cmd.Parameters.AddWithValue("@picture", DataPic_Update)
            ElseIf picPath IsNot Nothing Then

                Dim DataPic_Update1 As Byte() = ImageToBytes1(Image.FromFile(picPath), ImageFormat.Png)
                cmd.CommandText = "insert into Table1(Picture, F1, F2, F3,Picture1)" &
                      "values (@picture, @F1, @F2, @F3,@picture1)"


                cmd.Parameters.AddWithValue("@picture1", DataPic_Update1)
            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")
            RefreshData()
        End Sub

    Private Function ImageToBytes(ByVal image As Image, ByVal format As ImageFormat) As Byte()

            Using image_stream As MemoryStream = New MemoryStream()
                image.Save(image_stream, format)
                Return image_stream.ToArray()
            End Using

        End Function


    2020年6月5日 5:20

答案

  • Hi christing,

    尝试以下代码:

     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 Or picPath IsNot Nothing Then
    
                If PictureBox1.Image Is Nothing Then
                    cmd.CommandText = "insert into Table1(Picture1,F1, F2, F3)" &
                      " values (@picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update)
    
                ElseIf picPath Is Nothing Then
                    cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                      " values (@picture, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                Else
                    cmd.CommandText = "insert into Table1(Picture, Picture1, F1, F2, F3)" &
                      " values (@picture, @picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    'Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update)
                End If
            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

    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.


    2020年6月5日 9:12
    版主
  • @Xing Yu Zhao

    感谢您耐心的回复。我尝试了您所更新的代码我发现picture box 1 和picPath 就算放不同的照片,也会add到一样的图片。

    比如左边picturebox1 是red apple 右边的picPath 是green apple

    当user add data 成功后,user 得到的result是左图是red apple picpath 也是red apple


    如果,user只是为 picturebox1放red apple 的图那么picture box 1 也只显示red apple的图。

    如果,user 只是为picpath 放 green apple 的图那么picpath 也只会显示green apple的图。

    如果user 为picturebox1 和 picpath 放不一样的图 最终会save到一样的图 

    我尝试了以下代码解决了picturebox1 和 picpath 图片问题以下是我的代码

     Else
                    cmd.CommandText = "insert into Table1(Picture, Picture1, F1, F2, F3)" &
                      " values (@picture, @picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update1)
                End If
            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")
            RefreshData()
        End Sub



    • 已编辑 christing 2020年6月9日 13:04
    • 已标记为答案 christing 2020年6月9日 13:04
    2020年6月9日 11:56

全部回复

  • Hi christing,

    尝试以下代码:

     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 Or picPath IsNot Nothing Then
    
                If PictureBox1.Image Is Nothing Then
                    cmd.CommandText = "insert into Table1(Picture1,F1, F2, F3)" &
                      " values (@picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update)
    
                ElseIf picPath Is Nothing Then
                    cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                      " values (@picture, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                Else
                    cmd.CommandText = "insert into Table1(Picture, Picture1, F1, F2, F3)" &
                      " values (@picture, @picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    'Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update)
                End If
            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

    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.


    2020年6月5日 9:12
    版主
  • @xing yu zhao

    感谢你的回复。我尝试了你的代码,user 无法insert without picturebox的data进database。会拿到Data type mismatch in criteria expression.如果在同一时间add两张照片进database 会拿到Data type mismatch in criteria expression.

    我尝试了将  cmd.CommandText = "insert into Table1(F1, F2, F3,Picture1)" & 改成  cmd.CommandText = "insert into Table1(picture,F1, F2, F3,Picture1)" &

    但还是一样拿到error message (Number of query values and destination fields are not the same.)

    我尝试了以下代码但问题还是一样

     If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then
                    If PictureBox1.Image Is Nothing Then
                        cmd.CommandText = "insert into Table1(picture,F1, F2, F3,Picture1)" &
                          " values (@picture,@F1, @F2, @F3,@picture1)"

                    Dim parameter2 = cmd.Parameters.Add("@picture", CType(SqlDbType.VarBinary, OleDbType), -1)
                    If PictureBox1.ImageLocation Is Nothing Then
                        parameter2.Value = DBNull.Value

    2020年6月5日 15:14
  • Hi christing,

    谢谢你的更新,我检查发现之前的代码中出现了错误,因为 System.Data.OleDb 虽然允许我们使用@XX 作为参数(及其占位符),但是它忽略名称,并将参数顺序严格地视为后续操作的位置。因此,必须按照命令文本中参数顺序相同的顺序声明参数。

    我修改并更新了上面的回答,并且经过测试可以有效的保存值。

    希望可以帮助你解决问题,并且如果有后续的问题请让我知道。

    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.


    2020年6月8日 2:20
    版主
  • @Xing Yu Zhao

    感谢您耐心的回复。我尝试了您所更新的代码我发现picture box 1 和picPath 就算放不同的照片,也会add到一样的图片。

    比如左边picturebox1 是red apple 右边的picPath 是green apple

    当user add data 成功后,user 得到的result是左图是red apple picpath 也是red apple


    如果,user只是为 picturebox1放red apple 的图那么picture box 1 也只显示red apple的图。

    如果,user 只是为picpath 放 green apple 的图那么picpath 也只会显示green apple的图。

    如果user 为picturebox1 和 picpath 放不一样的图 最终会save到一样的图 

    我尝试了以下代码解决了picturebox1 和 picpath 图片问题以下是我的代码

     Else
                    cmd.CommandText = "insert into Table1(Picture, Picture1, F1, F2, F3)" &
                      " values (@picture, @picture1, @F1, @F2, @F3)"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update1)
                End If
            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")
            RefreshData()
        End Sub



    • 已编辑 christing 2020年6月9日 13:04
    • 已标记为答案 christing 2020年6月9日 13:04
    2020年6月9日 11:56