任何人都可以帮我更新/编辑图片框中的某些图片到数据库ms access 2007吗?
我正在使用此代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myCommand As New OleDbCommand
Dim SQL As String
Dim ms As New System.IO.MemoryStream
Dim bmpImage As New Bitmap(PictureBox1.Image)
Try
If Not con.State = ConnectionState.Open Then Open_Koneksi()
SQL = "UPDATE tb_pegawai SET nama=@nama, tgl_lahir=@tgl_lahir, gender=@gender, jabatan=@jabatan, alamat=@alamat, telepon=@telepon, foto=@image WHERE nik=@nik"
myCommand.Parameters.AddWithValue("@nik", TextBox1.Text)
myCommand.Parameters.AddWithValue("@nama", TextBox2.Text)
myCommand.Parameters.AddWithValue("@tgl_lahir", DateTimePicker1.Text)
myCommand.Parameters.AddWithValue("@gender", jk.Text)
myCommand.Parameters.AddWithValue("@jabatan", ComboBox1.Text)
myCommand.Parameters.AddWithValue("@alamat", TextBox4.Text)
myCommand.Parameters.AddWithValue("@telepon", TextBox5.Text)
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
myCommand.Parameters.AddWithValue("@image", bytImage)
MsgBox("Data saved")
myCommand.Connection = con
myCommand.CommandText = SQL
myCommand.ExecuteNonQuery()
Data_Load()
con.Close()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Button1.Enabled = False
Finally
con.Dispose()
End Try
End Sub
当我运行程序时,没有出现错误或任何错误。 但图片不会出现在ms访问
上答案 0 :(得分:0)
Access可以使用参数。我已经无数次地使用它了。我的访问更新查询的基本函数添加如下参数:
Dim parameter As OleDbParameter = New OleDbParameter("@parameterKey", parameterValue)
command.Parameters.Add(parameter)
您还应该事先创建连接。这是我经过充分测试的访问更新功能解决方案。
Function UpdateACCDB(ByRef connectionString As String, ByVal table As String, ByVal ColumnsAndValues As Dictionary(Of String, Object), ByRef whereColumn As String, ByRef whereParameter As Object)
table = "[" + table + "]"
Dim sqlStatement As String = "UPDATE " + table + " SET "
Dim rowsUpdated As Integer = 0
For Each pair As KeyValuePair(Of String, Object) In ColumnsAndValues
sqlStatement += "[" + pair.Key + "]" + "=" + "@" + pair.Key + ","
Next
sqlStatement = sqlStatement.Substring(0, sqlStatement.Length - 1)
sqlStatement += " WHERE " + whereColumn + "=@WHERE" + whereColumn
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(sqlStatement, connection)
''Set parameter for columns to be updated
For Each pair As KeyValuePair(Of String, Object) In ColumnsAndValues
Dim parameter As OleDbParameter = New OleDbParameter("@" + pair.Key, pair.Value)
command.Parameters.Add(parameter)
Next
''Set parameter for where clause column
command.Parameters.Add(New OleDbParameter("@WHERE" + whereColumn, whereParameter))
Try
command.Connection.Open()
rowsUpdated = command.ExecuteNonQuery()
command.Connection.Close()
Catch ex As Exception
Throw ex
Finally
If Not IsNothing(command) AndAlso Not IsNothing(command.Connection) AndAlso command.Connection.State = ConnectionState.Open Then
command.Connection.Close()
End If
End Try
End Using
Return rowsUpdated
End Function
如您所见,columnNames及其值以字典形式提供。