I. Connecting an Oracle Database
- Install the client: The Oracle database is built on the remote server, the local only needs to install a client, and then under the installation directory (D:\app\fangs\product\12.1.0\client_3\Network\Admin, not necessarily the D disk) Configure Tnsnames.ora to refer to the contents of the sample folder in the same directory. is to specify the connection name, remote Ip,servicename/sid. You can then use the SQL Develper tool to open the database and view all the contents of the database.
- C # programs through the ADO connection: first create the connection string: string connstr = "DATA source=***; password=***; USER id=** ", as well as subsequent ADO operations, can connect to the database.
- Get Data: Reference MSDN MSDN ADO
I. Establishing a database connection, Conn.Open ()
II. Create DataAdapter, which is the connection between the database and the DataSet object
III. Declare and create a DataSet object instance before you can begin loading the data, which may contain several separate tables
Iv. DataAdapter provides fill, with the FillSchema method.
The FillSchema method loads some data table information, column names, constraints, and so on.
A separate DataTable object within the Tables collection in the V. DataSet to provide data
VI. Datatables.row can access each row
Data types in two C # correspond to Oracle database field types
Refer to this article
It is particularly important to note that the type of number (10,1) in the Oracle database can be used in decimal
Three. sql
One needs to be passed in SQL, so here is the syntax for the SQL string.
- string [email protected] "";: @:c# Special String declaration method, called "verbatim string" (verbatim strings), using this symbol will tell the compiler this is a verbatim string, which itself is often used to write the path of the specified file or directory, Because it notifies the compiler to "\" as a literal constant instead of an escape character. It is used here because the SQL statement can be long, and he supports wrapping a string
- arguments to sql:string sql = @ "nsert into A Values (: Id_num): Indicates that this is a variable, which is how the Oracle database is expressed. Other databases use a different marking method, such as SQL Server using @. Note that the parameter name here does not make the Oracle database keyword, otherwise it will be an error, indicating that the parameter name is invalid.
- SQL statement Plus constant: eg. Where a>2, defines 2 as a variable count in class constants. Can be written in where a> ' "+ constants.count + @" ' must pay attention to the space, otherwise may not identify the keyword
NOTE: When writing SQL statements in C #, SQL statements are executed in SQL developer, ensuring that the SQL syntax is not error-free and then written to the program. As mentioned in the ":" tag, SQL developer can be checked out
C # and Oracle database