多线程 连接数据库问题

csrwgs 2004-11-19 03:20:16
我想用多线程读写数据库
建立了一个ThreadObject

void __fastcall TTest1::Execute()
{
TADOConnection *ADOConnection1;
ADOConnection1 = new TADOConnection(this);
WideString connectionstr = "Provider=SQLOLEDB.1;Password=csrwgsexpert;Persist Security Info=False;User ID=sa;Initial Catalog=csrwgs;Data Source=127.0.0.1";
ADOConnection1 ->ConnectionString = connectionstr ;
try
{
ADOConnection1 ->Open();
}
catch(...)
{
ShowMessage("Cannot connect to database");
exit(0);
}
}
总是出错
错误显示为 什么对象没有 'CoInitialize'
请指教

我在头文件里已经加了
adodb.hpp 了
...全文
154 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
3996906 2004-11-19
  • 打赏
  • 举报
回复
怎么说呢?你要理解线程的概念,和应用程序有差别的

这样说肯定不合适,在理解这个问题的时候你把它当作一个新的应用程序吧,先来一次 ::CoInitialize(NULL);
csrwgs 2004-11-19
  • 打赏
  • 举报
回复
我声明ADOConnection1 = new TADOConnection(NULL);
使用的是 NULL 参数
是不是跟这个有关?

我在主窗口
也是这么写的,就可以
到了ThreadObject 就不行了。奇怪啊~
3996906 2004-11-19
  • 打赏
  • 举报
回复
还有,线程里不能用这种东西:ShowMessage("Cannot connect to database");
3996906 2004-11-19
  • 打赏
  • 举报
回复
void __fastcall TTest1::Execute()
{
::CoInitialize(NULL); //+这个,不过你就算这个样子写可能还会出错,很麻烦,偶还在调试

TADOConnection *ADOConnection1;
ADOConnection1 = new TADOConnection(this);
WideString connectionstr = "Provider=SQLOLEDB.1;Password=csrwgsexpert;Persist Security Info=False;User ID=sa;Initial Catalog=csrwgs;Data Source=127.0.0.1";
ADOConnection1 ->ConnectionString = connectionstr ;
try
{
ADOConnection1 ->Open();
}
catch(...)
{
ShowMessage("Cannot connect to database");
exit(0);
}
}
特招 2004-11-19
  • 打赏
  • 举报
回复
up
csrwgs 2004-11-19
  • 打赏
  • 举报
回复
好像上面的程序
ADOConnection1 = new TADOConnection(this);
我写的是ADOConnection1 = new TADOConnection(NULL);

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