BCB多线程读取数据库
#include <vcl.h>#pragma hdrstop#include "Unit1.h"#include "Unit2.h"#pragma package(smart_init)__fastcall xtheadm::xtheadm(bool CreateSuspended) : TThread(CreateSuspended){}void __fastcall xtheadm::Execute(){ TADOConnection *ADOConnection1; TADOQuery *ADOQuery1; ADOConnection1=new TADOConnection(Application); ADOQuery1 = new TADOQuery(Application); ADOConnection1->ConnectionString="Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=swfcf;Data Source=My_Computer"; ADOQuery1->Connection=ADOConnection1; ADOQuery1->Close(); ADOQuery1->SQL->Clear(); ADOQuery1->SQL->Add("select * from swpr"); ADOQuery1->Open(); while(!ADOQuery1->Eof) { Form1->Memo1->Lines->Add(ADOQuery1->Fields->Fields[2]->AsString); } ADOQuery1->Close(); ADOQuery1->Close();}
void __fastcall test::Execute(){ //---- Place thread code here ---- CoInitialize(NULL); TADOConnection *ADOConnection1; TADOQuery *ADOQuery1; ADOConnection1=new TADOConnection(Application); ADOQuery1 = new TADOQuery(Application); ADOConnection1->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ExtractFilePath(Application->ExeName)+"test.mdb"; ADOConnection1->LoginPrompt = false; ADOConnection1->KeepConnection = true; ADOConnection1->Connected = true; ADOQuery1->Connection=ADOConnection1; ADOQuery1->SQL->Clear(); ADOQuery1->SQL->Add("select * from mytable1"); ADOQuery1->Open();while(!ADOQuery1->Eof) { Form1->Memo1->Lines->Add(ADOQuery1->Fields->Fields[2]->AsString); ADOQuery1->Next(); } ADOQuery1->Close(); delete ADOQuery1; delete ADOConnection1; CoUninitialize();}