动态创建TQuery(50分)

  • 主题发起人 主题发起人 coolblue
  • 开始时间 开始时间
C

coolblue

Unregistered / Unconfirmed
GUEST, unregistred user!
Var
Session11:TSession;
DataBase11:TDataBase;
Query11:TQuery;
Datasource11:TDatasource;
begin
{ Run two separate queries, each in their own thread }
//RunBackgroundQuery(Session1, DataBase1, Query1,Datasource1);
//RunBackgroundQuery(Session2, DataBase2, Query2,Datasource2);
Session11 := TSession.Create(Application);
Session11.Name := 'Session11';
Session11.SessionName := 'Ses11';
Session11.Active := True;
DataBase11 := TDataBase.Create(Application);
DataBase11.Name := 'DataBase11';
DataBase11.AliasName := 'GSMMonitor';
DataBase11.DatabaseName := 'DB1';
DataBase11.LoginPrompt := False;
DataBase11.Params.Add('USER NAME=sa');
DataBase11.Connected := True;
Query11 := TQuery.Create(Application);
Query11.Name := 'Query11';
Query11.SessionName := Session11.SessionName;
Query11.DatabaseName := DataBase11.DatabaseName;
Query11.SQL.Add('Select * from JZZT');
Query11.Open;----------------------------------------->出错:Unknown database.Alias DB1
Datasource11 := TDatasource.Create(Application);
RunBackgroundQuery(Session11, DataBase11, Query11,Datasource11);
//RunBackgroundQuery(Session21, DataBase21, Query21,Datasource21);
 
sYourDB := 'DataBase11';
DataBase11.Name := sYourDB ;
Query11.DatabaseName := sYourDB ;
 
Query11.DatabaseName := DataBase11.Name
 
谢谢你的关注,但我试验过!
不行!
 
加上DataBase11.SessionName := Session11.SessionName;
 
不说了,我的程序中用到的一个函数,是正确的,你看看
void __fastcall DataHandle::DataSourceInitial(void)
{
int DB_Count=0;
DataForm->DataSourceList->Close();
DataForm->DataSourceList->Open();
DataForm->DataSourceList->First();
DataSource_List=new TStringList;
DataSource_DB=new PDatabase;
DataSource_Query=new PQuery;
// DataFieldMap=new PPString_List;
while(!DataForm->DataSourceList->Eof)
{
// if(DataSource_DB[DB_Count]==NULL)
DataSource_DB[DB_Count]=new TDatabase(Application);
DataSource_DB[DB_Count]->Connected =false;
DataSource_DB[DB_Count]->Params->Clear();
DataSource_DB[DB_Count]->DatabaseName=DataForm->DataSourceList->FieldByName("aliasname")->AsString+IntToStr(DB_Count);
DataSource_DB[DB_Count]->AliasName =DataForm->DataSourceList->FieldByName("aliasname")->AsString;
DataSource_DB[DB_Count]->Params->Add("USER NAME="+DataForm->DataSourceList->FieldByName("connectuser")->AsString);
DataSource_DB[DB_Count]->Params->Add("ODBC DSN="+DataForm->DataSourceList->FieldByName("aliasname")->AsString);
DataSource_DB[DB_Count]->Params->Add("OPEN MODE=READ/WRITE");
DataSource_DB[DB_Count]->Params->Add("SCHEMA CACHE SIZE=8");
DataSource_DB[DB_Count]->Params->Add("SQLQRYMODE=");
DataSource_DB[DB_Count]->Params->Add("LANGDRIVER=");
DataSource_DB[DB_Count]->Params->Add("SQLPASSTHRU MODE=SHARED AUTOCOMMIT");
DataSource_DB[DB_Count]->Params->Add("SCHEMA CACHE TIME=-1");
DataSource_DB[DB_Count]->Params->Add("MAX ROWS=-1");
DataSource_DB[DB_Count]->Params->Add("BATCH COUNT=200");
DataSource_DB[DB_Count]->Params->Add("ENABLE SCHEMA CACHE=FALSE");
DataSource_DB[DB_Count]->Params->Add("SCHEMA CACHE DIR=");
DataSource_DB[DB_Count]->Params->Add("ENABLE BCD=FALSE");
DataSource_DB[DB_Count]->Params->Add("ROWSET SIZE=20");
DataSource_DB[DB_Count]->Params->Add("BLOBS TO CACHE=64");
DataSource_DB[DB_Count]->Params->Add("PASSWORD="+DataForm->DataSourceList->FieldByName("password")->AsString);
DataSource_DB[DB_Count]->LoginPrompt=false;
try
{
DataSource_DB[DB_Count]->Connected = true;
DataSource_List->Add(DataForm->DataSourceList->FieldByName("datasourcename")->AsString);
}
catch (Exception &exception)
{
ShowMessage("数据源初始化失败,数据库相关部分不可用");
}
// if(DataSource_Query[DB_Count]==NULL)
// {
DataSource_Query[DB_Count]=new TQuery(Application);
DataSource_Query[DB_Count]->DatabaseName=DataSource_DB[DB_Count]->DatabaseName;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DataSource_Query[DB_Count]->SQL->Text=DataForm->DataSourceList->FieldByName("sql")->Value;
// }
DataFieldMap[DB_Count]=new PString_List;
DataFieldMap[DB_Count][0]=new TStringList;
DataFieldMap[DB_Count][1]=new TStringList;
DataForm->DataFieldQuery->Close();
DataForm->DataFieldQuery->Params->Items[0]->AsString=DataForm->DataSourceList->FieldByName("datasourcename")->AsString;
DataForm->DataFieldQuery->Open();
DataForm->DataFieldQuery->First();
while(!DataForm->DataFieldQuery->Eof)
{
DataFieldMap[DB_Count][0]->Add(DataForm->DataFieldQuery->FieldByName("ChineseField")->AsString);
DataFieldMap[DB_Count][1]->Add(DataForm->DataFieldQuery->FieldByName("TableField")->AsString);
DataForm->DataFieldQuery->Next();
}
DataForm->DataFieldQuery->Close();
DB_Count++;
DataForm->DataSourceList->Next();
}
DataForm->DataSourceList->Close();
pGetCurrentDataSource=GetCurrentDataSource;
pGetField=GetField;
pSetLabelCaption=SetLabelCaption;
pSetImagePicture=SetImagePicture;
}
//----------------------------------------------------------------------------
 
sonie的方法我没有试验!
用来是我敲漏:DataBase11.SessionName := Session11.SessionName
谢谢panyongze!
 
后退
顶部