C# 将图片保存到数据库

static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=192.168.30.7;database=AIS20170401213620;uid=sa;pwd=ABCabc123";
            //con.ConnectionString = "server=192.168.30.7;database=AIS20170720100743;uid=sa;pwd=ABCabc123";
            con.Open();
            //创建文件流,path参数是文件路径
            FileStream file = new FileStream(@"C:\Users\VULCAN\Desktop\火影桌面壁纸\Cg-4V1RSC-qIb5v4AAwP30B8ANkAAQsPAAtspsADA_3018.jpg", FileMode.Open);
            int streamLength = (int)file.Length;
            byte[] image = new byte[streamLength];//声明字节数组,保存图片文件
            file.Read(image, 0, streamLength); //把图片文件转换成字节数组保存
            file.Close();

            string sql = string.Format(" update t_BOS200000075  set fpicture=@image where fid='1000' ", image);
            //string sql = string.Format("update ");
            SqlCommand com = new SqlCommand(sql, con);
            com.Parameters.Add(new SqlParameter("image", SqlDbType.Binary, image.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, image));

            SqlDataReader dr = com.ExecuteReader();
            dr.Close();
            con.Close();

        }

 

posted @ 2017-08-15 16:04  唐尧  阅读(5732)  评论(0编辑  收藏  举报