2016-11-30 62 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

相关问题