这是偶以前回答的一个问题里面的源码,呵呵,就是建表和删除表的,你看看吧。
这样避免了数据服务器与事物处理器所结合而带来的低效率问题。
写中间层规则也就是写用户的函数或者方法接口,尽量的把用户经常需要的函数或者查询写在
中间层,这样避免了网络通讯的数据量大的问题。
unit Uit_NumPlan;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComServ, ComObj, VCLCom, StdVcl, bdemts, DataBkr, DBClient,
MtsRdm, Mtx, NumPlan_TLB, Provider, DB, ADODB;
type
TMST_NumPlan = class(TMtsDataModule, IMST_NumPlan)
Adc_Numplan: TADOConnection;
Adq_NumPlan: TADOQuery;
Dsp_NumPlan: TDataSetProvider;
private
{ Private declarations }
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
procedure sAddTable(var sTableName: OleVariant);
safecall;
procedure sDropTable(var sTableName: OleVariant);
safecall;
public
{ Public declarations }
end;
var
MST_NumPlan: TMST_NumPlan;
implementation
{$R *.DFM}
class procedure TMST_NumPlan.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
procedure TMST_NumPlan.sAddTable(var sTableName: OleVariant);
var
sSQL : String;
begin
//检测是否存在表
sSQL := 'SELECT name FROM sysobjects WHERE rtrim(name)='''+sTableName+'''';
Try
With Adq_NumPlando
begin
Close;
SQL.Clear;
SQL.Add(sSQL);
Prepared;
Open;
//如果不存在,则建立,并设置主键与默认值
if Adq_NumPlan.RecordCount = 0 then
begin
Close;
sSQL := 'CREATE TABLE [dbo].['+sTableName+'] ('
+'[f_ibusnoid] [int] NOT NULL ,'
+'[f_iseatid] [int] NOT NULL ,'
+'[f_cvseatnu] [char] (4) COLLATE Chinese_PRC_BIN NOT NULL ,'
+'[f_cuserid] [char] (6) COLLATE Chinese_PRC_BIN NULL ,'
+'[f_ctickstatu] [char] (1) COLLATE Chinese_PRC_BIN NULL ,'
+'[f_istatorder0] [int] NULL ,'
+'[f_istatorder1] [int] NOT NULL'
+'[f_dtgq] [datetime] NULL,'
+') ON [PRIMARY]';
SQL.Clear;
SQL.Add(sSQL);
ExecSQL;
Close;
sSQL := 'ALTER TABLE [dbo].['+sTableName+'] WITH NOCHECK ADD'
+' CONSTRAINT [PK_'+sTableName+'] PRIMARY KEY CLUSTERED'
+' ('
+' [f_ibusnoid],'
+' [f_iseatid]'
+' ) ON [PRIMARY]';
SQL.Clear;
SQL.Add(sSQL);
ExecSQL;
Close;
sSQL := 'ALTER TABLE [dbo].['+sTableName+'] WITH NOCHECK ADD'
+' CONSTRAINT [DF_'+sTableName+'_f_ctickstatu] DEFAULT ('
+'0'
+') FOR [f_ctickstatu],'
+' CONSTRAINT [DF_'+sTableName+'_f_istatorder0] DEFAULT (1) FOR [f_istatorder0],'
+' CONSTRAINT [DF_'+sTableName+'_f_istatorder1] DEFAULT (99) FOR [f_istatorder1]';
Close;
SQL.Clear;
SQL.Add(sSQL);
ExecSQL;
end;
end;
except
on Exceptiondo
begin
SetAbort;
Raise;
end;
end;
end;
procedure TMST_NumPlan.sDropTable(var sTableName: OleVariant);
var
sSQL : String;
begin
Try
With Adq_NumPlando
begin
sSQL := 'DROP TABLE '+sTableName;
Close;
SQL.Clear;
SQL.Add(sSQL);
ExecSQL;
end;
except
on Exceptiondo
begin
SetAbort;
Raise;
end;
end;
end;
initialization
TComponentFactory.Create(ComServer, TMST_NumPlan,
Class_MST_NumPlan, ciMultiInstance, tmApartment);
end.
这只是其中的两个接口而已。
一个是建表,一个是删表,看了就应该有点头绪了。