Picture access technology in .NET in SQL Server

zhaozj2021-02-16  58

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: Click Event Processing Code for the Back Code File Upoto.aspx.cs: Private Void Btnadd_Click (Object Sender, System.EventArgs E) {// Get image and the image is converted to byte [] HttpPostedFile upPhoto = UpPhoto.PostedFile; int upPhotoLength = upPhoto.ContentLength; byte [] PhotoArray = new byte [upPhotoLength]; Stream PhotoStream = upPhoto.InputStream; PhotoStream.Read (PhotoArray, 0 , upPhotoLength); // connect to the database SqlConnection conn = new SqlConnection (); conn.ConnectionString = "Data Source = localhost; database = test; User Id = sa; Pwd = sa"; SqlCommand cmd = new SqlCommand ( "UpdateImage", CMD.CommandType = commandType.StoredProcedure; cmd.parameters.add ("@ updateImage"; cmd.Parameters ["@ updateImage"; // If you want to use store procedures To add a picture to change the above four code to: // string strsql = "Insert Into Test (FIMA GE) VALUES (@fimage) "; // sqlcommand cmd = new sqlcommand (strsql, conn); //cmd.parameters.add ("@fimage", sqldbtype.image); //cmd.parameters["@fimage" ] .Value = photoarray; conn (); cmd.executenonQuery (); conn.close ();} II. Read from SQL Server and displayed in place where you need to display pictures: showphoto.aspx body Ä code:

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;

转载请注明原文地址:https://www.9cbs.com/read-21696.html

New Post(0)