線程的簡單使用 ( 积分: 50 )

  • 主题发起人 主题发起人 lixin38
  • 开始时间 开始时间
L

lixin38

Unregistered / Unconfirmed
GUEST, unregistred user!
把數據從Access--->SQL中,參考了這裡代碼http://www.delphibbs.com/delphibbs/dispq.asp?lid=1691966
但我把程序改為線程來執行卻出現了錯誤,錯誤信息如下:CoInitialize 尚未被呼叫。
窗體上就一個ADOConnection,兩個Button,
代碼很簡單就如下幾行:
procedure ExportData;
begin
try
Screen.Cursor:=crSQLWait;
try
Form1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=PServer';
//下面這行就是出問題的地方
Form1.ADOConnection1.Execute('Insert into [MyAccess] SELECT * FROM OpenDataSource( '
+QuotedStr('Microsoft.Jet.OLEDB.4.0')+','
+QuotedStr('Data Source="//PCName/IC/IOData.mdb";Persist Security Info=False;User ID=Admin;Password=;')+')...[IOData]');
except
on E:Exception do
Form1.Caption:=E.Message;
end;
finally
Screen.Cursor:=crDefault;
Form1.ADOConnection1.Connected:=False;
end;
end;


procedure TForm1.Btn_NoThreadClick(Sender: TObject);//不用線程的沒問題
begin
ExportData;
end;

{ TMyThread }

procedure TMyThread.Execute;
begin
ExportData;
end;

procedure TForm1.Btn_UseThreadClick(Sender: TObject);//用線程的,會有問題
begin
aThread:=TMyThread.Create(False);
end;
 
把數據從Access--->SQL中,參考了這裡代碼http://www.delphibbs.com/delphibbs/dispq.asp?lid=1691966
但我把程序改為線程來執行卻出現了錯誤,錯誤信息如下:CoInitialize 尚未被呼叫。
窗體上就一個ADOConnection,兩個Button,
代碼很簡單就如下幾行:
procedure ExportData;
begin
try
Screen.Cursor:=crSQLWait;
try
Form1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=PServer';
//下面這行就是出問題的地方
Form1.ADOConnection1.Execute('Insert into [MyAccess] SELECT * FROM OpenDataSource( '
+QuotedStr('Microsoft.Jet.OLEDB.4.0')+','
+QuotedStr('Data Source="//PCName/IC/IOData.mdb";Persist Security Info=False;User ID=Admin;Password=;')+')...[IOData]');
except
on E:Exception do
Form1.Caption:=E.Message;
end;
finally
Screen.Cursor:=crDefault;
Form1.ADOConnection1.Connected:=False;
end;
end;


procedure TForm1.Btn_NoThreadClick(Sender: TObject);//不用線程的沒問題
begin
ExportData;
end;

{ TMyThread }

procedure TMyThread.Execute;
begin
ExportData;
end;

procedure TForm1.Btn_UseThreadClick(Sender: TObject);//用線程的,會有問題
begin
aThread:=TMyThread.Create(False);
end;
 
在线程Execute中用Synchronize(ExportData);试试。
 
加上 ActiveX单元
procedure ExportData;
begin
CoInitialize(Nil);//////
try
Screen.Cursor:=crSQLWait;
try
Form1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=ECNEITS8874';
//下面這行就是出問題的地方
Form1.ADOConnection1.Execute('Insert into [MyAccess] SELECT * FROM OpenDataSource( '
+QuotedStr('Microsoft.Jet.OLEDB.4.0')+','
+QuotedStr('Data Source="//ecneits8874/IC/IOData.mdb";Persist Security Info=False;User ID=Admin;Password=;')+')...[IOData]');
except
on E:Exception do
Form1.Caption:=E.Message;
end;
finally
Screen.Cursor:=crDefault;
Form1.ADOConnection1.Connected:=False;
end;
CoUninitialize;///////
end;
 
to xgwzw:
你的方法可行的,能否解釋一下為什麼要加上
CoInitialize(Nil)/CoUninitialize呢?謝謝!
 
接受答案了.
 
后退
顶部