Delphi中多线程下使用使用 UniDAC+MSSQL 需要注意的问题


本文整理自网络,侵删。

 一般解决方法是在线程开始启用 CoInitialize(nil),线程结束调用 CoUninitialize 。如果你使用多种数据库连接,比如三层中经常切换到MSSQL和Oracle,我们只需在判断 TUniConnection 的连接前事件 OnBeforeConnect 写下如下代码:
procedure TServDBFunc.ServConnBeforeConnect(Sender: TObject);
begin
if (ServConn <> nil) and SameText(ServConn.ProviderName,'SQL Server');then
CoInitialize(nil);
end;
在TUniConnection 的关闭连接后事件 OnAfterDisconnect 写下如下代码:
procedure TServDBFunc.ServConnAfterDisconnect(Sender: TObject);
begin
if (ServConn <> nil) and SameText(ServConn.ProviderName,'SQL Server');then
CoUninitialize;
end;
需要注意的是,必须先判断连接控件 (ServConn <> nil) 是否为空,否则,你会陷入指针释放的问题。

相关阅读 >>

Delphi在网格cxgrid的左上角放置一个按钮

Delphi 对汉字字符串的截取问题

Delphi线程简单创建、挂起、激活与终止

Delphi 双击隐藏tabsheet

Delphi mainmenu控件 checkde属性用法

Delphi最简单的多线程网页采集

Delphi 获取网页源代码的最简单办法

cvcode.pas

Delphi for 循环 to和downto的理解

Delphi xe mysql数据库操作类 mysqlhelper

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...