ADO.NET Database connection method

zhaozj2021-02-16  39

In the MSDN, the .NET's database connection string has a detailed description, I am here with code examples, and the meaning of each representative can see MSDN.

Database connection in ADO.NET (provided by Microsoft)

Microsoft provides the following four database connection methods: system.data.oledb.oledbconnectionsystem.data.sqlclient.sqlconnectionsystem.data.odbc.odbcconnectionsystem.data.OracleClient.OracleConnection Let's explain the way, in order to explain:

Some connection strings (C # code) common to System.Data.SqlClient.SqlConnection (C # code):

Sqlconnection conn = new sqlConnection ("Server = (local); integrated security = sspi; database = pubs");

SqlConnection conn = new sqlconnection ("Server = (local) // netsdk; database = pubs; integrated security = sspi");

SqlConnection conn = new sqlconnection ("data source = localhost; integrated security = sspi; initial catalog = northwind;");

SqlConnection conn = new sqlconnection ("data source = (local); INTEGRATED security = SSPI; persist security info = false; Workstation ID = xr; packet size = 4096;");

SqlConnection myconn = new system.data.sqlclient.sqlconnection ("PERSIST security info = false; integrated security = sspi; database = northwind; server = mysqlserver");

SqlConnection conn = new SQLCONNECTION ("UID = SA; PWD = Passwords; Initial Catalog = Pubs; Data Source = 127.0.0.1; connection timeout = 900");

More string connection notes See msdn: http://msdn.microsoft.com/library/default.asp? URL = / library / en-us / cpref / html / frsrfsystemDataSqlclientsqlConnectionClassConnectionstringtopic.asp

Some connection strings (C # code) commonly used by system.Data.oledb.oledbConnection (C # code):

OLEDBCONNECTION CONN = New OLEDBCONNECTION (@ "provider = microsoft.jet.oledb.4.0; data source = d: /myweb/81/05/grocertogo.mdb");

OleDbConnection conn = new OleDbConnection (@ "Provider = Microsoft.Jet.OLEDB.4.0; Password =; User ID = Admin; Data Source = grocertogo.mdb;"); OleDbConnection conn = new OleDbConnection ( "Provider = MSDAORA; Data Source = Oracle8i7; persist security info = false; integrated security = yes ");

OLEDBCONNECTION CONN = New OLEDBCONNECTION ("provider = microsoft.jet.Oledb.4.0; data source = c: /bin/localaccess40.mdb");

OLEDBCONNECTION CONN = New OLEDBCONNECTION ("provider = sqloledb; data source = mysqlserver; integrated security = SSPI");

More string connection instructions, please see MSDN: http://msdn.microsoft.com/library/default.asp? Url = / library / en-us / cpref / html / fr1rfsystemDataoledboledbconnectionclassconnectionstringtopic.asp? Frame = true

Some connection strings (C # code) common to System.Data.OracleClient.Data.OracleClient.OracleConnection: OracleConnection myconn = new system.data.OracleClient.Orcleconnection ("Data Source = ORACLE8I; Integrated Security = YES");

More string concatenation Description see MSDN: http: //msdn.microsoft.com/library/default.asp url = / library / en-us / cpref / html / frlrfSystemDataOracleClientOracleConnectionClassConnectionStringTopic.asp frame = true??

Some connection strings (C # code) commonly used system.data.odbc.odbcconnection:

OdbcConnection conn = new odbcconnection ("driver = {sql server}; server = myserver; trusted_connection = yes; data = northwind;");

Odbcconnection conn = new odbcconnection ("driver = {microsoft odbc for oracle}; server = Oracle8i7; Persist security info = false; trusted_connection = yes");

OdbcConnection conn = new odbcconnection ("driver = {Microsoft Access Driver (* .mdb)}; dbq = c: /bin/nwind.mdb");

OdbcConnection conn = new OdbcConnection ( "Driver = {Microsoft Excel Driver (* .xls)}; DBQ = c: /bin/book1.xls"); OdbcConnection conn = new OdbcConnection ( "Driver = {Microsoft Text Driver (* .txt ; * .csv)}; DBQ = C: / bin ");

OdbcConnection conn = new odbcconnection ("DSN = DSNNAME");

More string connection instructions, please see msdn: http://msdn.microsoft.com/library/default.asp? URL = / library / en-us / cpref / html / fr1rfsystemDataodBCODBCCCONNECLASSCONNECTIONSTRINGTOPIC.ASP? Frame = true

Database connection provided by other vendors:

DB2Connection myconn = new ibm.data.db2.db2connection ("Database = Sample; UID = ; pwd = ;");

DB2Connection myconn = new ibm.data.db2.db2connection ("Database = Sample");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection ( "assembly = Borland.Data.Mssql, Version = 1.1.0.0, Culture = neutral, PublicKeyToken = 91d62ebb5b0d1b1b; vendorclient = sqloledb.dll; osauthentication = False; database = Username = ; hostname = ; password = ; provider = mssql ");

BdpConnection myConn = new Borland.Data.Provider.BdpConnection ( "assembly = Borland.Data.Db2, Version = 1.1.0.0, Culture = neutral, PublicKeyToken = 91d62ebb5b0d1b1b; vendorclient = db2cli.dll; database = ; username = < User>; password = ; provider = dB2 ");

Connection Pooling

In the Data Provider in SQL Server, OLE DB, and .NET Framework, implicit connection pool connection support is provided. You can specify different parameter values ​​to control the behavior of the connection pool in Connectionstring. For example, the following example makes the OLE DB connecting pool in effect and automatically transaction: provider = sqloledb; OLE DB Services = -4; Data Source = localhost; integrated security = SSPI; below SQL Server.net Data Provider Parameter Settings Control Connection Pool: Connection LiftTime, Connection Reset, Enlist, Max Pool Size, Min Pool Size and Pooling. More database connection information, as well as non-ADO.NET connecting strings can be found: http://www.connectionstrings.COM/

转载请注明原文地址:https://www.9cbs.com/read-28356.html

New Post(0)