工程->增加到工程->Comppnents and Controls Gallery->Registered ActiveX Controls->Microsoft ADO Data Control 6.0 然后将该控件拖到工程里,属性->General 选项->Build,然后选择你要的数据源,最后点测试连接就可以看到你要的连接字串了。
CDemoApp::CDemoApp() { m_AppConnString=_T("Driver={SQL Server};Server=127.0.0.1;") _T("Database=qiuzhenyan;Uid=sa;Pwd=123456;"); m_bConnected = FALSE; }///////////////////////////////////////////////////////////////////////////// // The one and only CDemoApp objectCDemoApp theApp;///////////////////////////////////////////////////////////////////////////// // CDemoApp initializationBOOL CDemoApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. AfxOleInit(); /******************连接通讯录数据库********************/ HRESULT hRes; try { hRes=m_pAppConn.CreateInstance(_T("ADODB.Connection")); m_pAppConn->ConnectionTimeout = 8; hRes=m_pAppConn->Open(_bstr_t((LPCTSTR) m_AppConnString), _T(""),_T(""),adModeUnknown); if(SUCCEEDED(hRes)) { m_bConnected = TRUE; } } catch(_com_error e)//捕捉异常 { CString errormessage; errormessage.Format(_T("连接数据库失败 :%s"),e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 return FALSE; } // 已经打开数据库#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CDemoDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CDemoView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; }这是我的代码,Database=qiuzhenyan;Uid=sa;Pwd=123456; 我在ODBC数据源管理器里,已经配置过,而且测试连接也是成功的。 可是代码运行还是:数据库连接失败:未指定的错误
然后将该控件拖到工程里,属性->General 选项->Build,然后选择你要的数据源,最后点测试连接就可以看到你要的连接字串了。
http://www.codeproject.com/KB/database/connectionstrings.aspx实例可以参照:
http://blog.csdn.net/zxhx/archive/2011/03/14/6249384.aspx
cadodatabase_c.Open(_T("driver={SQL Server};Server=61.155.131.91;Database=wstdb;"), _T("vstdbuser"), _T("vstdbuser"));你要的事这样的例子么
try
{
_variant_t RecordsAffected;
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(_T("Provider=SQLOLEDB;Server=XP-201103191332;Database=TEXT;User ID=sa;pwd=123456;Integrated Security=SSPI "),_T(""),_T(""),adModeUnknown);///连接数据库
}
}
catch( _com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(_T("连接数据库失败!\r\n错误信息:%s"),e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
} 这个估计更符合你的要求,自己研究下吧
{
m_AppConnString=_T("Driver={SQL Server};Server=127.0.0.1;")
_T("Database=qiuzhenyan;Uid=sa;Pwd=123456;");
m_bConnected = FALSE;
}/////////////////////////////////////////////////////////////////////////////
// The one and only CDemoApp objectCDemoApp theApp;/////////////////////////////////////////////////////////////////////////////
// CDemoApp initializationBOOL CDemoApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need. AfxOleInit();
/******************连接通讯录数据库********************/
HRESULT hRes;
try
{
hRes=m_pAppConn.CreateInstance(_T("ADODB.Connection"));
m_pAppConn->ConnectionTimeout = 8;
hRes=m_pAppConn->Open(_bstr_t((LPCTSTR) m_AppConnString),
_T(""),_T(""),adModeUnknown);
if(SUCCEEDED(hRes))
{
m_bConnected = TRUE;
}
}
catch(_com_error e)//捕捉异常
{
CString errormessage;
errormessage.Format(_T("连接数据库失败 :%s"),e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
// 已经打开数据库#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDemoDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDemoView));
AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE; // The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow(); return TRUE;
}这是我的代码,Database=qiuzhenyan;Uid=sa;Pwd=123456;
我在ODBC数据源管理器里,已经配置过,而且测试连接也是成功的。
可是代码运行还是:数据库连接失败:未指定的错误
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
BOOL m_fConnected;
HRESULT hr;
_bstr_t source("Driver={SQL Server};Server=LONELYLONG;Uid=sa;Pwd=;Database=pubs"); CoInitialize(NULL);
try{
hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
hr=m_pConnection->Open(source,"","",16);
if(SUCCEEDED(hr))
hr=m_pRecordset.CreateInstance(__uuidof(Recordset));
if(SUCCEEDED(hr))
m_fConnected=TRUE;
else
m_fConnected=FALSE;
}
catch(_com_error &e){
// MessageBox(NULL,e.ErrorMessage(),"error",NULL);
m_fConnected=FALSE;
}
if(!m_fConnected)
MessageBox(NULL,"ADO 数据源初始化失败!","error",NULL);
else
MessageBox(NULL,"SUCCESS!","OK",NULL);// if(m_pConnection->State)
// m_pConnection->Close();return 0;
}