数据库连接代码大全.docx

上传人:b****8 文档编号:30674200 上传时间:2023-08-19 格式:DOCX 页数:24 大小:20.68KB
下载 相关 举报
数据库连接代码大全.docx_第1页
第1页 / 共24页
数据库连接代码大全.docx_第2页
第2页 / 共24页
数据库连接代码大全.docx_第3页
第3页 / 共24页
数据库连接代码大全.docx_第4页
第4页 / 共24页
数据库连接代码大全.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

数据库连接代码大全.docx

《数据库连接代码大全.docx》由会员分享,可在线阅读,更多相关《数据库连接代码大全.docx(24页珍藏版)》请在冰豆网上搜索。

数据库连接代码大全.docx

数据库连接代码大全

数据库连接代码大全

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;"

-or-

"Server=Aron1;Database=pubs;UserID=sa;Password=asdasd;Trusted_Connection=False"

(boothconnectionstringsproducesthesameresult)

TrustedConnection:

"DataSource=Aron1;InitialCatalog=pubs;IntegratedSecurity=SSPI;"

-or-

"Server=Aron1;Database=pubs;Trusted_Connection=True;"

(boothconnectionstringsproducesthesameresult)

(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;"

Wanttolearndatashaping?

Checkout4GuyfFromRolla'sgreatarticleaboutDataShaping>>

Readmore

Howtodefinewhichnetworkprotocoltouse

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'.NetSqlClientDataProvider'ifnoapplicationnameisprovided.

AttachDBFilename

-or-

extendedproperties

-or-

InitialFileNameThenameoftheprimaryfile,includingthefullpathname,ofanattachabledatabase.Thedatabasenamemustbespecifiedwiththekeyword'database'.

ConnectTimeout

-or-

ConnectionTimeout15Thelengthoftime(inseconds)towaitforaconnectiontotheserverbeforeterminatingtheattemptandgeneratinganerror.

ConnectionLifetime0Whenaconnectionisreturnedtothepool,itscreationtimeiscomparedwiththecurrenttime,andtheconnectionisdestroyedifthattimespan(inseconds)exceedsthevaluespecifiedbyconnectionlifetime.Usefulinclusteredconfigurationstoforceloadbalancingbetweenarunningserverandaserverjustbroughton-line.

ConnectionReset'true'Determineswhetherthedatabaseconnectionisresetwhenbeingremovedfromthepool.Settingto'false'avoidsmakinganadditionalserverround-tripwhenobtainingaconnection,buttheprogrammermustbeawarethattheconnectionstateisnotbeingreset.

CurrentLanguageTheSQLServerLanguagerecordname.

DataSource

-or-

Server

-or-

Address

-or-

Addr

-or-

NetworkAddressThenameornetworkaddressoftheinstanceofSQLServertowhichtoconnect.

Enlist'true'Whentrue,thepoolerautomaticallyenliststheconnectioninthecreationthread'scurrenttransactioncontext.

InitialCatalog

-or-

DatabaseThenameofthedatabase.

IntegratedSecurity

-or-

Trusted_Connection'false'Whethertheconnectionistobeasecureconnectionornot.Recognizedvaluesare'true','false',and'sspi',whichisequivalentto'true'.

MaxPoolSize100Themaximumnumberofconnectionsallowedinthepool.

MinPoolSize0Theminimumnumberofconnectionsallowedinthepool.

NetworkLibrary

-or-

Net'dbmssocn'ThenetworklibraryusedtoestablishaconnectiontoaninstanceofSQLServer.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.

PersistSecurityInfo'false'Whensetto'false',security-sensitiveinformation,suchasthepassword,isnotreturnedaspartoftheconnectioniftheconnectionisopenorhaseverbeeninanopenstate.Resettingtheconnectionstringresetsallconnectionstringvaluesincludingthepassword.

Pooling'true'Whentrue,theSQLConnectionobjectisdrawnfromtheappropriatepool,orifnecessary,iscreatedandaddedtotheappropriatepool.

UserIDTheSQLServerloginaccount.

WorkstationIDthelocalcomputernameThenameoftheworkstationconnectingtoSQLServer.

Note

Use;toseparateeachproperty.

Ifanameoccursmorethanonce,thevaluefromthelastoneintheconnectionstringwillbeused.

Ifyouarebuildingyourconnectionstringinyourappusingvaluesfromuserinputfields,makesuretheusercan'tchangetheconnectionstringbyinsertinganadditionalpropertywithanothervaluewithintheuservalue.

Access

ODBC

StandardSecurity:

"Driver={MicrosoftAccessDriver(*.mdb)};Dbq=C:

\mydatabase.mdb;Uid=Admin;Pwd=;"

Workgroup:

"Driver={MicrosoftAccessDriver(*.mdb)};Dbq=C:

\mydatabase.mdb;SystemDB=C:

\mydatabase.mdw;"

Exclusive:

"Driver={MicrosoftAccessDriver(*.mdb)};Dbq=C:

\mydatabase.mdb;Exclusive=1;Uid=admin;Pwd="

OLEDB,OleDbConnection(.NET)

Standardsecurity:

"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;UserId=admin;Password=;"

Workgroup(systemdatabase):

"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;JetOLEDB:

SystemDatabase=system.mdw;"

Withpassword:

"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;JetOLEDB:

DatabasePassword=MyDbPassword;"

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;"

Thisone'sfromMicrosoft,thefollowingarefromOracle

StandardSecurity:

"Provider=OraOLEDB.Oracle;DataSource=MyOracleDB;UserId=Username;Password=asdasd;"

TrustedConnection:

"Provider=OraOLEDB.Oracle;DataSource=MyOracleDB;OSAuthent=1;"

OracleConnection(.NET)

Standard:

"DataSource=MyOracleDB;IntegratedSecurity=yes;"

ThisoneworksonlywithOracle8irelease3orlater

Specifyingusernameandpassword:

"DataSource=MyOracleDB;UserId=username;Password=passwd;IntegratedSecurity=no;"

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

MissingtheSystem.Data.OracleClientnamespace?

Download.NETManagedProviderforOracle»

Greatarticle!

"FeaturesofOracleDataProviderfor.NET"byRamaMohanG.atC#Corner

CoreLabsOraDirect(.NET)

Standard:

"UserID=scott;Password=tiger;Host=ora;Pooling=true;MinPoolSize=0;MaxPoolSize=100;ConnectionLifetime=0"

ReadmoreatCoreLabandtheproductpage.

DataShape

MSDataShape:

"Provider=MSDataShape.1;PersistSecurityInfo=False;DataProvider=MSDAORA;DataSource=orac;userid=username;password=mypw"

Wanttolearndatashaping?

Checkout4GuyfFromRolla'sgreatarticleaboutDataShaping>>

MySQL

ODBC

ODBC2.50Localdatabase:

"Driver={mySQL};Server=localhost;Option=16834;Database=mydatabase;"

ODBC2.50Remo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1