C # connecting the datagridview to the oracle database of the server

Source: Internet
Author: User
UsingSystem; usingSystem. collections. generic; usingSystem. componentModel; usingSystem. data; usingSystem. drawing; usingSystem. linq; usingSystem. text; usingSystem. windows. forms; usingSystem. data. oracleClient; usingSystem. data. sqlClient; name

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. data. oracleClient; using System. data. sqlClient; name

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. data. oracleClient; using System. data. sqlClient; namespace TEST {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {// string connstring = "Data Source = ouc131; user = ouc; password = letu;"; // If an oracle client exists locally, use this connection string connstring = "User Id = ouc; Password = letu; Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = 222. 195. *. *) (PORT = 1523) (CONNECT_DATA = (SID = ouc); Unicode = True; "; OracleConnection conn = new OracleConnection (connstring); try {conn. open (); OracleDataAdapter AdapterSelect = new OracleDataAdapter ("select * from text", conn); DataTable dt = new DataTable (); AdapterSelect. fill (dt); dataGridView1.DataSource = dt. defaultView;} catch (Exception ee) {MessageBox. show (ee. message);} finally {conn. close ();}}}}

1. Go to oracle official website to download oracle client instant (http://www.oracle.com/technetwork/topics/winsoft-085727.html unzip the dllbehind to copy to your exefolder. (instantclient-basic-nt-11.2.0.zip supports multi-language, instantclient-basiclite-nt-11.2.0.2.0.zip only supports English, high version of oracle client can link lower version of oracle)

(Or if you install an oracle client on your local machine, you can search for * oci * in the oracle client folder *. dll (oci is an oracle call interface) file. Copy all the searched dll files to your exe folder. Copy the dll files on the Internet)

2. The link string should be written (you can write it into the configuration file, that is, you must write the address, port, instance, user name, and password of the database)

private static string CONNECTION_STRING ="User Id=myUserID;Password=myPassword;Data Source=(DESCRIPTION=" +"(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.server.com)(PORT=yourPort#))" +"(CONNECT_DATA=(SID=yourSID)));";

In this way, you do not need to read the configurations in tnsnames. ora of the installation client.

Note: For programming, you need to put the above dll under your debug or release Directory, which is the same as the directory of the exe file. Otherwise, the file cannot be found and various errors may occur.

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.