This article summarizes how to store pictures into SQL Server in .NET WinForm and .Net WebForm (ASP.NET) and read the displayed method 1. Using ASP.NET to copy the image and store the SQL Server and read from SQL Server. And displayed, upload and deposit the SQLServer database structure CREATE TABLE TEST {ID Identity (1, 1), Fimage Image} Related Store Create Proc UpdateImage (@UPDATEIMAGE) VALUES (@UPDATEIMAGE )
Add the following:
private void Page_Load (object sender, System.EventArgs e) {if {SqlConnection conn = new SqlConnection () conn.ConnectionString = "Data Source = localhost (Page.IsPostBack!); Database = test; User Id = sa; Pwd = sa "; String strsql =" select * from test where id = 2 "; // 假 假 获 获 获 2 2 2 l l l l l l () Response.binaryWrite ((byte []) Reader ["fimage"]); response.end (); reader.close ();}} 3, store the image in WinForm into SQL Server and read from SQL Server and Displayed in PictureBox 1, store the SQLServer database structure and the stored process used, the same 1.1 above, add an OpenFileDialog control in the form, named OfdselectPIC 1.2, add an open file button on the form, add the following Click Event Code: stream ms; Byte [] Picbyte; //ofdselectpic.showdialog (); if (OFDSELECTPIC.SHOWDIALOG () == DialogResult.ok) {== DialogResult.OpenFile ())! = Null) {//Messagebox.show("ok "); picbyte = new byte [ms.length]; ms.positi ON = 0; ms.read (picbyte, 0, convert.toint32 (ms.length)); //messagebox.show ("Reading!");
// Connect the database SQLConnection conn = new sqlConnection (); conn.connectionstring = "data source = localhost; database = test; user ID = sa; pwd = sa";
Sqlcommand cmd = new sqlcommand ("updateImage", conn); cmd.commandtype = commandtype.storedProcedure;
CMD.Parameters.Add ("@ UpdateImage"; cmd.parameters ["@ UpdateImage"]. Value = Picbyte;
CONN.Open (); cmd.executenonquery (); conn.close ();
ms.close ();}}
2, read and display in Picturebox 2.1 Add a Picturebox, add a button named PTBSHOW 2.2, add the following response event: SqlConnection conn = new sqlConnection (); conn.connectionstring = "data source = localhost; database = Test; user ID = SA; PWD = SA "; string strsql =" SELECT FIMAGE FROM TEST WHERE ID = 1 ";
Sqlcommand cmd = new SQLCOMMAND (strsql, conn);
CONN.Open (); sqldataareader reader = cmd.executeRead (); reader.read ();
MemoryStream MS = New MemoryStream ((Byte []) Reader ["Fimage"]);
Image image = image.fromstream (ms, true);
Reader.close (); Conn.Close ();
PTBShow.Image = image;