[SQL Server] connecting to SQL Server-. net

Source: Internet
Author: User

. Net connection to SQL SERVER: sqlclient, oledb, ODBC

 

1. sqlclient is dedicated to Microsoft's MSSQL, so the sqlclient speed will be a little bit faster.

Connection string: "Server = localhost; database = northwind; uid = sa; Pwd = ;"

Or "Data Source = localhost; Integrated Security = sspi; initial catalog = northwind"

 

System. data. sqlclient. sqlconnection conn = new sqlconnection ("Server = .; database = Master; uid = sa; Pwd =; "); <br/> Conn. open (); <br/> sqlcommand cmd = new sqlcommand ("select * from TB", Conn); <br/> system. data. sqlclient. sqldatareader mydr = cmd. executereader (); <br/> while (mydr. read () // if the last record is not reached <br/>{< br/> response. write (mydr. getstring (0); // 0 indicates the first field, and then 1 <br/> response. write ("<br>"); <br/>}< br/> mydr. close (); <br/> Conn. close (); </P> <p>

2. oledb

Oledbconnection nwindconn = new oledbconnection ("provider = sqloledb; Data Source = localhost; integrated" + <br/> "Security = sspi; initial catalog = northwind "); </P> <p> oledbcommand salescmd = new oledbcommand ("select * from TB", nwindconn); </P> <p> nwindconn. open (); </P> <p> oledbdatareader myreader = salescmd. executereader (); </P> <p> while (myreader. read () <br/>{< br/> console. writeline ("/t {0}, $ {1}", myreader. getstring (0), myreader. getdecimal (1); <br/>}</P> <p> myreader. close (); <br/> nwindconn. close (); <br/>

 

 

 

3. ODBC

 

Odbcconnection nwindconn = new odbcconnection ("driver = {SQL Server}; server = localhost; trusted_connection = yes;" + <br/> "database = northwind "); <br/> nwindconn. open (); </P> <p> odbccommand salescmd = new odbccommand ("select * from TB", nwindconn); </P> <p> odbcdatareader myreader = salescmd. executereader (); <br/> while (myreader. read () <br/>{< br/> console. writeline ("/t {0}, $ {1}", myreader. getstring (0), myreader. getdecimal (1); <br/>}</P> <p> myreader. close (); <br/> nwindconn. close ();

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.