2013-10-21 44 views
0

我無法使用此代碼進行搜索和編輯。插入正在工作。但插入編碼方法是不能確定(fileContent.ToString)。和搜索部分和更新部分是行不通的。如何使用vb.net在SQL中插入,編輯和刪除圖像

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 

請幫助我。謝謝。

+0

你能否在數據庫中存儲文件? –

+0

是的,你可以保存二進制數據,但我不確定它是否可取。更新和插入語句會出現什麼樣的錯誤? – Martao

+1

我真的建議你閱讀關於Sql Injection – Steve

回答

0

不知道這是你在找什麼,但是我使用以下方法來獲取和從數據庫中設置圖片:

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 
0
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 
相關問題