不能直接这么写, 用
cmd.Parameters.Add("@Image", SqlDbType.Binary, bytes.Length).Value = bytes;

解决方案 »

  1.   

    上传图片例子public void Button_Submit(Object o, EventArgs e)
    {
    HttpPostedFile upFile = up_file.PostedFile;
    int iFileLength = upFile.ContentLength;
    try
    {
    if(iFileLength == 0)
    {
    txtMess.Text = "请选择要上传的文件!";
    }
    else
    {
    Byte[] FileByteArray = new Byte[iFileLength];
    Stream StreamObject = upFile.InputStream;
    StreamObject.Read(FileByteArray, 0, iFileLength);
    SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
    string sql = "insert into t_imgs (imgData, type, description, imgSize) values " 
    + "(@Image, @ContentType, @ImageDescription, @ImgSize)";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
    cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
    cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    txtDesc.Text = "";
    txtMess.Text = "OK!你已经成功上传了类型的文件";
    }
    }
    catch(Exception ex)
    {
    txtMess.Text = ex.Message.ToString();
    }
    }