从对话框接收连接数据库的字符串,如Server,DATABASE,UID,PWD
CString SerName,DBName,SerAccount,SerPsw;//从对话框接来的数据,对应连接字符串的四项
UpdateData(TRUE);
SerName=pDlg.m_serName;
DBName=pDlg.m_dbName;
SerAccount=pDlg.m_serAccount;
SerPsw=pDlg.m_serPsw;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=SerName;DATABASE=DBName;UID=SerAccount;PWD=SerPsw","","",adModeUnknown);
但是无法成功连接,请高手帮帮忙,这是不是得需要强制类型转换,还是怎么的?
应该如何写?
谢谢!
5 个解决方案
#1
觉得错误的地方不在于你这段程序,而在于你实现连接的部分有误。
#2
应该不会吧,如果我把HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=SerName;DATABASE=DBName;UID=SerAccount;PWD=SerPsw","","",adModeUnknown);
改成
HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=hanyu;DATABASE=111;UID=sa;PWD=12345","","",adModeUnknown);
就可以正常连接,而且也可以操作,换了上边的方式就不行啊!
改成
HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=hanyu;DATABASE=111;UID=sa;PWD=12345","","",adModeUnknown);
就可以正常连接,而且也可以操作,换了上边的方式就不行啊!
#3
你的Open串写错了,虽然你写了DBName变量,但是你这么写会把DBName这个变量当成字符串锝
#4
帮你改一下:
CString str;
str.Format("Provider=SQLOLEDB;Server=%s;DATABASE=%s;UID=%s;PWD=%s",SerName,DBName,SerAccount,SerPsw);
HRESULT succ=m_pConnection->Open(str,"","",adModeUnknown);
CString str;
str.Format("Provider=SQLOLEDB;Server=%s;DATABASE=%s;UID=%s;PWD=%s",SerName,DBName,SerAccount,SerPsw);
HRESULT succ=m_pConnection->Open(str,"","",adModeUnknown);
#5
呵呵,多谢(快乐鹦鹉)!
不过有个小毛病,我已经改了,如下:
m_pConnection->Open((_bstr_t)str,"","",adModeUnknown);
问题解决了,我结帖了,多谢两位!
不过有个小毛病,我已经改了,如下:
m_pConnection->Open((_bstr_t)str,"","",adModeUnknown);
问题解决了,我结帖了,多谢两位!
#1
觉得错误的地方不在于你这段程序,而在于你实现连接的部分有误。
#2
应该不会吧,如果我把HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=SerName;DATABASE=DBName;UID=SerAccount;PWD=SerPsw","","",adModeUnknown);
改成
HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=hanyu;DATABASE=111;UID=sa;PWD=12345","","",adModeUnknown);
就可以正常连接,而且也可以操作,换了上边的方式就不行啊!
改成
HRESULT succ=m_pConnection->Open("Provider=SQLOLEDB;Server=hanyu;DATABASE=111;UID=sa;PWD=12345","","",adModeUnknown);
就可以正常连接,而且也可以操作,换了上边的方式就不行啊!
#3
你的Open串写错了,虽然你写了DBName变量,但是你这么写会把DBName这个变量当成字符串锝
#4
帮你改一下:
CString str;
str.Format("Provider=SQLOLEDB;Server=%s;DATABASE=%s;UID=%s;PWD=%s",SerName,DBName,SerAccount,SerPsw);
HRESULT succ=m_pConnection->Open(str,"","",adModeUnknown);
CString str;
str.Format("Provider=SQLOLEDB;Server=%s;DATABASE=%s;UID=%s;PWD=%s",SerName,DBName,SerAccount,SerPsw);
HRESULT succ=m_pConnection->Open(str,"","",adModeUnknown);
#5
呵呵,多谢(快乐鹦鹉)!
不过有个小毛病,我已经改了,如下:
m_pConnection->Open((_bstr_t)str,"","",adModeUnknown);
问题解决了,我结帖了,多谢两位!
不过有个小毛病,我已经改了,如下:
m_pConnection->Open((_bstr_t)str,"","",adModeUnknown);
问题解决了,我结帖了,多谢两位!