威尼斯www.9778.com-威尼斯正版官方网站

ADO连接MS SQL SERVER 2000程序

日期:2020-03-22编辑作者:数据库

ado连接ms sql server 2000程序

unit Unit1;

function adothread(param: pointer卡塔尔(قطر‎: integer; stdcall;var con, cmd : variant;begin coinitialize(nil卡塔尔; con := getnewconnection(...卡塔尔国; if varisempty(con卡塔尔国 then begin //未连接上,退出 exit; end eles cmd := getnewcommand(conState of Qatar while bterminate do begin if varisempty(con卡塔尔(قطر‎ then //连接失利 退出 exit else sleep(1000State of Qatar; end; closeconnection(con卡塔尔国; //自定义的关门函数。 其实地度量试时, cmd := unassigned; couninitialize; result := 0;end;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
Datasnap.DBClient;

type
TForm1 = class(TForm)
cds: TClientDataSet;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure ExportData(const tableNames: string);
procedure ImportData(const fileName, tableNames: string);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses untDB;

procedure TForm1.Button1Click(Sender: TObject);
begin
ExportData('bas_kind,bas_goods');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ImportData(ExtractFilePath(Application.ExeName) + 'export.sql', 'bas_kind,bas_goods');
end;

procedure TForm1.ExportData(const tableNames: string);
var
sql, err: string;
sl, tables: TStringList;
i, h: Integer;
fieldNames, values: string;
function GetValue(field: TField): string;
begin
case field.DataType of
ftString, ftTimeStamp:
begin
Result := QuotedStr(field.AsString);
end;
ftBoolean:
begin
case field.AsBoolean of
true: Result := '1';
false: Result := '0';
end;
end;
else
if not VarIsNull(field.Value) then
begin
Result := VarToStr(field.Value);
end
else
Result := 'null';
end;
end;

begin
if tableNames = '' then
exit;
tables := TStringList.Create;
tables.Delimiter := ',';
tables.DelimitedText := tableNames;
sl := TStringList.Create;
sl.Clear;
for h := 0 to tables.Count - 1 do
begin
sql := 'select * from ' + tables[h];
if frmDB.QuerySQL(sql, cds, err) and (not cds.IsEmpty) then
begin
cds.First;
while not cds.Eof do
begin
fieldNames := '';
values := '';
for i := 0 to cds.FieldCount - 1 do
begin
if fieldNames = '' then
begin
fieldNames := cds.Fields[i].FieldName;
end
else
begin
fieldNames := fieldNames + ',' + cds.Fields[i].FieldName;
end;
if values = '' then
begin
values := GetValue(cds.Fields[i]);
end
else
begin
values := values + ',' + GetValue(cds.Fields[i]);
end;
end;
sl.Add('insert into ' + tables[h] + '(' + fieldNames + ') values ('

  • values + ')');
    cds.Next;
    end;
    end;
    end;
    sl.SaveToFile(ExtractFilePath(Application.ExeName) + 'export.sql');
    sl.Free;
    tables.Free;
    end;

procedure TForm1.ImportData(const fileName, tableNames: string);
var
cmd, tables: TStringList;
err, sql: string;
i: Integer;
begin
if (not FileExists(fileName)) or (tableNames = '') then
exit;
tables := TStringList.Create;
tables.Delimiter := ',';
tables.DelimitedText := tableNames;
for i := 0 to tables.Count - 1 do
begin
sql := 'truncate table ' + tables[i];
frmDB.ExecuteSQL(sql, err);
end;
tables.Free;

cmd := TStringList.Create;
cmd.LoadFromFile(fileName);
frmDB.ExecuteSQL(cmd.Text, err);
cmd.Free;
end;

end.

本文由威尼斯www.9778.com发布于数据库,转载请注明出处:ADO连接MS SQL SERVER 2000程序

关键词:

mssql 通用分页存储过程实例

一篇好用的通用分页存储过程,可以用在任何开发上哦,只要小小的改动就好了,有需要的朋友可以参考一下本款实...

详细>>

减少mssqlserver数据库死锁的技巧

/**//*--调用示例 if @count0 begin create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255)) if @kill_lock_spid=1 begin  ...

详细>>

sql is not null 与 is null 用法

讲授:不能相比 NULL 和 0;它们是不等价的。 NULL 用作未知的或不适用的值的占位符。 NULL 值的管理方式与任何值不一...

详细>>

sql随机查询数据语句(NewID(),Rnd,Rand(),random())

Select * From TABLE Order By Rand() Limit 10 Se(www.111cn.net)lect TOP N * From TABLE Order BY Rnd(Len(UserName)) 代码如下复制代码 Select TOP N...

详细>>