2008年5月9日星期五

最全的数据库连接串代码

最全的数据库连接串代码

经常编写数据库程序,那么长长的串真不好记啊,这个东西应该是你需要的。
请看下文:



Overview
Generally, one of the first steps when you are trying to work with databases is open it. You can find several types of those, and each have a different mode of connection. When you try to connect with your database sometimes, you don't know the correct connection string that you must use. It is for that I wrote this article. I wanted to compile the connection strings to the majority of known databases...

ODBC DSN Less Connection
ODBC Driver for dBASE
strConnection = _T("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;"
                  "Dbq=c:\\DatabasePath;");
Note: You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.dbf");
ODBC Driver for Excel
strConnection = _T("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;"
   bq=C:\\DatabasePath\\DBSpreadSheet.xls;DefaultDir=c:\\databasepath;");
ODBC Driver for Text
strConnection = _T("Driver={Microsoft Text Driver (*.txt; *.csv)};"
       "Dbq=C:\\DatabasePath\\;Extensions=asc,csv,tab,txt;");
If you are using tab delimited files, you must create the schema.ini file, and you must inform the Format=TabDelimited option in your connection string.

Note: You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.csv");
Visual FoxPro
If you are using a database container, the connection string is the following:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
   ourceType=DBC;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");
If you are working without a database container, you must change the SourceType parameter by DBF as in the following connection string:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
   "SourceType=DBF;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");
ODBC Driver for Access
strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
       "Dbq=c:\\DatabasePath\\dbaccess.mdb;Uid=;Pwd=;");
If you are using a Workgroup (System database): you need to inform the SystemDB Path, the User Name and its password. For that, you have two solutions: inform the user and password in the connection string or in the moment of the open operation. For example:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
       "Dbq=C:\\VC Projects\\ADO\\Samples\\AdoTest\\dbTestSecurity.mdb;"
       "SystemDB=C:\\Program Files\\Microsoft Office\\Office\\SYSTEM.mdw;"
       "Uid=Carlos Antollini;Pwd=carlos");
or may be:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
       "Dbq=C:\\VC Projects\\ADO\\Samples\\AdoTest\\dbTestSecurity.mdb;"
       "SystemDB=C:\\Program Files\\Microsoft Office\\Office\\SYSTEM.mdw;");
if(pDB.Open(strConnection, "DatabaseUser", "DatabasePass"))
{
   DoSomething();
   pDB.Close();
}
If you want to open in Exclusive mode:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
       "Dbq=c:\\DatabasePath\dbaccess.mdb;Exclusive=1;");
ODBC Driver for SQL Server
For Standard security:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
       "Trusted_Connection=no;"
       "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");
For Trusted Connection security (Microsoft Windows NT integrated security):

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
    "Database=myDatabaseName;Uid=;Pwd=;");
Also, you can use the parameter Trusted_Connection that indicates that you are using the Microsoft Windows NT Authentication Mode to authorize user access to the SQL Server database. For example:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
   "Database=MyDatabaseName;Trusted_Connection=yes;");
If the SQL Server is running in the same computer, you can replace the name of the server by the word (local) like in the following sample:

strConnection = _T("Driver={SQL Server};Server=(local);"
       "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");
If you want to connect with a remote SQL Server, you must inform the address, the port, and the Network Library to use:

The Address parameter must be an IP address and must include the port. The Network parameter can be one of the following:

dbnmpntw Win32 Named Pipes
dbmssocn Win32 Winsock TCP/IP
dbmsspxn Win32 SPX/IPX
dbmsvinn Win32 Banyan Vines
dbmsrpcn Win32 Multi-Protocol (Windows RPC)
For more information, see Q238949.

strConnection = _T("Driver={SQL Server};Server=130.120.110.001;"
    "Address=130.120.110.001,1052;Network=dbmssocn;Database=MyDatabaseName;"
    "Uid=myUsername;Pwd=myPassword;");
ODBC Driver for Oracle
For the current Oracle ODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;"
       "Uid=MyUsername;Pwd=MyPassword;");
For the older Oracle ODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC Driver for Oracle};"
    "ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;");
ODBC Driver for MySQL
If you want to connect to a local database, you can use a connection string like the following:

strConnect = _T("Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
    "Database=MyDatabase;User=MyUserName;Password=MyPassword;Option=4;");
If you want to connect with a remote database, you need to specify the name of the server or its IP in the Server parameter. If the Port is distinct to 3306 (default port), you must specify it.

strConnect = _T("Driver={mySQL ODBC 3.51 Driver};Server=MyRemoteHost;"
    "Port=3306;Option=4;Database=MyDatabase;Uid=MyUsername;Pwd=MyPassword;");
The parameter Option can be one or more of the following values:

1 - The client can't handle that MyODBC returns the real width of a column.
2 - The client can't handle that MySQL returns the true value of affected rows. If this flag is set then MySQL returns 'found rows' instead. One must have MySQL 3.21.14 or newer to get this to work.
4 - Make a debug log in c:\myodbc.log. This is the same as putting MYSQL_DEBUG=d:t:O,c::\myodbc.log in AUTOEXEC.BAT.
8 - Don't set any packet limit for results and parameters.
16 - Don't prompt for questions even if driver would like to prompt.
32 - Enable or disable the dynamic cursor support. This is not allowed in MyODBC 2.50.
64 - Ignore use of database name in 'database.table.column'.
128 - Force use of ODBC manager cursors (experimental).
256 - Disable the use of extended fetch (experimental).
512 - Pad CHAR fields to full column length.
1024 - SQLDescribeCol() will return fully qualified column names.
2048 - Use the compressed server/client protocol.
4096 - Tell server to ignore space after function name and before '(' (needed by PowerBuilder). This will make all function names keywords!
8192 - Connect with named pipes to a MySQLd server running on NT.
16384 - Change LONGLONG columns to INT columns (some applications can't handle LONGLONG).
32768 - Return 'user' as Table_qualifier and Table_owner from SQLTables (experimental).
65536 - Read parameters from the client and ODBC groups from my.cnf.
131072 - Add some extra safety checks (should not be needed but...).
If you want to have multiple options, you should add the above flags! For example: 16 + 1024 = 1030 and use Option= 1030;.

For more information, go to MyODBC Reference Manual.

ODBC Driver for AS400
strConnect = _T("Driver={Client Access ODBC Driver (32-bit)};System=myAS400;"
     "Uid=myUsername;Pwd=myPassword;");
ODBC Driver for SyBase
strConnect = _T("Driver={Sybase System 10};Srvr=MyServerName;Uid=MyUsername;"
      "Pwd=myPassword;");
ODBC Driver for Sybase SQL AnyWhere
strConnect = _T("ODBC;Driver=Sybase SQL Anywhere 5.0;"
   "DefaultDir=C:\\DatabasePath\;Dbf=C:\\SqlAnyWhere50\\MyDatabase.db;"
   "Uid=MyUsername;Pwd=MyPassword;Dsn=\"\";");
DSN Connection
ODBC DSN
strConnect = _T("DSN=MyDSN;Uid=MyUsername;Pwd=MyPassword;");
OLE DB Provider
OLE DB Provider for SQL Server
For Standard security:

strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
       "Initial Catalog=MyDatabaseName;"
       "User Id=MyUsername;Password=MyPassword;");

本文转自:SEO基地

本文链接:http://www.11zhuce.com/seo/511.html

0 回复,0 引用: 最全的数据库连接串代码

添加回复

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。