GDI +将Picturebox保存到ole数据库时发生了一般错误

时间:2014-12-30 01:50:59

标签: vb.net oledb

我正在尝试将图片框保存到ole数据库中。

这是我的代码:

Dim stream As New IO.MemoryStream
PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)
Try
    Dim query As String = "INSERT INTO Guestinfo ([GuestName],[Phone],[Idofguest],[Room],[Arrival],[Checkout],[Address],[IDImage]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "','" & TextBox4.Text & "',(@IDImage))"
    Dim command As New OleDbCommand

    With command
        .CommandText = query
        .Parameters.AddWithValue("@Picture", stream.GetBuffer())
        .Connection = conn
        .ExecuteNonQuery()
    End With
    MsgBox("Saved Successfully!", MsgBoxStyle.Information)
    conn.Close()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

在“GDI +中出现通用错误”中出现此错误:

PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)

顺便说一句,我的列类型是Ole Object。 我希望有人帮助我...

1 个答案:

答案 0 :(得分:0)

此应用程序有一个打开按钮,可以帮助您使用OpenFileDialog打开表单上PictureBox的任何图片文件。您将在禁用的TextBox中看到图片文件的路径。单击更新按钮时,Picture的路径将保存到Access数据库。

按照以下步骤为自己创建一个类似的项目: *创建一个新的Visual Basic.net项目。从新项目对话框中选择Windows窗体应用程序。无论您想要什么,都可以为此应 *使用以下提到的属性创建以下内容:   - 表单 - (名称):示例,文本:FormPictureApplication   - PictureBox - (Name):PictureBox1,SizeMode:StretchImage   - 按钮 - (名称):ButtonUpdate,文本:&更新   - 按钮 - (名称):ButtonOpen,Text:& Open   - TextBox - (Name):TextBoxPictureFilePath,Enabled:False

  • 双击表单,在公共类{...}上面插入以下代码:

    导入System.Data.OleDb 进口System.IO 导入Microsoft.Win32

    双击ButtonOpen并插入以下代码:

    Dim img As String

    Dim myStream As Stream = Nothing 昏暗的openFileDialog1作为新的OpenFileDialog()

    openFileDialog1.InitialDirectory =" c:\" openFileDialog1.Filter =没什么 openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True openFileDialog1.FileName =""

    如果openFileDialog1.ShowDialog()= System.Windows.Forms.DialogResult.OK那么 尝试 myStream = openFileDialog1.OpenFile() 如果(myStream IsNot Nothing)那么

    TextBoxPictureFilePath.Text =""

    img = openFileDialog1.FileName PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)

    TextBoxPictureFilePath.Text = openFileDialog1.FileName

    结束如果 抓住Ex Exception MessageBox.Show("无法从磁盘读取文件。原始错误:"& Ex.Message) 最后 如果(myStream IsNot Nothing)那么 myStream.Close() 万一 结束尝试 结束如果

  • 在方便的位置创建Microsoft Access数据库,并将其命名为Databasemikeoe2003PictureApplication.mdb

  • 创建一个名为Tablemikeoe2003PictureApplication的表,并将以下列添加到其中:

    Id - 数据类型:自动编号 PicturePath - 数据类型:备忘录(因为文件路径有时可能相当长)

  • 双击UpdateButton并插入以下代码:

    尝试 Dim myConnection作为OleDbConnection Dim myCommand As OleDbCommand 将mySQLString调暗为String myConnection =新的OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0;数据源= Databasemikeoe2003PictureApplication.mdb;") myConnection.Open() mySQLString =" INSERT INTO Tablemikeoe2003PictureApplication(PicturePath)VALUES('"& Replace $(TextBoxPictureFilePath.Text,"'","&# 39;'")&"')" myCommand = New OleDbCommand(mySQLString,myConnection) myCommand.ExecuteNonQuery()

    PictureBox1.Image =没什么 TextBoxPictureFilePath.Text =""

    Catch ex As Exception MessageBox.Show(ex.Message&" - "& ex.Source) 结束尝试

  • 运行应用程序,它应该可以正常工作。