How to save images to the Oracle database (Code tutorial ),
1. Save images to the Oracle database
The structure of the example Table NEWS is: newsid number (10), title varchar2 (100), image (blob)
Method 1: Use the OracleCommandBuilder class (this class is used to automatically generate single table commands used to coordinate 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. As a server control, use 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 ")
Method 2: exploitation process
The pre-defined Oracle process 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;
The following code stores 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. trim Dim fs As Stream = File1.PostedFile. inputStream Dim mydata (fs. length) As Byte 'defines a Byte array used to read 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 ()