RSS订阅 | 匿名投稿
您的位置:网站首页 > 数据库 > 正文

DELPHI ADO连接数据库

作者:admin 来源: 日期:2019/4/13 9:38:52 人气: 标签:

说明:代码都是工程里拷贝出来的,一定好用,但可能引用了无用的单元。不追求效率,能完成就可以。其它连接数据库的控件,修改一下就可以。ANDROID上连接SQLITE用的TFDConnection这个控件,也可以写成这样的方式。

一、新建立一个TDataModule单元(个人理解,就是没有界面,方便管理)

二、放一个TADOConnection控件

三、上代码(这个是连接ACCESS的)

unit data_mouble;
 
interface
 
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, Data.Win.ADODB;
 
type
  TDataModule1 = class(TDataModule)
    con_DB: TADOConnection;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  DataModule1: TDataModule1;
 
implementation
 
{%CLASSGROUP 'Vcl.Controls.TControl'}
 
{$R *.dfm}
 
procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
  condbstr: string;
 
begin
  try           
    condbstr := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;Persist Security Info=False';
    con_DB.ConnectionString := condbstr;
    con_DB.LoginPrompt := False;
    con_DB.Connected := true;
  except
    Application.MessageBox('网络数据库连接失败,请检测网络!', '提示信息', MB_OK + MB_ICONINFORMATION);
    ExitProcess(0);
  end;
end;
 
end.
四、新建一个空白单元(用来保存操作数据库的小函数)

unit fun_data;
 
 
{************************************************************}
{                                                            }
{           模块名称:数据库操作函数模块                                }
{                                                            }
{           技术支持:                                       }
{                                                            }
{                                                            }
{************************************************************}
 
 
interface
uses ADODB, Windows, ComCtrls, SysUtils,
  Math,data_mouble,System.Classes,Data.DB;
function DB_CheckEmpty(TabName: string): Boolean;
 
function DB_ExecSql(Str_Sql: string): Boolean;
 
function DB_SelectRecord(Str_Sql: string): TADOQuery;
function DB_insert_photo(Str_Sql: string;ms:TMemoryStream;file_name:string): Boolean;
 
 
implementation
{-------------------------------------------------------------------------------
  函数描述:  返回一个数据集
  过程名:    DB_SelectRecord
  作者:      fkwbllby
  日期:      2010.10.16
  参数:      Str_Sql:string
  返回值:    TADOQuery
-------------------------------------------------------------------------------}
 
function DB_SelectRecord(Str_Sql: string): TADOQuery;
var Ret: TADOQuery;
begin
  try
    Ret := TADOQuery.Create(nil);
    Ret.Connection := data_mouble.DataModule1.con_DB;
    Ret.Close;
    Ret.SQL.Clear;
    Ret.SQL.Add(Str_Sql);
    Ret.Open;
    DB_SelectRecord := Ret;
  except
    DB_SelectRecord := nil;
    Exit;
  end;
end;
 
 
 
 
function DB_CheckEmpty(TabName: string): Boolean;
var str: string;
  qry_temp: TADOQuery;
begin
  try
    str := 'SELECT * FROM ' + TabName;
    qry_temp := DB_SelectRecord(str);
    if qry_temp.IsEmpty then
      Result := False
    else
      Result := True;
    qry_temp.Free;
  except
    Result := False;
    Exit;
  end;
end;
 
{-------------------------------------------------------------------------------
  函数描述:  执行SQL语句
  过程名:    DB_ExecSql
  作者:      fkwbllby
  日期:      2010.10.16
  参数:      Str_Sql:string
  返回值:    Boolean
-------------------------------------------------------------------------------}
 
function DB_ExecSql(Str_Sql: string): Boolean;
var
  qry: TADOQuery;
begin
  try
    qry := TADOQuery.Create(nil);
    qry.Connection := data_mouble.DataModule1.con_DB;
    qry.Close;
    qry.SQL.Clear;
    qry.SQL.Add(Str_Sql);
    qry.ExecSQL;
    Result := True;
    qry.Free;
  except
    Result := False
  end;
end;
 
{-------------------------------------------------------------------------------
  函数描述:  插入SQL语句(带一张图片)
  过程名:    DB_insert_photo
  作者:      fkwbllby
  日期:      2010.10.16
  参数:      Str_Sql:string(SQL字符串前半部分)ms:TMemoryStream(图片内存流)file_name:string(图片字段名)
  返回值:    Boolean
-------------------------------------------------------------------------------}
 
