【问题标题】:How to insert, edit, delete image in SQL using vb.net如何使用 vb.net 在 SQL 中插入、编辑、删除图像
【发布时间】:2013-10-29 13:28:25
【问题描述】:

我无法使用此代码进行搜索和编辑。插入正在工作。但是插入编码方法不确定(fileContent.ToString)。搜索部分和更新部分不起作用。

Dim fileContent As Byte() = My.Computer.FileSystem.ReadAllBytes(OpenFileDialog1.FileName)

Sub NormalUpdate(ByVal _Query)
    con.Open()
    Dim cmdUpdate As New SqlCommand(_Query)

    cmdUpdate.Connection = con
    cmdUpdate.CommandType = CommandType.Text
    cmdUpdate.ExecuteNonQuery()
    con.Close()
End Sub

Sub NormalSave(ByVal _Query)
    con.Open()
    Dim cmdSave As New SqlCommand(_Query)

    cmdSave.Connection = con
    cmdSave.CommandType = CommandType.Text
    cmdSave.ExecuteNonQuery()
    con.Close()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    NormalSave("Insert into Registration values('" + txtMemberNo.Text + "','" + fileContent.ToString + "')")
End Sub

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    con.Open()
    Using cmd As New SqlClient.SqlCommand("Select MemberPicture From Registration where MemberNo = '" + txtMemberNo.Text + "'", con)
        Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
            Using dt As New DataTable
                dt.Load(dr)
                Dim row As DataRow = dt.Rows(0)
                Using ms As New IO.MemoryStream(CType(row("MemberPicture"), Byte()))
                    Dim img As Image = Image.FromStream(ms)
                    ProfilePic.Image = img
                    con.Close()
                End Using
            End Using
        End Using
    End Using
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click        
    NormalUpdate("Update Registration set MemberPicture = '" + fileContent.ToString + "' where MemberNo = '" + txtMemberNo.Text + "'")
End Sub

请帮助我。谢谢。

【问题讨论】:

  • 你能把文件存入数据库吗???
  • 是的,您可以保存二进制数据,尽管我不确定它是否可取。您在更新和插入语句中遇到什么样的错误?
  • 我真的建议你阅读一下 Sql Injection
  • Rony - Yep Martao - 搜索错误是“参数无效”。行是 - Dim img As Image = Image.FromStream(ms) 更新错误是不更新数据库(没有编码错误)谢谢大家。!

标签: vb.net image edit sql-insert sql-delete


【解决方案1】:

不确定这是否是您要查找的内容,但我使用以下内容从数据库中获取和设置图像:

Public Function GetClientImage(ID As String) As Image
    Dim ClientPicture As Image = Nothing

    Dim DS As DataSet = Nothing
    'Populate DS here

    If (DS IsNot Nothing) AndAlso (DS.Tables.Count > 0) AndAlso (DS.Tables(0).Rows.Count > 0) Then

        Dim DR As DataRow = DS.Tables(0).Rows(0)

        Try
            Dim Pic As Object = DR!Picture

            If Pic IsNot DBNull.Value Then
                Dim Buffer As Byte() = CType(DR!Picture, Byte())
                If Buffer IsNot Nothing AndAlso (Buffer.Length > 0) Then
                    Using MS As New IO.MemoryStream(Buffer, 0, Buffer.Length)
                        MS.Write(Buffer, 0, Buffer.Length)
                        ClientPicture = Image.FromStream(MS, True)
                    End Using
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Error retrieving Image: " & ControlChars.NewLine & ControlChars.NewLine & ex.ToString, My.Application.Info.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

        End Try

    End If


    Return ClientPicture
End Function

和:

Public Function UpdateClientImage(ID As String, Pic As Image) As Integer
    Dim Result As Integer = 0

    If Client IsNot Nothing Then

        Dim SQL As String = "UPDATE Clients SET Picture = @photo WHERE ID = '" & ID & "' ;"

        Using SQLConn As New SqlClient.SqlConnection(ConnectionString)

            Try
                SQLConn.Open()

                Using SQLCmd As New SqlClient.SqlCommand(SQL, SQLConn)

                    Dim PhotoParameter As New SqlClient.SqlParameter("@photo", SqlDbType.Image)

                    Dim MS As New IO.MemoryStream()
                    If Pic IsNot Nothing Then
                        Pic.Save(MS, Imaging.ImageFormat.Bmp)
                    End If

                    PhotoParameter.SqlValue = MS.GetBuffer
                    SQLCmd.Parameters.Add(PhotoParameter)

                    Result = SQLCmd.ExecuteNonQuery()

                End Using

            Catch ex As Exception
                Dim Msg As String = "Unable to save Client's Picture"

                If ex IsNot Nothing Then
                    Msg &= ":" & ControlChars.NewLine & ControlChars.NewLine & ex.ToString
                End If

                MessageBox.Show(Msg, My.Application.Info.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

            Finally
                If Not SQLConn.State = ConnectionState.Closed Then
                    SQLConn.Close()
                End If

            End Try

        End Using

    End If

    Return Result
End Function

【讨论】:

    【解决方案2】:
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles    
         Dim OpennFileDialog As OpenFileDialog
    
         query = "update tblEmployeeSetup set dbImage = @dbImage where empy_id = '" + txtEmpId.Text + "' "
         insertImage(query, "Data Saved...", lblSave, OpennFileDialog )
    End Sub
    
    Sub insertImage(ByVal query As String, ByVal message As String, ByVal   lblSave As Label, ByVal a As OpenFileDialog)
        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
    
            End If
            '   Dim result As DialogResult = a.ShowDialog()
            '   a.FileName = My.Resources.DefultImage.ToString
    
            cmd.Connection = con
            cmd.CommandText = query
            cmd.Parameters.Add(New SqlClient.SqlParameter("@dbimage", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
    
            cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
    
            lblSave.Text = message
            a.Dispose()
    
        Catch ex As Exception
    
            MessageBox.Show("Error" & ex.Message)
        Finally
            con.Close()
        End Try
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2017-11-07
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多