2023年6月21日发(作者:)

C#将图⽚以⼆进制保存,读取⼆进制转换图⽚⾸先定义数据库连接字符串:String strCn = "server=.;database=DB_NAME;user=sa;pwd=XXX";读取图⽚,将图⽚转换⼆进制保存到数据库⽅法: SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO ProductID_Image (ProductID,ImageData) VALUES ('1',@BLOBData)", cn); String strBLOBFilePath = @"C:/Users/Administrator/Desktop/打印模板.jpeg"; FileStream fsBLOBFile = new FileStream(strBLOBFilePath, , ); Byte[] bytBLOBData = new Byte[]; (bytBLOBData, 0, ); (); SqlParameter prm = new SqlParameter("@BLOBData", ary, , , false, 0, 0, null, t, bytBLOBData); (prm); (); eNonQuery(); ();读取数据库中⼆进制数据,转换成图⽚⽅法: SqlConnection cn = new SqlConnection(strCn); (); SqlCommand cmd = new SqlCommand("select ProductID,ImageData from ProductID_Image", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); (ds, "ProductID_Image"); int c = ["ProductID_Image"].; if (c > 0) { Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])(["ProductID_Image"].Rows[c - 1]["ImageData"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); = ream(stmBLOBData); } ();

2023年6月21日发(作者:)

C#将图⽚以⼆进制保存,读取⼆进制转换图⽚⾸先定义数据库连接字符串:String strCn = "server=.;database=DB_NAME;user=sa;pwd=XXX";读取图⽚,将图⽚转换⼆进制保存到数据库⽅法: SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO ProductID_Image (ProductID,ImageData) VALUES ('1',@BLOBData)", cn); String strBLOBFilePath = @"C:/Users/Administrator/Desktop/打印模板.jpeg"; FileStream fsBLOBFile = new FileStream(strBLOBFilePath, , ); Byte[] bytBLOBData = new Byte[]; (bytBLOBData, 0, ); (); SqlParameter prm = new SqlParameter("@BLOBData", ary, , , false, 0, 0, null, t, bytBLOBData); (prm); (); eNonQuery(); ();读取数据库中⼆进制数据,转换成图⽚⽅法: SqlConnection cn = new SqlConnection(strCn); (); SqlCommand cmd = new SqlCommand("select ProductID,ImageData from ProductID_Image", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); (ds, "ProductID_Image"); int c = ["ProductID_Image"].; if (c > 0) { Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])(["ProductID_Image"].Rows[c - 1]["ImageData"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); = ream(stmBLOBData); } ();