2016-11-30 130 views
1

我正在用c#asp.net構建一個應用程序。我需要將一些數據插入數據庫。一切工作正常,但我得到插入圖像的問題。無法將參數值從字節[]轉換爲字符串

我的數據庫表:

訂購

OrderID int 
description varchar(50) 
numOfItems int 
material varchar(50) 
image varbinary(max) 

我將數據插入到數據庫代碼

protected void btnAddItem_Click(object sender, EventArgs e) 
    { 
     string filepath = fileUpload.PostedFile.FileName; 
     string filename = Path.GetFileName(filepath); 
     string ext = Path.GetExtension(filename); 
     string contentType = String.Empty; 

     switch (ext) 
     { 
      case ".jpg": 
       contentType = "image/jpg"; 
       break; 
      case ".png": 
       contentType = "image/png"; 
       break; 
      case ".gif": 
       contentType = "image/gif"; 
       break; 
      case ".pdf": 
       contentType = "application/pdf"; 
       break; 
     } 
     if (contentType != String.Empty) 
     { 
      Stream fs = fileUpload.PostedFile.InputStream; 
      BinaryReader br = new BinaryReader(fs); 
      Byte[] bytes = br.ReadBytes((Int32)fs.Length); 

      string kon = ConfigurationManager.ConnectionStrings["mk"].ConnectionString; 

      using (SqlConnection conn = new SqlConnection(kon)) 
      { 
       using (SqlCommand cmd = new SqlCommand("INSERT INTO Order(description, numOfItems, material, image")) 
       { 
        cmd.Connection = conn; 
        cmd.Parameters.AddWithValue("@description", inputTextArea.Text); 
        cmd.Parameters.AddWithValue("@numOfItems", inputTextArea.Text); 
        cmd.Parameters.AddWithValue("@material", inputTextArea.Text); 
        cmd.Parameters.Add("@image", SqlDbType.VarChar).Value = bytes; 
        conn.Open(); 
        cmd.ExecuteNonQuery(); 
        Response.Write("Success!"); 
       } 
      } 
     } 
    } 

當我這樣做,我得到以下錯誤:無法將參數值從Byte []轉換爲字符串。

有什麼建議嗎?

更新 - 新的錯誤

附近有語法錯誤 '形象'。錯誤。有任何想法嗎?

+0

http://stackoverflow.com/questions/5824620/what-sqldbtype-maps-to-varbinarymax –

回答

2

對於參數@image傳遞的值是一個字節數組,但是您指定輸入將爲VarChar將其更改爲Binary。使添加特定參數的聲明將是類似於以下

cmd.Parameters.Add("@image", SqlDbType.Binary).Value = bytes; 

而且你要佔位符添加到您的查詢,這意味着查詢文本應該是這樣的:

"INSERT INTO Order(description, numOfItems, material, image)values(@description, @numOfItems,@material,@image)" 
+1

這樣一個容易和愚蠢的錯誤。謝謝! – aiden87

+0

實際上現在我得到'圖像'附近的語法不正確。錯誤。有任何想法嗎? – aiden87

+0

@ fokz8:您必須在查詢中添加佔位符,檢查我的答案 –

2
cmd.Parameters.Add("@image", SqlDbType.VarChar).Value = bytes; 

您的圖片類型不是VarChar您需要修復該問題。最有可能的是,您需要Binary

+0

實際上現在我得到'圖像'附近的語法錯誤。錯誤。有任何想法嗎? – aiden87

1

更改SqlDbType基於數據庫列類型:

在SQL保存圖像可能的數據類型是:

字符串類型:

數據類型和說明:

二進制(n)的固定寬度的二進制串。最大8,000字節

varbinary可變寬度二進制字符串。最大8,000字節

varbinary(max)可變寬度二進制字符串。最大2GB

圖片可變寬度二進制字符串。最大2GB

cmd.Parameters.Add("@image", SqlDbType.image).Value = bytes; 

// Replace 8000, below, with the correct size of the field 
    cmd.Parameters.Add("@image", SqlDbType.VarBinary, 8000).Value = bytes; 

修改SQL命令到:

SqlCommand("INSERT INTO Order(description, numOfItems, material, image) values (@description,@numOfItems,@material,@image)") 
+0

實際上現在我在'圖像'附近得到錯誤的語法。錯誤。有任何想法嗎? – aiden87

+0

在得到這個不正確的語法錯誤之前你做了什麼修改? –

+0

認爲我的插入語句有問題,因爲斷點獲取過去的圖像部分 – aiden87

相關問題