Store pictures to Oracle and read image methods from Oracle

xiaoxiao2021-03-06  100

First, deposit pictures into Oracle Database

Example Table News Structure: Newsid Number (10), Title Varchar2 (100), Image (blob)

Method 1: Using the OracleCommandBuilder class (this class is used to automatically generate a single meter command for coordinating DataSet changes and associated databases.)

DIM CN As New OracleConnection ("Data Source = Site; UID = GF; PWD = Macro;") cn.open ()

Dim da As New OracleDataAdapter ( "select * from news", cn) Dim myCB As New OracleCommandBuilder (da) Dim ds As New DataSet ( "news") da.MissingSchemaAction = MissingSchemaAction.AddWithKey Dim fs As Stream = File1.PostedFile.InputStream 'File1 is the file selection box, uses Dim MyData (fs.length) as Byte fs.read (MyData, 0, fs.length) fs.close () DA.FILL (DS, "news") DIM MyRow AS DataRow = ds.tables ("news"). Newrow MyRow ("newsid") = txtnewsid.text myrow ("title") = txttitle.text myrow ("image") = mydata ds.tables ("news"). ROWS .Add (myrow) Da.Update (DS, "News")

cn.close ()

Method 2: Utilization process

The Oracle process defined in advance is:

CREATE OR REPLACE PROCEDURE "GF". "NEWS_ADD" (in_newsid in news.newsid% type, in_title in news.title% type, in_imag in news.image% type) as begin insert into gf.news values ​​(in_newsid, in_title, in_image END;

Below is the code to store data into the database:

DIM CN As New OracleConnection ("Data Source = Site; UID = GF; PWD = Macro;") cn.open ()

Dim cmd As New OracleCommand ( "news_add", cn) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Add (New OracleParameter ( "in_newsid", OracleType.Number, 10)) cmd.Parameters ( "in_newsid"). Value = txtNewsID.Text.Trim cmd.Parameters.Add (New OracleParameter ( "in_title", OracleType.VarChar, 100)) cmd.Parameters ( "in_title"). Value = txtTitle.Text.TrimDim fs As Stream = File1.PostedFile.InputStream DIM MyData (fs.length) as byte 'Defines a byte array for reading the image stream fs.read (mydata, 0, fs.length) fs.close ()

Cmd.Parameters.Add (New OracleParameter ("in_image", oracletype.blob) cmd.parameters ("in_image"). Value = MyData

cmd.executenonquery ()

cn.close ()

Second, read the picture from the Oracle database

Method 1: Read the data from the database and remain on the server in the file

DIM Conn AS New OracleClient.OracleConnection DimleCommand Dim MyReader As OracleClient.OracleDataReader Dim SQL AS STRING DIM FL AS File

SQL = "SELECT * from news where newsid = 3211" Removes picture data from the database blob conn.connectionstring = "password = macro; user id = GF; data source = site" cmd.commandtext = SQL cmd.connection = conn

Conn.open () MyReader = cmd.executeReader (Commandbehavior.SequentialAracse) MyReader.Read ()

Label1.text = MyReader ("Title")

.. Dim fs As FileStream '. Writes the BLOB to a file (* .bmp) Dim bw As BinaryWriter' Streams the binary data to the FileStream object Dim bufferSize As Integer = 1000 'The size of the BLOB buffer Dim outbyte (bufferSize - 1) As byte 'the BLOB byte () buffer to be filled by GetBytes. Dim retval As Long' the bytes returned from GetBytes. Dim startIndex As Long = 0 'the starting position in the BLOB output. Dim fpath As String' pictures Data stored local file fpath = server.mappath (request.applicationPath) & "/image/photo.bmp" fs = new filestream (fipath, filemode.openorcreate, fileaccess.write) bw = new binarywriter (fs)

'Reset the starting byte for a new blob. StartIndex = 0

'Read bytes Into Outbyte () and retain the number of bytes returned. Retval = MyReader.getbytes (2, startIndex, outbyte, 0, buffersize "getBytes returns the number of bytes in the field, the first parameter starts from 0 Column number

'Continue Reading and Writing While The Buffer. Do While Retval = Buffersize Bw.write (Outbyte) BW.FLUSH ()' Clean all buffers of the current writer to write all buffer data to the infrastructure .

'Reposition the start index to the end buffer and finalindex buffersize retval = myreader.getbytes (2, StartIndex, Outbyte, 0, Buffearsize) loop

'Write the remaining buffer. Bw.write (outbyte) BW.FLUSH ()

'Close the output file. Bw.close () fs.close ()

'Close the reader and the connection. MyReader.close () Conn.close ()

Dim logo As Image 'file format conversion logo = Image.FromFile (fpath) fpath = Server.MapPath (Request.ApplicationPath) & "/Image/zkjhy.jpeg" logo.Save (fpath, System.Drawing.Imaging.ImageFormat.Jpeg )

'Me.image1.width = new unit (300)' Adjustment display size 'me.image1.Height = new unit (350) me.image1.imageurl = "image / zkjhy.jpeg"

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

New Post(0)