unit Mainunit;
interface
uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs,
  ExtCtrls, DB, Grids, DBGrids, ADODB,dmunit, StdCtrls, Buttons;
type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    ADOQuery1: TADOQuery;
    dbgrd1: TDBGrid;
    DataSource1: TDataSource;
    btnquery: TButton;
    lbledtusername: TLabeledEdit;
    Panel3: TPanel;
    btnadd: TBitBtn;
    btnmodify: TBitBtn;
    btndelete: TBitBtn;
    lbledtDepart: TLabeledEdit;
    procedure btnqueryClick(Sender: TObject);
    procedure btnmodifyClick(Sender: TObject);
    procedure btndeleteClick(Sender: TObject);
    procedure btnaddClick(Sender: TObject);
    procedure lbledtDepartKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
  private
  public
  end;
var
  Form1: TForm1;
implementation
uses blluserinfo,dzmodeluserinfo, AddUnit, ModifyUnit;
{$R *.dfm}
procedure TForm1.btnaddClick(Sender: TObject);
var bll:blluserinfo.TBLLUserInfo ;model:TmodelUserinfo;
begin
  if addform.showmodal=mrok then
  begin
    bll:=TBLLUserInfo.Create(DataModule1.con1);
    model:=TmodelUserinfo.Create ;
    with Addform do begin
      Model.UserName.Value:=lbledtusername.Text;
      Model.Password.Value:=lbledtpass.Text;
      Model.Nickname.value:=lbledtnickname.Text;
      model.HeadImg.Value:=lbledthead.Text;
      Model.Sex.Value:=lbledtsex.Text;
      Model.Birthday.Value:=lbledtbirthday.Text;
      Model.DepartMent.value:=lbledtDepart.Text;
      model.JobType.Value:=lbledtJobType.Text;
      Model.Edu.Value :=LbledtEdu.Text;
      Model.Personality.Value:=lbledtPersonality.Text;
      Model.PersonHobbie.value:=lbledtPersonHobbie.Text;
      Model.PersonSign.Value:=lbledtpersonsign.Text;
      Model.Email.value:=lbledtemail.Text;
      Model.PersonPhone.Value:=lbledtphone.Text;
      Model.Remark.Value:=mmoRemark.Text;
    end;
    if bll.Add(model)then showmessage('新增成功！');     
  end;
end;

procedure TForm1.btnmodifyClick(Sender: TObject);
var bll:blluserinfo.TBLLUserInfo ;
begin
  if self.ADOQuery1.RecordCount <1 then
  exit;
  bll:=TBLLUserInfo.Create(DataModule1.con1);
  Modifyform.Model:=Bll.GetModel(AdoQuery1.FieldByName('UserName').AsString);
  //调用逻辑层的方法获取实体对象   
  if ModifyForm.ShowModal=mrok then
  if bll.Update(Adoquery1.FieldByName('Username').asstring,ModifyForm.Model )then
  Showmessage('修改成功！');
  //调用逻辑层的方法修改入库
  Bll.Free;//调用完毕后释放掉  
end;

procedure TForm1.btndeleteClick(Sender: TObject);
var bll:blluserinfo.TBLLUserInfo ;
begin
  if self.ADOQuery1.RecordCount <1 then
  exit;
  if messagebox(self.handle,'确定要删除当前记录吗？','提示',MB_OKCANCEL or
   MB_ICONQUESTION)=id_ok then
  self.ADOQuery1.Delete ;//adoquery1控件的删除方法，
  //也可以通过下面的逻辑调用方式删除
  {bll:=TBLLUserInfo.Create(DataModule1.con1);
  if bll.DeleteModel(Adoquery1.FieldByName('Username').AsString ) then
  showmessage('删除成功!');
  bll.Free;}
end;

procedure TForm1.btnqueryClick(Sender: TObject);
var bll:blluserinfo.TBLLUserInfo ;
strwhere:string;
begin
  bll:=TBLLUserInfo.Create(DataModule1.con1);
  Strwhere:=' (1=1)';
  if trim(lbledtusername.Text)<>'' then
  strwhere:=strwhere+' and (Username='''+Self.lbledtusername.Text+''')';
  if Trim(self.lbledtDepart.Text)<>'' then
  Strwhere:=strwhere+' and (DepartMent='''+self.lbledtDepart.Text+''')';
  bll.GetDataSet(Strwhere,self.ADOQuery1);
  bll.Free;   
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
{dmunit.DataModule1.con1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=127.0.0.1;Initial Catalog=demo;';
dmunit.DataModule1.con1.Connected:=true;//首先建立数据库连接
}
end;

procedure TForm1.lbledtDepartKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_return then
  self.btnqueryClick(sender); 
end;

end.
