Ado connects to SQL server and adosqlserver
// Add the following sentence to the SQL server connection header file of ado: # import "C: \ Windows \ system \ msado15.dll" no_namespace rename ("EOF", "rsEOF ")
</Pre> <pre code_snippet_id = "620722" snippet_file_name = "blog_201501__3_721_60" name = "code" class = "cpp"> // CoInitialize (NULL) in cpp ); _ ConnectionPtr pConn (_ uuidof (Connection); _ RecordsetPtr pRst (_ uuidof (Recordset); _ CommandPtr pCmd (_ uuidof (Command )); try {// here is the entry from windows authentication. the SQL server authentication is also debugged on my host pConn-> ConnectionString = "Provider = SQLOLEDB.1; integrated Security = SSPI; Persist Security Info = False; Ini Tial Catalog = test; Data Source = WIN7-20120101NA \ SQLEXPRESS; Use Procedure for Prepare = 1; Auto Translate = True; Packet Size = 4096; Workstation ID = WIN7-20120101NA; use Encryption for Data = False; Tag with column collation when possible = False "; // pConn-> ConnectionString =" Provider = SQLOLEDB.1; Integrated Security = SSPI; Initial Catalog = test; data Source = WIN7-20120101NA \ SQLEXPRESS; "; // the field in the comment that must contain pConn-> Open ("","", "", AdConnectUnspecified); // open the connection pRst = pConn-> Execute ("select * from [dbo]. [user] ", NULL, ad1_text); // read the user table _ variant_t vUsername; while (! PRst-> rsEOF) {vUsername = pRst-> GetCollect ("id"); CString message; message. format ("% s", (LPCTSTR) (_ bstr_t) vUsername); AfxMessageBox (message); // display the number of current records pRst-> MoveNext ();}} catch (_ com_error e) // catch exceptions {CString errormessage; errormessage. format ("failed to connect to the database! \ R \ n error message: % s ", e. errorMessage (); AfxMessageBox (errormessage); // display the error message return;} pRst-> Close (); pConn-> Close (); pCmd. release (); pRst. release (); pConn. release (); CoUninitialize ();