How to connect Oracle with C #

Source: Internet
Author: User
Try to connect to Oracle and use the oraclecommand to read records in the database, which is displayed on the console: 1 using system;
2 using system. Data;
3 using system. Data. oracleclient;
4
5. Public class mainclass
6 {
7 static void main ()
8 {
9 oracleconnection conn = new oracleconnection ();
10 conn. statechange + = new statechangeeventhandler (onstatechanged );
11 conn. connectionstring = "Data Source = chenchong; user id = system; Password = ;";
12 try
13 {
14 conn. open ();
15 console. writeline ("the database connection is successfully opened. ");
16 oraclecommand command = new oraclecommand ();
17 // command. commandtext = "";
18 command. commandtext = "select nsmc from Scott. NSR where nsmc like: pnsrmc order by nsmc ";
19 command. Connection = conn;
20 oracleparameter parameter = command. Parameters. Add ("pnsrmc", oracletype. varchar );
21 parameter. value = "_ country % ";
22 oracledatareader reader = command. executereader ();
23
24 // verify whether two datareader s can be opened simultaneously
25 oraclecommand command2 = new oraclecommand ();
26 command2.commandtext = "select count (*) from Scott. NSR ";
27 command2.connection = conn;
28 oracledatareader reader2 = command2.executereader ();
29 reader2.read ();
30 console. writeline ("the database has a total of {0} records. ", Reader2.getint32 (0 ));
31 reader2.close ();
32
33 while (reader. Read ())
34 {
35 if (! Reader. isdbnull (reader. getordinal ("nsmc ")))
36 {
37 console. writeline (reader. getstring (reader. getordinal ("nsmc"). Trim ());
38}
39 // string S = NULL;
40 // console. writeline (s );
41}
42 command. commandtext = "select count (nsmc) from Scott. NSR where nsmc like: pnsrmc order by nsmc ";
43 console. writeline ("A total of {0} records meet the condition. ", Command. executescalar ());
44}
45 catch (exception E)
46 {
47 console. writeline ("exception: {0}", E. tostring ());
48}
49 finally
50 {
51 if (Conn! = NULL)
52 {
53 conn. Close ();
54}
55}
56
57 console. Readline ();
58}
59
60 Private Static void onstatechanged (Object sender, statechangeeventargs E)
61 {
62 console. writeline ("the database connection status has changed. The original status is {0} and the current status is {1 }. ", E. originalstate, E. currentstate );
63}
64}

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.