ODBC, OLE DB connections in C #

Source: Internet
Author: User

Original: ODBC, OLE DB connection in C #

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Data.Odbc;usingSystem.Data.OleDb;namespaceodbctest{classProgram {Static voidMain (string[] args) {Console.WriteLine ("################################# #ODBC连接1 #############################"); using(OdbcConnection conn =NewOdbcConnection ("Driver={sql Server}; server=127.0.0.1; Uid=sa; Pwd=abc123123;database=booking") {Conn.                Open (); OdbcCommand cmd=Conn.                CreateCommand (); Cmd.commandtext="SELECT * from TEST"; using(OdbcDataReader reader =cmd. ExecuteReader ()) {Console.WriteLine ("number of fields: \t{0}", reader.                    FieldCount); Console.WriteLine ("============== data Content ===========");  while(reader. Read ()) {Console.WriteLine ("ID: {0}\ttitle: {1}", reader. GetInt32 (0). ToString (), reader. GetString (1)); }}} Console.WriteLine ("############################### #ODBC连接2 ###############################"); stringConstring ="dsn=ggyy1;"+"Uid=sa;"+"pwd=abc123123;";//DSN represents the user data source name created in ODBCOdbcConnection con=NewOdbcConnection (constring); stringsql ="SELECT * from TEST";//stuinfo A table in a database that is bound to a user data sourcecon.            Open (); OdbcCommand com=Newodbccommand (sql, con); using(OdbcDataReader reader =com. ExecuteReader ()) {Console.WriteLine ("number of fields: \t{0}", reader.                FieldCount); Console.WriteLine ("============== data Content ===========");  while(reader. Read ()) {Console.WriteLine ("ID: {0}\ttitle: {1}", reader. GetInt32 (0). ToString (), reader. GetString (1));            }} Console.WriteLine (); Console.WriteLine ("################################# #OLEDB连接 #############################"); stringConString1 =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb";//connect to an Access database            stringSQL1 ="Select COUNT (*) from TEST"; OleDbConnection Con1=NewOleDbConnection (CONSTRING1); Con1.            Open (); OleDbCommand COM1=NewOleDbCommand (SQL1, Con1); inti =Convert.ToInt32 (COM1.            ExecuteScalar ());            Console.WriteLine (i);        Console.read (); }    }}

ODBC, OLE DB connections in C #

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.