none
想问看如何把insert data 的代码和 update data 的代码运作在同一个button RRS feed

  • 问题

  • 我想将insert data 的代码和update data 的代码运作在同一个button

    以下是我insert data代码

    cmd.Parameters.Clear()

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

            Dim folderP As String = "D:\Picture1"
            If Not Directory.Exists(folderP) Then
                Directory.CreateDirectory(folderP)
            End If

            controltextbox()

            If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then

                If PictureBox1.Image Is Nothing Then
                    Form1.PictureBox1.Image.Save("D:\Picture1\" + txtname2.Text)
                    ''SaveImage(ImageToBytes(Image.FromFile(picPath), ImageFormat.Png), txtname2.Text)
                    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
                    PictureBox1.Image.Save("D:\Picture1\" + txtname1.Text)
                    ''SaveImage(ImageToBytes(PictureBox1.Image, ImageFormat.Png), txtname1.Text)
                    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)"
                    Form1.PictureBox1.Image.Save("D:\Picture1\" + txtname2.Text)
                    PictureBox1.Image.Save("D:\Picture1\" + txtname1.Text)

                    ''SaveImage(ImageToBytes(PictureBox1.Image, ImageFormat.Png), txtname1.Text)
                    ''SaveImage(ImageToBytes(Image.FromFile(picPath), ImageFormat.Png), txtname2.Text)

                    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, F4, F5)" &
                                "values (@F1, @F2, @F3,@F4,@F5)"
            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()

    以下是我的update data的代码 

    cmd.Parameters.Clear()
            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
                    ' Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.CommandText = "UPDATE Table1 SET Picture1 = @picture1, F1 = @F1, F2 = @F2, F3 = @F3 Where ID = " & ID.Text
                    Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture1", DataPic_Update)

                ElseIf picPath Is Nothing Then
                    cmd.CommandText = "UPDATE Table1 SET Picture = @picture, F1 = @F1, F2 = @F2, F3 = @F3 Where ID = " & ID.Text
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@picture", DataPic_Update)
                Else
                    cmd.CommandText = "UPDATE Table1 SET Picture = @picture,Picture1 = @picture1, F1 = @F1, F2 = @F2, F3 = @F3 Where ID = " & ID.Text
                    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
            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()

            RefreshData()
            MsgBox("Update Data Successful", MsgBoxStyle.OkOnly, "Message")
            RefreshData()

    感恩 谢谢
    2020年6月25日 7:00

全部回复

  • 如果将其分割放。代码如下

      Dim myid As String
            myid = Product.Text

            If Product.Text = Nothing Then
                Add_Data()
            Else
                Product.Text = myid
                Update_Data()
            End If

        End Sub

    2020年6月25日 9:46
  • Hi christing,

    如果需要根据不同情况插入或更新数据,可以将插入和更新数据分别放入两个方法中,正如你代码中所提到的 Add_Data() 和 Update_Data() 。

    另外如果还有其他问题,可以提供更多的细节。

    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月29日 6:01
    版主