NET中各种数据库连接大全.docx
《NET中各种数据库连接大全.docx》由会员分享,可在线阅读,更多相关《NET中各种数据库连接大全.docx(8页珍藏版)》请在冰豆网上搜索。
![NET中各种数据库连接大全.docx](https://file1.bdocx.com/fileroot1/2023-1/26/7872f770-ebd1-4517-87f0-79d6ba693033/7872f770-ebd1-4517-87f0-79d6ba6930331.gif)
NET中各种数据库连接大全
.NET中各种数据库连接大全
SQLServer
ODBC
StandardSecurity:
"Driver={SQLServer};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"
Trustedconnection:
"Driver={SQLServer};Server=Aron1;Database=pubs;Trusted_Connection=yes;"
Promptforusernameandpassword:
oConn.Properties("Prompt")=adPromptAlways
oConn.Open"Driver={SQLServer};Server=Aron1;DataBase=pubs;"
OLEDB,OleDbConnection(.NET)
StandardSecurity:
"Provider=sqloledb;DataSource=Aron1;InitialCatalog=pubs;UserId=sa;Password=asdasd;"
TrustedConnection:
"Provider=sqloledb;DataSource=Aron1;InitialCatalog=pubs;IntegratedSecurity=SSPI;"
(useserverName/instanceNameasDataSourcetouseanspecifikSQLServerinstance,onlySQLServer2000)
Promptforusernameandpassword:
oConn.Provider="sqloledb"
oConn.Properties("Prompt")=adPromptAlways
oConn.Open"DataSource=Aron1;InitialCatalog=pubs;"
ConnectviaanIPaddress:
"Provider=sqloledb;DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;InitialCatalog=pubs;UserID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IPinsteadofNamedPipes,attheendoftheDataSourceistheporttouse(1433isthedefault))
SqlConnection(.NET)
StandardSecurity:
"DataSource=Aron1;InitialCatalog=pubs;UserId=sa;Password=asdasd;"
TrustedConnection:
"DataSource=Aron1;InitialCatalog=pubs;IntegratedSecurity=SSPI;"
(useserverName/instanceNameasDataSourcetouseanspecifikSQLServerinstance,onlySQLServer2000)
ConnectviaanIPaddress:
"DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;InitialCatalog=pubs;UserID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IPinsteadofNamedPipes,attheendoftheDataSourceistheporttouse(1433isthedefault))
DeclaretheSqlConnection:
C#:
usingSystem.Data.SqlClient;
SqlConnectionoSQLConn=newSqlConnection();
oSQLConn.ConnectionString="myconnectionstring";
oSQLConn.Open();
VB.NET:
ImportsSystem.Data.SqlClient
DimoSQLConnAsSqlConnection=NewSqlConnection()
oSQLConn.ConnectionString="myconnectionstring"
oSQLConn.Open()
DataShape
MSDataShape
"Provider=MSDataShape;DataProvider=SQLOLEDB;DataSource=Aron1;InitialCatalog=pubs;UserID=sa;Password=asdasd;"
Readmore
Howtodefinewichnetworkprotocoltouse
Example:
"Provider=sqloledb;DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;InitialCatalog=pubs;UserID=sa;Password=asdasd;"
NameNetworklibrary
dbnmpntwWin32NamedPipes
dbmssocnWin32WinsockTCP/IP
dbmsspxnWin32SPX/IPX
dbmsvinnWin32BanyanVines
dbmsrpcnWin32Multi-Protocol(WindowsRPC)
Importantnote!
WhenconnectingthroughtheSQLOLEDBproviderusethesyntaxNetworkLibrary=dbmssocn
andwhenconnectingthroughMSDASQLproviderusethesyntaxNetwork=dbmssocn
AllSqlConnectionconnectionstringproperties
ThistableshowsallconnectionstringpropertiesfortheADO.NETSqlConnectionobject.MostofthepropertiesarealsousedinADO.Allpropertiesanddescriptionsisfrommsdn.
NameDefaultDescription
ApplicationNameThenameoftheapplication,or.NetSqlClientDataProviderifnoapplicationnameisprovided.
AttachDBFilename
-or-
extendedproperties
-or-
InitialFileNameThenameoftheprimaryfile,includingthefullpathname,ofanattachabledatabase.Thedatabasenamemustbespecifiedwiththekeyworddatabase.
ConnectTimeout
-or-
ConnectionTimeout15Thelengthoftime(inseconds)towaitforaconnectiontotheserverbeforeterminatingtheattemptandgeneratinganerror.
ConnectionLifetime0Whenaconnectionisreturnedtothepool,itscreationtimeiscomparedwiththecurrenttime,andtheconnectionisdestroyedifthattimespan(inseconds)exceedsthevaluespecifiedbyconnectionlifetime.Usefulinclusteredconfigurationstoforceloadbalancingbetweenarunningserverandaserverjustbroughton-line.
ConnectionResettrueDetermineswhetherthedatabaseconnectionisresetwhenbeingremovedfromthepool.Settingtofalseavoidsmakinganadditionalserverround-tripwhenobtainingaconnection,buttheprogrammermustbeawarethattheconnectionstateisnotbeingreset.
CurrentLanguageTheSQLServerLanguagerecordname.
DataSource
-or-
Server
-or-
Address
-or-
Addr
-or-
NetworkAddressThenameornetworkaddressoftheinstanceofSQLServertowhichtoconnect.
EnlisttrueWhentrue,thepoolerautomaticallyenliststheconnectioninthecreationthreadscurrenttransactioncontext.
InitialCatalog
-or-
DatabaseThenameofthedatabase.
IntegratedSecurity
-or-
Trusted_ConnectionfalseWhethertheconnectionistobeasecureconnectionornot.Recognizedvaluesaretrue,false,andsspi,whichisequivalenttotrue.
MaxPoolSize100Themaximumnumberofconnectionsallowedinthepool.
MinPoolSize0Theminimumnumberofconnectionsallowedinthepool.
NetworkLibrary
-or-
NetdbmssocnThenetworklibraryusedtoestablishaconnectiontoaninstanceofSQLServer.Supportedvaluesincludedbnmpntw(NamedPipes),dbmsrpcn(Multiprotocol),dbmsadsn(AppleTalk),dbmsgnet(VIA),dbmsipcn(SharedMemory)anddbmsspxn(IPX/SPX),anddbmssocn(TCP/IP).
ThecorrespondingnetworkDLLmustbeinstalledonthesystemtowhichyouconnect.Ifyoudonotspecifyanetworkandyouusealocalserver(forexample,"."or"(local)"),sharedmemoryisused.
PacketSize8192SizeinbytesofthenetworkpacketsusedtocommunicatewithaninstanceofSQLServer.
Password
-or-
PwdThepasswordfortheSQLServeraccountloggingon.
PersistSecurityInfofalseWhensettofalse,security-sensitiveinformation,suchasthepassword,isnotreturnedaspartoftheconnectioniftheconnectionisopenorhaseverbeeninanopenstate.Resettingtheconnectionstringresetsallconnectionstringvaluesincludingthepassword.
PoolingtrueWhentrue,theSQLConnectionobjectisdrawnfromtheappropriatepool,orifnecessary,iscreatedandaddedtotheappropriatepool.
UserIDTheSQLServerloginaccount.
WorkstationIDthelocalcomputernameThenameoftheworkstationconnectingtoSQLServer.
Note
Use;toseparateeachproperty.
Ifanameoccursmorethanonce,thevaluefromthelastoneintheconnectionstringwillbeused.
Ifyouarebuildingyourconnectionstringinyourappusingvaluesfromuserinputfields,makesuretheusercantchangetheconnectionstringbyinsertinganadditionalpropertywithanothervaluewithintheuservalue.
Access
ODBC
StandardSecurity:
"Driver={MicrosoftAccessDriver(*.mdb)};Dbq=/somepath/mydb.mdb;Uid=Admin;Pwd=asdasd;"
Workgroup:
"Driver={MicrosoftAccessDriver(*.mdb)};Dbq=/somepath/mydb.mdb;SystemDB=/somepath/mydb.mdw;","admin",""
OLEDB,OleDbConnection(.NET)
Standardsecurity:
"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=/somepath/mydb.mdb;UserId=admin;Password=asdasd;"
Workgroup(systemdatabase):
"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=/somepath/mydb.mdb;JetOLEDB:
SystemDatabase=system.mdw;","admin",""
Withpassword:
"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=/somepath/mydb.mdb;JetOLEDB:
DatabasePassword=MyDbPassword;","admin",""
Oracle
ODBC
Newversion:
"Driver={MicrosoftODBCforOracle};Server=OracleServer.world;Uid=Username;Pwd=asdasd;"
Oldversion:
"Driver={MicrosoftODBCDriverforOracle};ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;"
OLEDB,OleDbConnection(.NET)
Standardsecurity:
"Provider=msdaora;DataSource=MyOracleDB;UserId=UserName;Password=asdasd;"
ThisonesfromMicrosoft,thefollowingarefromOracle
StandardSecurity:
"Provider=OraOLEDB.Oracle;DataSource=MyOracleDB;UserId=Username;Password=asdasd;"
TrustedConnection:
"Provider=OraOLEDB.Oracle;DataSource=MyOracleDB;OSAuthent=1;"
OracleConnection(.NET)
Standard:
"DataSource=Oracle8i;IntegratedSecurity=yes";
ThisoneworksonlywithOracle8irelease3orlater
DeclaretheOracleConnection:
C#:
usingSystem.Data.OracleClient;
OracleConnectionoOracleConn=newOracleConnection();
oOracleConn.ConnectionString="myconnectionstring";
oOracleConn.Open();
VB.NET:
ImportsSystem.Data.OracleClient
DimoOracleConnAsOracleConnection=NewOracleConnection()
oOracleConn.ConnectionString="myconnectionstring"
oOracleConn.Open()
DataShape
MSDataShape:
"Provider=MSDataShape.1;PersistSecurityInfo=False;DataProvider=MSDAORA;DataSource=orac;userid=username;password=mypw"
MySQL
ODBC
Localdatabase:
"Driver={mySQL};Server=mySrvName;Option=16834;Database=mydatabase;"
Remotedatabase:
"Driver={mySQL};Server=;Port=3306;Option=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;"
OLEDB,OleDbConnection(.NET)
Standard:
"Provider=MySQLProv;DataSource=mydb;UserId=UserName;Password=asdasd;"
MySqlConnection(.NET)
EID:
"DataSource=server;Database=mydb;UserID=username;Password=pwd;CommandLogging=false"
ThisoneisusedwitheInfoDesignsdbProvider,anadd-onto.NET
DeclaretheMySqlConnection:
C#:
usingEID.MySqlClient;
MySqlConnectionoMySqlConn=newMySqlConnection();
oMySqlConn.ConnectionString="myconnectionstring";
oMySqlConn.Open();
VB.NET:
ImportsEID.MySqlClient
DimoMySqlConnAsMySqlConnection=NewMySqlConnection()
oMySqlConn.ConnectionString="myconnectionstring"
oMySqlConn.Open()
IBMDB2
OLEDB,OleDbConnection(.NET)fromms
TCP/IP:
"Provider=DB2OLEDB;NetworkTransportLibrary=TCPIP;NetworkAddress=XXX.XXX.XXX.XXX;InitialCatalog=MyCtlg;PackageCollection=MyPkgCol;DefaultSchema=Schema;UserID=MyUser;Password=MyPW"
APPC:
"Provider=DB2OLEDB;APPCLocalLUAlias=MyAlias;APPCRemoteLUAlias=MyRemote;InitialCatalog=MyCtlg;PackageCollection=MyPkgCol;DefaultSchema=Schema;UserID=MyUser;Password=MyPW"
Sybase
ODBC
Standard:
"Driver={