. 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 ();