byte[] 怎么用insert into 语句插入?  
我在做一个图片上传的功能,可是有问题,byte[]类型的图片内容数据在insert into 的时候变成了System.Byte[] 字符串
这是我的源代码
    
     protected int imageSize;
    protected byte[] imageBody;
    protected string SqlCom = "";
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();    protected void Page_Load(object sender, EventArgs e)
    {
        //创建数据库链接对象
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["databasecon"].ToString());
    }    //图片上传
    protected void BT_UpFile_Click(object sender, EventArgs e)
    {
        string imagePath;
        string imageType;
        string imageName;
        if (UpFileControl.PostedFile.FileName != "")
        { 
            //取得选择的图片路径
            imagePath = UpFileControl.PostedFile.FileName;
            //取得选择的图片扩展名
            imageType = imagePath.Substring(imagePath.LastIndexOf(".") + 1);
            //取得选择的图片名
            imageName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);            if ("jpg" != imageType && "gif" != imageType)
            {
                Response.Write("对不起!请您选择JPG或者GIF格式的图片!");
                return;
            }
            else
            { 
                //建立访问客户端上传文件的对象
                HttpPostedFile SendImage = UpFileControl.PostedFile;
                //取得图片的大小
                imageSize = SendImage.ContentLength;
                imageBody = new Byte[imageSize];
                //建立数据流对象
                Stream StreamObject = SendImage.InputStream;
                //把图像数据放到imageBody中,其中0代表数据指针位置,ImageSize代表要读取的流的长度
                StreamObject.Read(imageBody, 0, imageSize);
                SqlCom = "insert into Images(ImageName,Images,ImageSize,imgtype)";
                SqlCom += " VALUES('" + imageName + "','" + imageBody + "','" + imageSize.ToString() + "','" + imageType + "')";
                con.Open();
                cmd = new SqlCommand(SqlCom, con);
                try
                {
                    cmd.ExecuteNonQuery();
                    Response.Write("成功!");
                }
                catch
                {
                    Response.Write("失败!");
                }
                finally
                {
                    con.Close();
                }
            }
        }
    }

解决方案 »

  1.   

    参考下面的方法            byte[] imagebody;
                SqlCommand cmd = new SqlCommand("insert into tbname(col1,col2) values(@col1,@col2)");
                cmd.Parameters.Add("@col1", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@col2",SqlDbType.Image).Value=imagebody;
                cmd.ExecuteNonQuery();
      

  2.   

    Stream imgdatastream = File1.PostedFile.InputStream;
    int imgdatalen = File1.PostedFile.ContentLength;
    string imgtype = File1.PostedFile.ContentType;
    string imgtitle = TextBox1.Text;
    byte[] imgdata = new byte[imgdatalen];
    int n = imgdatastream.Read(imgdata,0,imgdatalen);
    string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand
             ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
             VALUES ( @imgtitle, @imgtype,@imgdata )", connection );SqlParameter paramTitle = new SqlParameter
             ("@imgtitle", SqlDbType.VarChar,50 );paramTitle.Value = imgtitle;
    command.Parameters.Add( paramTitle);SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
    paramData.Value = imgdata;
    command.Parameters.Add( paramData );SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
    paramType.Value = imgtype;
    command.Parameters.Add( paramType );connection.Open();
    int numRowsAffected = command.ExecuteNonQuery();
    connection.Close();