vb.net中SQL_database中的图像

时间:2016-04-21 15:29:09

标签: vb.net

我有一个问题是将图像记录在vb.net的SQL_database中。作为一个文件没有问题但是我无法用SQL编写.ABCreateNewBarcode是一个PictureBox。我有一个问题是从PictureBox中取BackgroundImage以保存在SQL中。我在BarcodeImg文件夹中保存了BackgroundImage,但我无法保存在SQl

Private Sub btnSaveBarcode_Click(sender As Object, e As EventArgs) Handles btnSaveBarcode.Click
    'Create Image Object
    Dim ABCreateNewBarcode As Object
    ABCreateNewBarcode = CreateObject("BARCODE.BarcodeCtrl.1")
    ABCreateNewBarcode.Text = txtNewBarcode.Text
    ABCreateNewBarcode.typename = "Code128"
    DirBarcodeImg = Application.StartupPath & "\barcodeimg"
    'Save Image
    If txtNewBarcode.Text = "" Then
        MsgBox("Click the Create Button to create a New Barcode")

    ElseIf Directory.Exists(DirBarcodeImg) = False Then
        Call Directory.CreateDirectory(DirBarcodeImg)

    Else
        ABCreateNewBarcode.SaveAsBySize(DirBarcodeImg & "\" & txtInsertPartName.Text & ".png", 300, 130)

        Dim ImageToSave As Image = ABCreateNewBarcode.BackgroundImage

        Dim ms As New MemoryStream
        ImageToSave.Save(ms, ImageToSave.RawFormat)
        Dim buffer As Byte() = MS.GetBuffer()

        'Add SQL Parameters
        SQL.AddParam("@name", txtInsertPartName.Text)
        SQL.AddParam("@image", buffer)

        'Run Imsert Command
        SQL.ExecQuery("INSERT INTRO information (PartName,BarcodeImg) " &
                      "VALUES (@name,@image) ")
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

在你找到请求之后" INTO"而不是" INTRO"试试这一行:

SQL.Parameters.AddWithValue("@image", txtInsertPartName.Text).SqlDbType = SqlDbType.Image

并替换:

Dim ms As New MemoryStream
ImageToSave.Save(ms, ImageToSave.RawFormat)
Dim buffer As Byte() = MS.GetBuffer()

由此:

Dim fs As FileStream
    fs = New FileStream(imagename, FileMode.Open, FileAccess.Read)
    'a byte array to read the image
    Dim picbyte As Byte() = New Byte(fs.Length - 1) {}

    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length))
    fs.Close()
    'open the database using odp.net and insert the data
    Dim buffer As Byte() =picbyte