日期:2014-05-18  浏览次数:20880 次

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

------解决方案--------------------

C# code


  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();