您好,有人可以帮助我如何在ms access和vb.net中保存图像吗?它一直说标准表达式中的数据类型不匹配,顺便说一下,这是我的源代码。
Dim ms As New System.IO.MemoryStream
Dim bmpImage As New Bitmap(PHOTOPictureBox.Image)
Try
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
strsql = "insert into request (FIRSTNAME, MIDDLENAME, LASTNAME, QLFR, ALIAS, DATEOFBIRTH, AGE, PLACEOFBIRTH, BARANGAY, TOWN, PROVINCE, GENDER, CIVILSTATUS, CITIZENSHIP, CONTACTNUMBER, PHOTO)values(@a0,@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10,@a11,@a12,@a13,@14,@a15)"
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acscmd.Parameters.AddWithValue("@a0", FIRSTNAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a1", MIDDLENAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a2", LASTNAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a3", QLFRComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a4", ALIASTextBox.Text)
acscmd.Parameters.AddWithValue("@a5", DATEOFBIRTHDateTimePicker.Text)
acscmd.Parameters.AddWithValue("@a6", AGETextBox.Text)
acscmd.Parameters.AddWithValue("@a7", PLACEOFBIRTHTextBox.Text)
acscmd.Parameters.AddWithValue("@a8", BARANGAYComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a9", TOWNTextBox.Text)
acscmd.Parameters.AddWithValue("@a10", PROVINCETextBox.Text)
acscmd.Parameters.AddWithValue("@a11", GENDERComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a12", CIVILSTATUSComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a13", CITIZENSHIPTextBox.Text)
acscmd.Parameters.AddWithValue("@a14", CONTACTNUMBERTextBox.Text)
acscmd.Parameters.AddWithValue("@a15", bytImage)
acscmd.ExecuteNonQuery()
acscmd.Dispose()
MsgBox("ADDED")
print.Show()
答案 0 :(得分:0)
分配第14个参数时出现小错误。您错过了其中的a
。由于您在使用第15个参数时遇到了问题,因此它可能会或可能不会有所帮助。
请确保您已声明bytImage如下
Dim bytImage() As Byte
Dim ms As New System.IO.MemoryStream
Dim bmpImage As New Bitmap(PHOTOPictureBox.Image)
Try
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytImage = ms.ToArray()
ms.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
strsql = "insert into request (FIRSTNAME, MIDDLENAME, LASTNAME, QLFR, ALIAS, DATEOFBIRTH, AGE, PLACEOFBIRTH, BARANGAY, TOWN, PROVINCE, GENDER, CIVILSTATUS, CITIZENSHIP, CONTACTNUMBER, PHOTO)values(@a0,@a1,@a2,@a3,@a4,@a5,@a6,@a7,@a8,@a9,@a10,@a11,@a12,@a13,@a14,@a15)"
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acscmd.Parameters.AddWithValue("@a0", FIRSTNAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a1", MIDDLENAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a2", LASTNAMETextBox.Text)
acscmd.Parameters.AddWithValue("@a3", QLFRComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a4", ALIASTextBox.Text)
acscmd.Parameters.AddWithValue("@a5", DATEOFBIRTHDateTimePicker.Text)
acscmd.Parameters.AddWithValue("@a6", AGETextBox.Text)
acscmd.Parameters.AddWithValue("@a7", PLACEOFBIRTHTextBox.Text)
acscmd.Parameters.AddWithValue("@a8", BARANGAYComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a9", TOWNTextBox.Text)
acscmd.Parameters.AddWithValue("@a10", PROVINCETextBox.Text)
acscmd.Parameters.AddWithValue("@a11", GENDERComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a12", CIVILSTATUSComboBox.SelectedItem)
acscmd.Parameters.AddWithValue("@a13", CITIZENSHIPTextBox.Text)
acscmd.Parameters.AddWithValue("@a14", CONTACTNUMBERTextBox.Text)
acscmd.Parameters.AddWithValue("@a15", bytImage)
acscmd.ExecuteNonQuery()
acscmd.Dispose()
MsgBox("ADDED")
print.Show()
答案 1 :(得分:0)
我看不到您的代码有任何问题,但我只是向现有的测试MDB数据库中添加了一个OLE对象列,然后运行了下面的代码,并且记录没有问题:
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using ofd As New OpenFileDialog
If ofd.ShowDialog() = DialogResult.OK Then
PictureBox1.ImageLocation = ofd.FileName
End If
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim pictureData As Byte()
Using ms As New MemoryStream
PictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg)
pictureData = ms.ToArray()
End Using
Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""|DataDirectory|\Database2.mdb"";Persist Security Info=True"),
command As New OleDbCommand("INSERT INTO Person (FullName, DateOfBirth, Picture) VALUES (@FullName, @DateOfBirth, @Picture)",
connection)
With command.Parameters
.AddWithValue("@FullName", TextBox1.Text)
.AddWithValue("@DateOfBirth", DateTimePicker1.Value.Date)
.AddWithValue("@Picture", pictureData)
End With
connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
End Class
我建议重写代码并遵循我的格式,然后看看问题是否消失。