谁能给个sql数据库保存图片的例子,最好是C#语言的,最近做一个窗体,需要动态输入个人信息,包括图片,但是图片始终无法保存,所以求高人指点或者给个例子,谢谢了

解决方案 »

  1.   

    C#在SQl中存取图片image
      

  2.   


      byte[] imagebytes = null;
                using (FileStream fs = new FileStream("D:\\12345.jpg", FileMode.Open))
                {
                    imagebytes = new byte[fs.Length];
                    BinaryReader br = new BinaryReader(fs);
                    imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                }
                SqlConnection con = new SqlConnection("Data Source=FCSDL-DT-002\\SQLEXPRESS;Initial Catalog=stuDB;Integrated Security=True");            con.Open();            string sql = "insert into b(ID,zhiwu,picture)values(@ID,@zhiwu,@picture)";
                System.Data.SqlClient.SqlParameter[] para = new System.Data.SqlClient.SqlParameter[] {
                new System.Data.SqlClient.SqlParameter("@ID",12),
                    new System.Data.SqlClient.SqlParameter("@zhiwu","dahui"),
                new System.Data.SqlClient.SqlParameter("@picture",imagebytes)
                };            //string sql = string.Format("insert into b(ID,zhiwu,picture)values({0},'{1}',{2})", 9, "xiaohuilang", imagebytes);            SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(para);
                int resulet = cmd.ExecuteNonQuery();
                con.Close();取
    byte[] imagebytes = null;            SqlConnection con = new SqlConnection("Data Source=FCSDL-DT-002\\SQLEXPRESS;Initial Catalog=stuDB;Integrated Security=True");            con.Open();            string sql = "select * from b where ID=11";            SqlCommand cmd = new SqlCommand(sql, con);            SqlDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    imagebytes = (byte[])read["picture"];
                }
                read.Close();
                con.Close();            MemoryStream ms = new MemoryStream(imagebytes);
                Bitmap bmpt = new Bitmap(ms);
                this.pictureBox1.Image = bmpt;