One, the first connection string to connect to SQL Server:
is for SQL SERVER and Windows for the database authentication mode,
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 ";
(contains 6 items, user name and password, database name, server name (the server name can be on the network, this is an IP address))
Second connection string connecting to SQL Server:
For the authentication mode "Windows only",
The connection string is:
Strconn= "
Provider = sqloledb.1;\
Integrated security=sspi;\
Persist Security Info =false;\
Initial Catalog = tempdb;\
Data Source = (local) ";
(contains 5 items, because there is already Windows authentication, so the user name and password is no longer required, if it is the local SQL Server, then the server name can be written as (local))
Third, connect the second connection string to SQL Server:
ODBC-only Connection mode,
The connection string is:
Strconn= "
Provider = sqloledb;\
Server = (local); \
Database = tempdb;\
UID = sa;\
pwd=12345; "
The above three kinds of connection, remember any one can, recommend the use of the second
regarding the connection string problem, we can use the UDL file to process ~ ~
Create a new empty TXT file. It's OK to change the extension to UDL. The UDL itself is a text file. Its main function is to view the configured database connection Word. Configure it first. Then use Notepad to open
eg: we create a test.udl file on the desktop,
1. If you are connecting to the SQL Server database, the provider chooses:
Microsoft OLE DB Provider for SQL Server,
2. "Connection", the server name, select Richardlee (that is, the native server name)
If we choose Windows Authentication mode, we don't have to specify a user name and password.
The "Select database on Server" column, fill in the database that we want to connect to in SQL Servers, such as test
Then click "Test Connection", if the connection is successful, it means that our connection string is right ~ ~
3. (We fill in the connection string, in the "All" this column can be seen)
4. Change the suffix name of the file to *.txt open, and you will find the connection string, eg:
[OLE DB]
; Everything after the "is" an OLE DB initstring
Provider=sqloledb.1;integrated Security=sspi; Persist Security info=false;initial Catalog=test1;data Source=richardlee
5. The above connection string can be used in our program, such as
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 the parts has no effect on the database connection, that is, integrated security and persist security Info, who is in front of who in the post, it doesn't matter)
*************************************************************************************************************** *********
Vc_ado connect to SQL Server connection string and Vc-ado connection string for access there are some different places ~ ~
1. When connecting to access, common
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"
The data Source=d:\\test1.mdb here is the name of the Access database, unless the database file is placed under the current project path, the full pathname is written, and the suffix name of the database to be written
Note that it is "d:\\", not "d:\", which is written in the latter, which will make a mistake.
2. When you connect to SQL Server,
Initial Catalog=test1; This represents the database name of the connection, note that the test1 here is the name of the database, you cannot add a path to it, and you cannot add a suffix *.mdf, written
Initial catalog=d:\program Files\Microsoft SQL Server\mssql10. The mssqlserver\mssql\data\test1.mdf will make a mistake.