function DB_insert_photo(Str_Sql: string;ms:TMemoryStream;file_name:string): Boolean;
var
  qry: TADOQuery;
begin
 
 
  try
    qry := TADOQuery.Create(nil);
    qry.Connection := data_mouble.DataModule1.con_DB;
    qry.Close;
    qry.SQL.Clear;
    qry.SQL.Add(Str_Sql);
    qry.Parameters.ParamByName(file_name).LoadFromStream(ms,ftBlob);
    qry.ExecSQL;
    Result := True;
    qry.Free;
  except
    Result := False
  end;
end;
 
 
 
 
 
end.
五、调用代码未例

unit login;
 
interface
 
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, Data.Win.ADODB,
  fun_data, Vcl.Buttons, Vcl.Imaging.pngimage, Vcl.ExtCtrls;
 
type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    Image1: TImage;
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
 
  private
 
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
uses main_all;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  sqltemp: string;
  qrytemp: TADOQuery;
begin
  if (ComboBox1.Text = '') or (Edit2.Text = '') then
  begin
    MessageBox(0, '用户名或密码不能为空', '提示', MB_TASKMODAL);
  end
  else
  begin
    sqltemp := 'select  * from login_user where username=' + char(39) +
      ComboBox1.Text + char(39) + ' and userpass=' + char(39) + Edit2.Text
      + char(39);
    qrytemp := DB_SelectRecord(sqltemp);
    if not qrytemp.Eof then
    begin
 
      // Form2.ShowModal;
      MessageBox(0, '登录成功', '提示', MB_TASKMODAL);
      // 需要----格式化权限
      user_modi := '0';
      Placing_area_modi := '0';
      Basic_equipment_modi := '0';
      Device_mess_modi := '0';
      now_username := ComboBox1.Text;
 
      user_modi := copy(qrytemp.fieldByname('quanxian').AsString, 1, 1);
      Placing_area_modi := copy(qrytemp.fieldByname('quanxian').AsString, 2, 1);
      Basic_equipment_modi :=
        copy(qrytemp.fieldByname('quanxian').AsString, 3, 1);
      Device_mess_modi := copy(qrytemp.fieldByname('quanxian').AsString, 4, 1);
      if Placing_area_modi = '0' then
      begin
        form2.is_del.Enabled := false;
        form2.is_modi.Enabled := false;
      end;
      Form1.hide;
      form2.Show;
 
    end
    else
    begin
      MessageBox(0, '请检查用户名和密码是否正确', '提示', MB_TASKMODAL);
      Edit2.Text := '';
    end
  end;
 
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  if MessageBox(Handle, PChar('        是否退出'), PChar('提示'), MB_YESNO) = 6 then
  begin
    application.Terminate;
  end;
end;
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if MessageBox(Handle, PChar('        是否退出'), PChar('提示'), MB_YESNO) = 6 then
  begin
    application.Terminate;
  end
  else
  begin
    CanClose := false;
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  // setwindowlong(Form1.Handle, gwl_style, getwindowlong(Handle, gwl_style) and not ws_caption);
  // height := clientheight; // 隐藏标题栏
end;
 
procedure TForm1.FormShow(Sender: TObject);
var
  sqltemp: string;
  qrytemp: TADOQuery;
begin
  // 取login_user中的username 放入
  sqltemp := 'SELECT * FROM login_user';
  // 'select * from mess where name=' + char(39) + ListView2.Items.Item[n].Caption + char(39) + ' and type=' + chr(39) + ListView2.Items.Item[n].SubItems[0] + chr(39);
  qrytemp := DB_SelectRecord(sqltemp);
  ComboBox1.Clear;
  if not qrytemp.Eof then
  begin
    while not qrytemp.Eof do
    begin
      ComboBox1.Items.Add(qrytemp.fieldByname('username').AsString);
      qrytemp.Next;
    end;
    combobox1.ItemIndex:=0;
  end
  else
  begin
 
  end;
 
end;
 
end.
六、界面截图



七、数据库截图





八、小技巧

1、EDIT中的PASSWORDCHAR属性写上个*号,就是密码隐藏

2、COMBOBOX的STYLE属性选择csDropDownList,只能选择,不能写,如果要默认哪个值就修改ITEMindex
--------------------- 
作者:以后换名字 
原文:https://blog.csdn.net/weixin_44387646/article/details/87355469 


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: