2016-01-19 87 views
0

我想插入一個圖像到我的SQL數據庫中。我目前有這個代碼,它在圖像應該是輸入一個空值。爲什麼會發生?試圖將圖片插入到sql數據庫中

我在這裏使用了一個簡單的addquestion類。

你可以找到我波紋管使用的代碼:

Imports System.IO 
Imports System.Data.SqlClient 

Public Class addquestion 
    Dim con As New System.Data.Odbc.OdbcConnection("DRIVER={MySQL ODBC 5.2 ANSI Driver};SERVER=localhost;PORT=3306;DATABASE=physicsapp;USER=root;PASSWORD=root;OPTION=3;") 
    Dim rs As Odbc.OdbcDataReader 

Private Sub addquestion_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

End Sub 

Private Sub QuestionButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionButton.Click 
    Dim myFileDlog As New OpenFileDialog 


    myFileDlog.InitialDirectory = "c:/" 


    myFileDlog.Filter = "All Files (*.*)|*.*" & _"|Picture Files (*.png)|*.png" 


    myFileDlog.FilterIndex = 1 


    myFileDlog.RestoreDirectory = True 


    If myFileDlog.ShowDialog() = _ 
     DialogResult.OK Then 
     If Dir(myFileDlog.FileName) <> "" Then 
      MsgBox("File Exists: " & _ 
        myFileDlog.FileName, _ 
        MsgBoxStyle.Information) 
     Else 
      MsgBox("File Not Found", _ 
        MsgBoxStyle.Critical) 
     End If 
    End If 


    qnametextbox.Text = myFileDlog.FileName 
End Sub 

    Dim ans as Char = "B" 
    Dim file As String = qnametextbox.Text 
    Dim question As Image 
    question = Image.FromFile(file) 

    Dim ms As New MemoryStream() 
    question.Save(ms, System.Drawing.Imaging.ImageFormat.Png) 
    Dim data As Byte() = ms.GetBuffer() 

    Dim s As String = "INSERT INTO tests VALUES (@Question ,'" & ans & "', 200)" 

    Dim sql As New Odbc.OdbcCommand(s, con) 
    sql.Parameters.Add("@Question", Odbc.OdbcType.VarBinary).Value = data 
    con.Open() 
    sql.ExecuteNonQuery() 
    con.Close() 

這是表

CREATE TABLE `tests` (
    `Question` blob NOT NULL, 
    `CorrectAnswer` varchar(8) DEFAULT NULL, 
    `QuestionNumber` int(8) NOT NULL AUTO_INCREMENT, 
    KEY `QuestionNumber` (`QuestionNumber`) 
) ENGINE=MyISAM AUTO_INCREMENT=201 DEFAULT CHARSET=utf8 

任何建議,能幫助的定義。

謝謝。

+0

1 - 哪個DBMS你的工作嗎?從你的連接看起來像MYSQL。從問題中刪除sql-server標記。 –

+0

您要插入的表的2-Post SQL定義。 –

+0

你在哪裏定義了'ans' – Nomeaning25

回答

0

無論什麼樣的數據訪問技術,或者你使用的數據庫的,你需要轉換是ImageByte,然後再保存。在檢索時,您將Byte陣列轉換回Image

爲了節省:

Dim connection As New SqlConnection("connection string here") 
Dim command As New SqlCommand("UPDATE MyTable SET Picture = @Picture WHERE ID = 1", connection) 

'Create an Image object.' 
Using picture As Image = Image.FromFile("file path here") 
    'Create an empty stream in memory.' 
    Using stream As New IO.MemoryStream 
     'Fill the stream with the binary data from the Image.' 
     picture.Save(stream, Imaging.ImageFormat.Jpeg) 

     'Get an array of Bytes from the stream and assign to the parameter.' 
     command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer() 
    End Using 
End Using 

connection.Open() 
command.ExecuteNonQuery() 
connection.Close() 
+0

我沒有看到這段代碼和我的原始代碼之間的區別,但我更新了我的代碼以匹配你的錯誤,並得到相同的錯誤命令.ExecuteNonQuery行說:「錯誤[HY000] [MySQL的] [ODBC 5.2(a)驅動程序] [mysqld-5.5.33]列'問題'不能爲空' –

+0

myabe你應該如果可能嘗試更改數據類型longblob – Dandy

+0

圖像即時嘗試添加只有8kb,但無法正常工作時,我將它改爲Longblob無論如何 –

相關問題