Delphi写的一个简单的数据库操作代码
unit DbContext; interface uses Windows, SysUtils, IniFiles, DB, ADODB, Dialogs; type TDbContext = class private _fileFullName: string; _connString: string; public IsOk: Boolean; Connection: TADOConnection; Query: TADOQuery; constructor Create(fileFullName: string); overload; constructor Create(); overload; destructor Destroy; override; end; implementation //带参构造函数 constructor TDbContext.Create(fileFullName: string); const connStrTemplate = 'Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=%s;Password=%s;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = %s)(PORT = %s)))(CONNECT_DATA = (SERVICE_NAME = %s)))'; var iniFile: TIniFile; ip, port, sid, userName, password, fileName, filePath: string; begin IsOk := False; _fileFullName := fileFullName; if FileExists(_fileFullName) then begin iniFile := TIniFile.Create(_fileFullName); ip := iniFile.ReadString('DataBase', 'ip', '127.0.0.1'); port := iniFile.ReadString('DataBase', 'port', '1521'); sid := iniFile.ReadString('DataBase', 'sid', 'orcl'); userName := iniFile.ReadString('DataBase', 'userName', 'admin'); password := iniFile.ReadString('DataBase', 'password', 'admin'); _connString := Format(connStrTemplate, [userName, password, ip, port, sid]); //如果未分配 if not Assigned(Connection) then Connection := TADOConnection.Create(nil); //打开连接 if not Connection.Connected then begin Connection.ConnectionString := _connString; Connection.KeepConnection := True; Connection.LoginPrompt := False; Connection.Open; end; Query := TADOQuery.Create(nil); Query.Connection := Connection; IsOk := True; end else showMessage('找不到dbConfig.ini配置文件'); end; //无参构造函数 constructor TDbContext.Create(); var filePath: string; fileFullName: string; begin filePath := ExtractFilePath(Paramstr(0)); fileFullName := filePath + 'dbConfig.ini'; Create(fileFullName); end; //析构函数 destructor TDbContext.Destroy; begin FreeAndNil(Query); FreeAndNil(Connection); //调用基类的析构函数 inherited Destroy; end; end.
unit Edit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, DBClient, DbContext, StdCtrls; type TEditForm = class(TForm) ds1: TDataSource; lblId: TLabel; lblObjKey: TLabel; lblObjValue: TLabel; lblObjValue2: TLabel; edtId: TEdit; edtObjKey: TEdit; edtObjValue: TEdit; mmoObjValue2: TMemo; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } _dbContex: TDbContext; _id: string; public { Public declarations } constructor Create(aOwner: TComponent; id: string); reintroduce; overload; end; implementation {$R *.dfm} constructor TEditForm.Create(aOwner: TComponent; id: string); begin _id := id; //调用基类的构造函数 inherited Create(aOwner); end; procedure TEditForm.FormCreate(Sender: TObject); var query: string; objValue2: string; begin //创建实例 _dbContex := TDbContext.Create(); //如果创建连接失败 if _dbContex.IsOk = False then begin Exit; end; ds1.DataSet := _dbContex.Query; query := 'select * from spareparts_cache where id=:id'; with _dbContex.Query do begin Close; SQL.Clear; Parameters.Clear; SQL.Add(query); Parameters.ParamByName('id').Value := _id; Open; edtId.Text := FieldByName('Id').Value; edtObjKey.Text := FieldByName('ObjKey').Value; edtObjValue.Text := FieldByName('ObjValue').Value; //blob大字段 objValue2:= TBlobField(FieldByName('ObjValue2')).AsString(); mmoObjValue2.Text:= objValue2; end; end; procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction); begin FreeAndNil(_dbContex); end; end.
dbConfig.ini:
[DataBase]
ip=xxx.xxx.xxx.xxx
port=1521
sid=xxx
password=xxx
userName=xxx
浙公网安备 33010602011771号