AutoCAD.Net/C#.Net QQ群:193522571 MSSQL中保存图片,读取MSSQL中图片到PB
1.把图片保存到数据库
FileStream fs = new FileStream(@"E:\MyCloud\Files\Shot\00-624.PNG", FileMode.Open, FileAccess.Read); byte[] arrbyte = new byte[fs.Length]; fs.Read(arrbyte, 0, arrbyte.Length); fs.Close(); ConSQL.Connect(ConType.PvErp); using (SqlConnection conn = new SqlConnection(ConSQL.ConnectString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into T_Image(ImageName,Image) values('123',@imgfile)"; SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image); par.Value = arrbyte; cmd.Parameters.Add(par); int t = (int)(cmd.ExecuteNonQuery()); if (t > 0) { Console.WriteLine("插入bai成功"); } conn.Close(); }
2.把数据库中的image读取并放到pb
byte[] MyData = new byte[0]; string sql = "select Image from T_Image where ImageName = '123'"; DataTable dt = ConSQL.GetServerTable(sql); MyData = (byte[])dt.Rows[1][0]; int ArraySize = MyData.GetUpperBound(0); MemoryStream mystream = new MemoryStream(MyData); //用指定的数据流来创建一个image图片 System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true); pbImage.Image = img;