void CAasdDlg::OnButton1()
{
_ConnectionPtr m_pConnection; //创建智能指针
m_pConnection.CreateInstance(_uuidof(Connection)); CoInitialize(NULL); //初始化COM
try
{
//连接字符串,pubs为数据库名
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=21259520q;Persist Security Info=True;User ID=sa;Initial Catalog=pubs";
m_pConnection->Open(" "," "," ",adConnectUnspecified); //调用Open方法 }
catch(_com_error e) //捕获连接异常
{
return ;
}
_RecordsetPtr m_pRecordset(_uuidof(Recordset));
_CommandPtr m_pCommand(_uuidof(Command)); //查询
m_pCommand->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
m_pCommand->CommandText="select * from authors"; //authors为表名
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdText);
//写进list
while(!m_pRecordset->adoEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)m_pRecordset->GetCollect("au_lname"));
m_pRecordset->MoveNext();
} m_pRecordset->Close();
m_pConnection->Close();
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release(); CoUninitialize();
// TODO: Add your control notification handler code here } 读取默认SQL数据库中pubs文件 author表里的数据并输出到列表框里 编译成功但是无法读取? 什么地方错了?搞不清楚,查半天了。
{
_ConnectionPtr m_pConnection; //创建智能指针
m_pConnection.CreateInstance(_uuidof(Connection)); CoInitialize(NULL); //初始化COM
try
{
//连接字符串,pubs为数据库名
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Password=21259520q;Persist Security Info=True;User ID=sa;Initial Catalog=pubs";
m_pConnection->Open(" "," "," ",adConnectUnspecified); //调用Open方法 }
catch(_com_error e) //捕获连接异常
{
return ;
}
_RecordsetPtr m_pRecordset(_uuidof(Recordset));
_CommandPtr m_pCommand(_uuidof(Command)); //查询
m_pCommand->put_ActiveConnection(_variant_t((IDispatch*)m_pConnection));
m_pCommand->CommandText="select * from authors"; //authors为表名
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdText);
//写进list
while(!m_pRecordset->adoEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)m_pRecordset->GetCollect("au_lname"));
m_pRecordset->MoveNext();
} m_pRecordset->Close();
m_pConnection->Close();
m_pCommand.Release();
m_pRecordset.Release();
m_pConnection.Release(); CoUninitialize();
// TODO: Add your control notification handler code here } 读取默认SQL数据库中pubs文件 author表里的数据并输出到列表框里 编译成功但是无法读取? 什么地方错了?搞不清楚,查半天了。
解决方案 »
- 单项选择题标准化考试系统设计(跪求)
- 我想问一下有关图像处理方面的知识
- 用拆分条拆分窗口后,如何控制其上的滚动条?
- (MFC)为什么WCHAR 的数组里明明存的是一个连续的字符串,当格式成CString 时就出问题呢?
- 一个关于 LOGFONT 结构的问题 续
- 求助!!!如何改变Button上的字体大小????
- 哪有vc6英文版
- 请问如何用代码删除已有的网络协议或网络适配器?????
- 错误--"缺少更新和刷新的键列信息",怎么解决?
- VC水晶报表动态加载bmp图片
- 做了兩個語言版本的string table,怎麽生成另一個語言版本的程序
- QQ2009 聊天记录用了个webbrowser control 这个大概如何实现的呢?
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)m_pRecordset->GetCollect("au_lname"));
这样的代码很差的,是不可取的。你根本就没有办法调试它。将它拆分开,然后看每一步是否得出正确的数据。
csConnect.Format("Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s"
,dbConfig.m_sHostPass,dbConfig.m_sHostUser,dbConfig.m_sDBName,dbConfig.m_sHostName);
其中Initial Catalog是数据库名称,Data Source是数据库服务器名称
名字叫sqlse,测试连接也成功了,代码改成
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=pubs;Data Source=sqlse;User ID=sa;Password=21259520q";还是无法连接上啊
郁闷了 GHOST-7642677BD就是服务器名
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=pubs;Data Source=GHOST-7642677BD;User ID=sa;Password=21259520q"; 难道是Open方法有错
m_pConnection->Open(" "," "," ",adConnectUnspecified); 去查查用法去先破书受不了,什么都不讲的
那你再怎么改代码也是没有用的..
修改后的连接代码为
_ConnectionPtr m_pConnection; //创建智能指针
m_pConnection.CreateInstance(_uuidof(Connection));
CoInitialize(NULL);//初始化COM
try
{
//连接字符串,pubs为数据库名
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=false;Initial Catalog=pubs;Data Source=GHOST-7642677BD;User ID=sa;Password=21259520q";
m_pConnection->Open(" "," "," ",adConnectUnspecified); //调用Open方法
}
catch(_com_error e) //捕获连接异常
{
MessageBox("数据库连接失败","警告",MB_OK);
return ;
}仍然连接不上,找了很久服务器名,SQL SERVER用户名 密,数据库名都写好了,仍然无法打开这个实在找不出错误了,open的方法问题还是定义指针存在问题?
把这句放在最前面执行,哥们
放在前面的话 开始连接后停顿好几秒后才弹出 连接失败
如果放在后面就立刻弹出失败消息框
放在前面也没用的。。
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print COM errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
我这个密码就是21259520q 用户名sa
连接不上为什么
需要怎样配置呢。
m_pConnection->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=false;Initial Catalog=pubs;Data Source=GHOST-7642677BD;User ID=sa;Password=21259520q";
m_pConnection->Open(" "," "," ",adConnectUnspecified); //调用Open方法 改为
_bstr_t strConnect="Provider=…………";
m_pConnection->Open(strConnect,"","",adModeUnknown);就OK啦