|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
OverviewGenerally, 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 ConnectionODBC Driver for dBASEstrConnection = _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 ExcelstrConnection = _T("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;" bq=C:\\DatabasePath\\DBSpreadSheet.xls;DefaultDir=c:\\databasepath;"); ODBC Driver for TextstrConnection = _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 Note: You must specify the filename in the SQL statement... For example: CString strQuery = _T("Select Name, Address From Clients.csv"); Visual FoxProIf 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 strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;" "SourceType=DBF;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No"); ODBC Driver for AccessstrConnection = _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 ServerFor 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 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 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
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 OracleFor 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 MySQLIf 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 strConnect = _T("Driver={mySQL ODBC 3.51 Driver};Server=MyRemoteHost;" "Port=3306;Option=4;Database=MyDatabase;Uid=MyUsername;Pwd=MyPassword;"); The parameter
If you want to have multiple options, you should add the above flags! For example: 16 + 1024 = 1030 and use For more information, go to MyODBC Reference Manual. ODBC Driver for AS400strConnect = _T("Driver={Client Access ODBC Driver (32-bit)};System=myAS400;" "Uid=myUsername;Pwd=myPassword;"); ODBC Driver for SyBasestrConnect = _T("Driver={Sybase System 10};Srvr=MyServerName;Uid=MyUsername;" "Pwd=myPassword;"); ODBC Driver for Sybase SQL AnyWherestrConnect = _T("ODBC;Driver=Sybase SQL Anywhere 5.0;" "DefaultDir=C:\\DatabasePath\;Dbf=C:\\SqlAnyWhere50\\MyDatabase.db;" "Uid=MyUsername;Pwd=MyPassword;Dsn=\"\";"); DSN ConnectionODBC DSNstrConnect = _T("DSN=MyDSN;Uid=MyUsername;Pwd=MyPassword;"); OLE DB ProviderOLE DB Provider for SQL ServerFor Standard security: strConnect = _T("Provider=sqloledb;Data Source=MyServerName;" "Initial Catalog=MyDatabaseName;" "User Id=MyUsername;Password=MyPassword;"); For Trusted Connection security (Microsoft Windows NT integrated security): strConnect = _T("Provider=sqloledb;Data Source=MyServerName;" "Initial Catalog=MyDatabaseName;" "Integrated Security=SSPI;"); If you want to connect to a "Named Instance" (SQL Server 2000), you must to specify strConnect = _T("Provider=sqloledb;Data Source=MyServerName\MyInstanceName;" "Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;"); If you want to connect with a SQL Server running on the same computer, you must specify the keyword strConnect = _T("Provider=sqloledb;Data Source=(local);" "Initial Catalog=myDatabaseName;" "User ID=myUsername;Password=myPassword;"); To connect to SQL Server running on a remote computer (via an IP address): strConnect = _T("Provider=sqloledb;Network Library=DBMSSOCN;" "Data Source=130.120.110.001,1433;" "Initial Catalog=MyDatabaseName;User ID=MyUsername;" "Password=MyPassword;"); OLE DB Provider for MySQL (By Todd Smith)strConnection = _T("Provider=MySQLProv;Data Source=test"); Where OLE DB Provider for AS400strConnect = _T("Provider=IBMDA400;Data source=myAS400;User Id=myUsername;" "Password=myPassword;"); For more information, see: Using the OLE DB Provider for AS/400 and VSAM. OLE DB Provider for Active DirectorystrConnect = _T("Provider=ADSDSOObject;User Id=myUsername;Password=myPassword;"); For more information, see: Microsoft OLE DB Provider for Microsoft Active Directory Service. OLE DB Provider for DB2If you are using a TCP/IP connection: strConnect = _T("Provider=DB2OLEDB;Network Transport Library=TCPIP;" "Network Address=130.120.110.001;" "Initial Catalog=MyCatalog;Package Collection=MyPackageCollection;" "Default Schema=MySchema;User ID=MyUsername;Password=MyPassword;"); If you are using APPC connection: strConnect = _T("Provider=DB2OLEDB;APPC Local LU Alias=MyLocalLUAlias;" "APPC Remote LU Alias=MyRemoteLUAlias;Initial Catalog=MyCatalog;" "Package Collection=MyPackageCollection;Default Schema=MySchema;" "User ID=MyUsername;Password=MyPassword;"); For more information, see: Using the OLE DB Provider for DB2. OLE DB Provider for Microsoft Jet
OLE DB Provider for ODBC DatabasesIf you want to connect with a Microsoft Access database: strConnect = _T("Provider=MSDASQL;Driver={Microsoft Access Driver (*.mdb)};" "Dbq=c:\\DatabasePath\\MyDatabase.mdb;Uid=MyUsername;Pwd=MyPassword;"); If you want to connect with a SQL Server database: strConnect = _T("Provider=MSDASQL;Driver={SQL Server};Server=MyServerName;" "Database=MyDatabaseName;Uid=MyUsername;Pwd=MyPassword;"); If you want to use DSN: strConnect = _T("Provider=MSDASQL;PersistSecurityInfo=False;" "Trusted_Connection=Yes;" "Data Source=MyDSN;catalog=MyDatabase;"); For more information, see: Microsoft OLE DB Provider for ODBC. OLE DB Provider for OLAPMicrosoft® OLE DB for Online Analytical Processing (OLAP) is a set of objects and interfaces that extends the ability of OLE DB to provide access to multidimensional data stores. strConnect = _T("Provider=MSOLAP;Data Source=MyOLAPServerName;" "Initial Catalog=MyOLAPDatabaseName;"); Connection using HTTP:This feature enables a client application to connect to an Analysis server through Microsoft® Internet Information Services (IIS) by specifying a URL in the Using a URLstrConnect = _T("Provider=MSOLAP;Data Source=http://MyOLAPServerName/;" "Initial Catalog=MyOLAPDatabaseName;"); Using SSLstrConnect = _T("Provider=MSOLAP;Data Source=https://MyOLAPServerName/;" "Initial Catalog=MyOLAPDatabaseName;"); For more information, see: OLE DB for OLAP, Connecting Using HTTP. OLE DB Provider for OracleOLE DB Provider for Oracle (from Microsoft)The Microsoft OLE DB Provider for Oracle allows ADO to access Oracle databases. strConnect = _T("Provider=MSDAORA;Data Source=MyOracleDB;User Id=myUsername;" "Password=myPassword;"); For more information, see: Microsoft OLE DB Provider for Oracle. OLE DB Provider for Oracle (from Oracle).For Standard security: strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;" "User Id=myUsername;Password=myPassword;"); For a Trusted connection:
For more information, see: Oracle Provider for OLE DB Developer's Guide. OLE DB Provider for Visual FoxProstrConnect = _T("Provider=vfpoledb;" "Data Source=C:\\DatabasePath\\MyDatabase.dbc;"); For more information, see: Microsoft OLE DB Provider for Visual FoxPro. OLE DB Provider for Index Server (By Chris Maunder)strConnect = _T("provider=msidxs;Data Source=MyCatalog;"); For more information, see: Microsoft OLE DB Provider for Microsoft Indexing Service. OLE DB Data Link ConnectionsstrConnection = _T("File Name=c:\\DataBasePath\\DatabaseName.udl;"); If you want to create a Data Link File, you can make a new empty text file, change its extension by .udl, then double click over the file, and the operating system calls for you the Data Link app.
| ||||||||||||||||||||||||||||