如何编写用来执行sql的函数???? 如何编写用来执行sql的函数???? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要什么函数?直接在STRING类型中加就行了嘛。如:var sqlstr:string;sqlstr:=edit1.text;adoquery1.sql.add(sqlstr); //-------------------------------------------------------------------------// 文件名:DBOperate.pas//// 类名:数据库操作类// 描述:用于实现数据库的基本操作功能//// 作者:Win Lai// 创建日期:2003-12-11// 修改日期:2003-12-18//// 在Delphi6.0编译通过//// 使用://// 1.定义类对象。// MyClass : TDBOperate;//// 2.分配空间给类对象。// MyClass := TDBOperate.Create(nil);//// 3.建立数据库链接。// MyClass.ConnectionStrings := '[数据库链接串]'// MyClass.Connect//// 4.操作数据库。// MyClass.SQLStrings := '[SQL语句]'// MyClass.SQLExec// MyClass.SQLOpen//// 5.链接数据库控件。// MyClass.DataSource.DateSet := MyClass.ADOTable;// [数据库控件].DataSource := MyClass.DataSource;//// 6.释放类对象。// MyClass.Free;////-------------------------------------------------------------------------unit DBOperate;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ADODB, DB;type TDBOperate = class(TObject) private FADOConnection: TADOConnection; FADOQuery: TADOQuery; FADOTable: TADOTable; FDataSource: TDataSource; FConnectionStrings: string; FSQLStrings: string; protected public constructor Create(AOwner: TComponent); virtual; destructor Destroy; override; procedure Connect; procedure SQLExec; procedure SQLOpen; function Disconnect: boolean; function IsConnected: boolean; property ADOConnection: TADOConnection read FADOConnection write FADOConnection; property ADOQuery: TADOQuery read FADOQuery write FADOQuery; property ADOTable: TADOTable read FADOTable write FADOTable; property DataSource: TDataSource read FDataSource write FDataSource; property ConnectionStrings: string read FConnectionStrings write FConnectionStrings; property SQLStrings: string read FSQLStrings write FSQLStrings;end;implementation//-------------------------------------------------------------------------// 构造函数constructor TDBOperate.Create(AOwner: TComponent);begin inherited Create(); //.. FADOConnection := TADOConnection.Create(nil); FADOQuery := TADOQuery.Create(nil); FADOTable := TADOTable.Create(nil); FDataSource := TDataSource.Create(nil); FADOConnection.LoginPrompt := false; // 不显示登录框end;//-------------------------------------------------------------------------// 析构函数destructor TDBOperate.Destroy;begin //.. Disconnect; FADOConnection.Free; FADOQuery.Free; FADOTable.Free; FDataSource.Free; inherited Destroy;end;//-------------------------------------------------------------------------// 链接数据库procedure TDBOperate.Connect;begin // 已经链接 if IsConnected=true then begin exit end; try FADOQuery.Connection := FADOConnection; FADOTable.Connection := FADOConnection; FADOConnection.Close; FADOConnection.ConnectionString := FConnectionStrings; FADOConnection.Open; if FADOConnection.Connected = false then begin // 链接数据库失败 Application.MessageBox('链接数据库失败!', '错误', MB_OK OR MB_ICONERROR); Application.Terminate; end; except Application.MessageBox('链接数据库失败!', '错误', MB_OK OR MB_ICONERROR); Application.Terminate; end;end;//-------------------------------------------------------------------------// 断开链接数据库function TDBOperate.Disconnect: boolean;begin // 已经断开链接 if IsConnected=false then begin Result := true; exit; end; try FADOConnection.Close; Result := true; except Result := false; end;end;//-------------------------------------------------------------------------// 检查是否链接数据库function TDBOperate.IsConnected: boolean;begin if ADOConnection.Connected = false then Result := false else Result := true;end;//-------------------------------------------------------------------------// 执行SQL语句,不返回数据集procedure TDBOperate.SQLExec;begin FADOQuery.Close; FADOQuery.SQL.Text := FSQLStrings; FADOQuery.ExecSQL;end;//-------------------------------------------------------------------------// 执行SQL语句,可返回数据集procedure TDBOperate.SQLOpen;begin FADOQuery.Close; FADOQuery.SQL.Text := FSQLStrings; FADOQuery.Open;end;//-------------------------------------------------------------------------end. delphi2007 代码自动完成的增加问题 老问题??? 怎么在程序中模拟一个输入来替换标准输入Input? delphi难题!关于DBGrid 各位老大帮忙!!!! 关于CreateOleObject问题 谁能提供<商场管理系统单机版>所用的的第三方控件啊? 小问题-----30分 为什么用Twebbrower控件读取网页时没办法记录Session的内容 关于Pascal语言,问题2 一个小问题(主丛表录入),希望各位路过来看看。 为什么这个函数调用就出错呢???????????
sqlstr:=edit1.text;
adoquery1.sql.add(sqlstr);
// 文件名:DBOperate.pas
//
// 类名:数据库操作类
// 描述:用于实现数据库的基本操作功能
//
// 作者:Win Lai
// 创建日期:2003-12-11
// 修改日期:2003-12-18
//
// 在Delphi6.0编译通过
//
// 使用:
//
// 1.定义类对象。
// MyClass : TDBOperate;
//
// 2.分配空间给类对象。
// MyClass := TDBOperate.Create(nil);
//
// 3.建立数据库链接。
// MyClass.ConnectionStrings := '[数据库链接串]'
// MyClass.Connect
//
// 4.操作数据库。
// MyClass.SQLStrings := '[SQL语句]'
// MyClass.SQLExec
// MyClass.SQLOpen
//
// 5.链接数据库控件。
// MyClass.DataSource.DateSet := MyClass.ADOTable;
// [数据库控件].DataSource := MyClass.DataSource;
//
// 6.释放类对象。
// MyClass.Free;
//
//-------------------------------------------------------------------------unit DBOperate;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ADODB, DB;type
TDBOperate = class(TObject) private
FADOConnection: TADOConnection;
FADOQuery: TADOQuery;
FADOTable: TADOTable;
FDataSource: TDataSource; FConnectionStrings: string;
FSQLStrings: string;
protected public
constructor Create(AOwner: TComponent); virtual;
destructor Destroy; override; procedure Connect;
procedure SQLExec;
procedure SQLOpen; function Disconnect: boolean;
function IsConnected: boolean; property ADOConnection: TADOConnection read FADOConnection write FADOConnection;
property ADOQuery: TADOQuery read FADOQuery write FADOQuery;
property ADOTable: TADOTable read FADOTable write FADOTable;
property DataSource: TDataSource read FDataSource write FDataSource;
property ConnectionStrings: string read FConnectionStrings write FConnectionStrings;
property SQLStrings: string read FSQLStrings write FSQLStrings;
end;implementation//-------------------------------------------------------------------------
// 构造函数
constructor TDBOperate.Create(AOwner: TComponent);
begin
inherited Create();
//..
FADOConnection := TADOConnection.Create(nil);
FADOQuery := TADOQuery.Create(nil);
FADOTable := TADOTable.Create(nil);
FDataSource := TDataSource.Create(nil); FADOConnection.LoginPrompt := false; // 不显示登录框
end;//-------------------------------------------------------------------------
// 析构函数
destructor TDBOperate.Destroy;
begin
//..
Disconnect;
FADOConnection.Free;
FADOQuery.Free;
FADOTable.Free;
FDataSource.Free; inherited Destroy;
end;//-------------------------------------------------------------------------
// 链接数据库
procedure TDBOperate.Connect;
begin
// 已经链接
if IsConnected=true then
begin
exit
end; try
FADOQuery.Connection := FADOConnection;
FADOTable.Connection := FADOConnection; FADOConnection.Close;
FADOConnection.ConnectionString := FConnectionStrings;
FADOConnection.Open; if FADOConnection.Connected = false then
begin
// 链接数据库失败
Application.MessageBox('链接数据库失败!', '错误', MB_OK OR MB_ICONERROR);
Application.Terminate;
end;
except
Application.MessageBox('链接数据库失败!', '错误', MB_OK OR MB_ICONERROR);
Application.Terminate;
end;
end;//-------------------------------------------------------------------------
// 断开链接数据库
function TDBOperate.Disconnect: boolean;
begin
// 已经断开链接
if IsConnected=false then
begin
Result := true;
exit;
end; try
FADOConnection.Close;
Result := true;
except
Result := false;
end;
end;//-------------------------------------------------------------------------
// 检查是否链接数据库
function TDBOperate.IsConnected: boolean;
begin
if ADOConnection.Connected = false then
Result := false
else
Result := true;
end;//-------------------------------------------------------------------------
// 执行SQL语句,不返回数据集
procedure TDBOperate.SQLExec;
begin
FADOQuery.Close;
FADOQuery.SQL.Text := FSQLStrings;
FADOQuery.ExecSQL;
end;//-------------------------------------------------------------------------
// 执行SQL语句,可返回数据集
procedure TDBOperate.SQLOpen;
begin
FADOQuery.Close;
FADOQuery.SQL.Text := FSQLStrings;
FADOQuery.Open;
end;//-------------------------------------------------------------------------
end.