C # read data from Oracle and fill in Excel

Source: Internet
Author: User

the following example connects to the Oracle database (name) in C #, reads data from the table (tablename), and writes data to excel.

string cnstring = "provider = msdaora.1; Data Source = Name;";
cnstring = cnstring + "User ID = username; Password = password ";
try
{< br> oledbconnection Cn = new oledbconnection (cnstring);
CN. open ();
try
{< br> string S = "select * from name. tablename ";
oledbcommand cmd = new oledbcommand (S, CN);
oledbdatareader DR = cmd. executereader ();
excel. application xlapp = new excel. application ();
If (xlapp = NULL) {MessageBox. Show ("can't open excel! "); Return ;}< br> xlapp. application. workbooks. add (true);
int ROW = 2, fieldcount;
fieldcount = dr. fieldcount;
for (INT Col = 0; Col while (dr. read ()
{< br> for (INT Col = 0; Col xlapp. cells [row, Col + 1] = dr. getvalue (COL ). tostring ();
row ++;
}< br> xlapp. visible = true;
xlapp = NULL;
}< br> catch (exception ex) {MessageBox. show (ex. message) ;}< br> finally {CN. close () ;}< BR >}< br> catch (exception ex) {MessageBox. show (ex. message) ;}< BR >}

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.