How to save images to the Oracle database (Code tutorial ),

Source: Internet
Author: User
Tags oracleconnection

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.