1、c#连接数据库代码集合SQL Server ODBC standard Security:Driver=SQL Server;Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd; trusted connection:Driver=SQL Server;Server=Aron1;Database=pubs;Trusted_Connection=yes; prompt for username and password:oConn.Properties(Prompt) = adPromptAlwaysoConn.Open Driver=SQL Server;
2、Server=Aron1;DataBase=pubs; OLE DB, OleDbConnection (.NET) standard Security:Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd; trusted Connection:Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI; (use serverNameinstanceName as Data
3、Source to use an specifik SQLServer instance, only SQLServer2000)prompt for username and password:oConn.Provider = sqloledboConn.Properties(Prompt) = adPromptAlwaysoConn.Open Data Source=Aron1;Initial Catalog=pubs; connect via an IP address:Provider=sqloledb;Data Source=190.190.200.100,1433;Network
4、Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd; (dbmssocn=tcp/ip instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default)SqlConnection (.NET) standard Security:Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd; - or -Server=Aron
5、1;Database=pubs;User ID=sa;Password=asdasd;Trusted_Connection=False (booth connection strings produces the same result)trusted Connection:Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI; - or -Server=Aron1;Database=pubs;Trusted_Connection=True; (booth connection strings produces the
6、same result)(use serverNameinstanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)connect via an IP address:Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd; (dbmssocn=tcp/ip instead of Named Pipes, at the end of
7、 the Data Source is the port to use (1433 is the default)declare the SqlConnection:C#:using System.Data.SqlClient;SqlConnection oSQLConn = new SqlConnection();oSQLConn.ConnectionString=my connectionstring;oSQLConn.Open(); VB.NET:Imports System.Data.SqlClientDim oSQLConn As SqlConnection = New SqlCon
8、nection()oSQLConn.ConnectionString=my connectionstringoSQLConn.Open() Data Shape ms Data ShapeProvider=MSDataShape;Data Provider=SQLOLEDB;Data Source=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd; want to learn data shaping? Check out 4GuyfFromRollas great article about Data Shaping Read mor
9、e How to define which network protocol to useExample:Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd; Name Network library dbnmpntw Win32 Named Pipes dbmssocn Win32 Winsock TCP/IP dbmsspxn Win32 SPX/IPX dbmsvinn Win32 Banyan
10、 Vines dbmsrpcn Win32 Multi-Protocol (Windows RPC) important note! When connecting through the SQLOLEDB provider use the syntax Network Library=dbmssocnand when connecting through MSDASQL provider use the syntax Network=dbmssocn All SqlConnection connectionstring propertiesThis table shows all conne
11、ctionstring properties for the ADO.NET SqlConnection object. Most of the properties are also used in ADO. All properties and descriptions is from msdn. Name Default Description Application Name The name of the application, or .Net SqlClient Data Provider if no application name is provided. AttachDBF
12、ilename-or-extended properties-or-Initial File Name The name of the primary file, including the full path name, of an attachable database. The database name must be specified with the keyword database. Connect Timeout-or-Connection Timeout 15 The length of time (in seconds) to wait for a connection
13、to the server before terminating the attempt and generating an error. Connection Lifetime 0 When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by connection lifetim
14、e. Useful in clustered configurations to force load balancing between a running server and a server just brought on-line. Connection Reset true Determines whether the database connection is reset when being removed from the pool. Setting to false avoids making an additional server round-trip when ob
15、taining a connection, but the programmer must be aware that the connection state is not being reset. Current Language The SQL Server Language record name. Data Source-or-Server-or-Address-or-Addr-or-Network Address The name or network address of the instance of SQL Server to which to connect. Enlist
16、 true When true, the pooler automatically enlists the connection in the creation threads current transaction context. Initial Catalog-or-Database The name of the database. Integrated Security-or-Trusted_Connection false Whether the connection is to be a secure connection or not. Recognized values ar
17、e true, false, and sspi, which is equivalent to true. Max Pool Size 100 The maximum number of connections allowed in the pool. Min Pool Size 0 The minimum number of connections allowed in the pool. Network Library-or-Net dbmssocn The network library used to establish a connection to an instance of S
18、QL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmsipcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP). The corresponding network DLL must be installed on the system to which you connect. If you do not speci
19、fy a network and you use a local server (for example, . or (local), shared memory is used. Packet Size 8192 Size in bytes of the network packets used to communicate with an instance of SQL Server. Password-or-Pwd The password for the SQL Server account logging on. Persist Security Info false When se
20、t to false, security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Pooling true When true, the SQLConnecti
21、on object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. User ID The SQL Server login account. Workstation ID the local computer name The name of the workstation connecting to SQL Server. note Use ; to separate each property.If a name occurs more t
22、han once, the value from the last one in the connectionstring will be used.If you are building your connectionstring in your app using values from user input fields, make sure the user cant change the connectionstring by inserting an additional property with another value within the user value. Acce
23、ss ODBC standard Security:Driver=Microsoft Access Driver (*.mdb);Dbq=C:mydatabase.mdb;Uid=Admin;Pwd=; workgroup:Driver=Microsoft Access Driver (*.mdb);Dbq=C:mydatabase.mdb;SystemDB=C:mydatabase.mdw; exclusive:Driver=Microsoft Access Driver (*.mdb);Dbq=C:mydatabase.mdb;Exclusive=1;Uid=admin;Pwd= OLE
24、DB, OleDbConnection (.NET) standard security:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=somepathmydb.mdb;User Id=admin;Password=; workgroup (system database):Provider=Microsoft.Jet.OLEDB.4.0;Data Source=somepathmydb.mdb;Jet OLEDB:System Database=system.mdw; with password:Provider=Microsoft.Jet.OLE
25、DB.4.0;Data Source=somepathmydb.mdb;Jet OLEDB:Database Password=MyDbPassword; Oracle ODBC new version:Driver=Microsoft ODBC for Oracle;Server=OracleServer.world;Uid=Username;Pwd=asdasd; old version:Driver=Microsoft ODBC Driver for Oracle;ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword
26、; OLE DB, OleDbConnection (.NET) standard security:Provider=msdaora;Data Source=MyOracleDB;User Id=UserName;Password=asdasd; this ones from Microsoft, the following are from Oraclestandard Security:Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=Username;Password=asdasd; trusted Connection:P
27、rovider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1; OracleConnection (.NET) standard:Data Source=MyOracleDB;Integrated Security=yes; this one works only with Oracle 8i release 3 or laterspecifying username and password:Data Source=MyOracleDB;User Id=username;Password=passwd;Integrated Securi
28、ty=no; this one works only with Oracle 8i release 3 or laterdeclare the OracleConnection:C#:using System.Data.OracleClient;OracleConnection oOracleConn = new OracleConnection();oOracleConn.ConnectionString = my connectionstring;oOracleConn.Open(); VB.NET:Imports System.Data.OracleClientDim oOracleCo
29、nn As OracleConnection = New OracleConnection()oOracleConn.ConnectionString = my connectionstringoOracleConn.Open() missing the System.Data.OracleClient namespace? Download .NET Managed Provider for Oracle great article! Features of Oracle Data Provider for .NET by Rama Mohan G. at C# CornerCore Lab
30、s OraDirect (.NET) standard:User ID=scott; Password=tiger; Host=ora; Pooling=true; Min Pool Size=0;Max Pool Size=100; Connection Lifetime=0 Read more at Core Lab and the product page.Data Shape ms Data Shape:Provider=MSDataShape.1;Persist Security Info=False;Data Provider=MSDAORA;Data Source=orac;us
31、er id=username;password=mypw want to learn data shaping? Check out 4GuyfFromRollas great article about Data Shaping MySQL ODBC odbc 2.50 Local database:Driver=mySQL;Server=localhost;Option=16834;Database=mydatabase; odbc 2.50 Remote database:Driver=mySQL;Server=;Port=3306;Option=131072;Stmt=;Database
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1