如何编写用来执行sql的函数????

解决方案 »

  1.   

    要什么函数?直接在STRING类型中加就行了嘛。如:var sqlstr:string;
    sqlstr:=edit1.text;
    adoquery1.sql.add(sqlstr);
      

  2.   

    //-------------------------------------------------------------------------
    // 文件名: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.