1. The first connection string connecting to SQL SERVER:
For the database Authentication Mode "SQL SERVER and windows,
The connection string is:
CString strConn;
StrConn = "Provider = SQLOLEDB.1 ;\
Persist Security Info = true ;\
User ID = sa ;\
Password = 123456 ;\
Initial Catalog = tempdb ;\
Data Source = 127.0.0.1 ";
(Six items are included, including the user name and password, database name, and server name (the server name can be on the network and an IP address at this time ))
2. The second connection string used to connect to SQL SERVER:
The authentication mode is "windows only ",
The connection string is:
StrConn ="
Provider = SQLOLEDB.1 ;\
Integrated Security = SSPI ;\
Persist Security Info = false ;\
Initial Catalog = tempdb ;\
Data Source = (local )";
(Five items are included. Because windows has been verified, the user name and password are no longer required. If it is a local SQL SERVER, the SERVER name can be written as (local ))
3. The second connection string used to connect to SQL SERVER:
Only for the ODBC connection mode,
The connection string is:
StrConn ="
Provider = SQLOLEDB ;\
Server = (local );\
Database = tempdb ;\
Uid = sa ;\
Pwd = 12345 ;"
Remember any of the above three connection methods. The second method is recommended.
We can use udl files to handle connection string problems.~~
Create an empty txt file. modify the extension to udl. UDL itself is a text file. it is mainly used to view the configured database connection words. configure first. use NotePad to open
Eg: Create a test. udl file on the desktop,
1. If you want to connect to the SQLserver database, choose [provider:
Microsoft ole db provider for SQL server,
2. In connection, select the server name and select Richard Lee (the name of the local server)
If you select windows Authentication mode, you do not need to specify the user name and password,
Select database on the server. In this field, enter a database you want to connect to in sqlserver, such as test.
Click test connection. If the connection is successful, the connection strings are correct ~~
3. (We can see the connection string in the [all] column)
4. Change the file suffix to *. txt. After opening the file, you can find the connection string. For example:
[Oledb]
; Everything after this line is an ole db initstring
Provider = SQLOLEDB.1; Integrated Security = SSPI; Persist Security Info = False; Initial Catalog = test1; Data Source = Richard
5. The above connection string can be used in our program, such
Hr = m_pConnection.CreateInstance (_ uuidof (Connection ));
Hr = m_pConnection-> Open ("Provider = SQLOLEDB.1 ;\
Integrated Security = SSPI ;\
Persist Security Info = False ;\
Initial Catalog = test1 ;\
Data Source = (local) "," "," ", adModeUnknown );
(PS: In the connection string, the order of each part has no effect on the database connection, that is, the Integrated Security and Persist Security Info. It doesn't matter who is there first and who is there)
**************************************** **************************************** ****************************************
VC_ADO connection SQL server connection string and VC-ADO connection ACCESS connection string some different places ~~
1. Frequently Used for ACCESS connection
Hr = m_pConnection-> Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = D: \ test1.mdb", "", "", adModeUnknown );
That is, the connection string is "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = D: \ test1.mdb"
Here Data Source = D: \ test1.mdb is the name of the ACCESS database. Unless the database file is placed in the current project path, all the path names should be written and the database suffix name should be written.
Note that it is "D :\\" instead of "D: \". If it is written as the latter, an error will occur.
2. When connecting to SQLSERVER,
Initial Catalog = test1; this indicates the name of the connected database. Note that test1 is the name of the database. You cannot add a path or suffix *. mdf to it.
Initial Catalog = D: \ Program Files \ Microsoft SQL Server \ MSSQL10.MSSQLSERVER \ MSSQL \ DATA \ test1.mdf; error ~~