怎么讲图片用二进制传到SQLSERVER,然后在窗体显示SQL中的二进制图片

解决方案 »

  1.   

    如果一定要保存到SQL Server中,那么它是普通的BLOB类型数据,在内存中也就是byte[]。请查找有关内存中的数据跟图片的对应关系。请打好基础。
      

  2.   


    http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&tbo=d&q=%22sql+server%22+%E8%AF%BB%E5%86%99blob&btnG=Google+%E6%90%9C%E7%B4%A2http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&tbo=d&q=asp.net+%E8%BE%93%E5%87%BA%E5%9B%BE%E7%89%87&btnG=Google+%E6%90%9C%E7%B4%A2
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    namespace PassMan
    {
        public partial class Image : Form
        {
            public Image()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif|*png|*.png";
                   if (openFileDialog1.ShowDialog() == DialogResult.OK)
                   {
                       string fullpath = openFileDialog1.FileName;
                       FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
                       byte[] imagebytes = new byte[fs.Length];
                       BinaryReader br = new BinaryReader(fs);
                       imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//把所选图片文件的流中的数据读入字节数组                    SqlConnection conn = new SqlConnection("server=192.168.51.215;database=PassGL;uid=sa;pwd=123");
                       conn.Open();
                       SqlCommand cmd = new SqlCommand("insert into photos(photoid,photoimg,photoname) values(@id,@Image,@name)", conn);
                       cmd.Parameters.Add("@id", SqlDbType.VarChar, 50);
                       cmd.Parameters.Add("@Image", SqlDbType.Image);
                       cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);
                       cmd.Parameters["@id"].Value = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
                       cmd.Parameters["@Image"].Value = imagebytes;
                       cmd.Parameters["@name"].Value = comboBox1.Text.ToString();
                       cmd.ExecuteNonQuery();
                       conn.Close();
                       MessageBox.Show("图片上传成功");
                   }
            }        private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                try
                {
                    using (SqlConnection conn = new SqlConnection("server=192.168.51.215;database=PassGL;uid=sa;pwd=123"))
                    {
                        SqlDataAdapter da = new SqlDataAdapter("SELECT photoimg FROM photos WHERE photoname='"+comboBox1.Text+"'", conn);
                        da.Fill(dt);
                    }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message);
                }
                byte[] imagedata = (byte[])(dt.Rows[0][0]);
                MemoryStream myStream = new MemoryStream();
                foreach (byte a in imagedata)
                {
                    myStream.WriteByte(a);
                }
                Image myImage = Image.FromStream(myStream);
                myStream.Close();
                this.pictureBox1.Image = myImage;
                this.pictureBox1.Refresh();
            }  
        }
    }错误 1 “PassMan.Image”并不包含“FromStream”的定义 E:\项目\密码工具\PassMan\Image.cs 69 35 PassMan
    错误 2 无法将类型“PassMan.Image”隐式转换为“System.Drawing.Image” E:\项目\密码工具\PassMan\Image.cs 71 38 PassMan请前辈帮忙看看什么原因,DLL的我都加载了,还提示这个错误!
      

  4.   

    看到发布会、有说SQL Server 2012版本可以直接存图片
    是真的么