⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 清华大学出版社出版的数据库系统教程附带的实验教程中所有的源代码
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls,Inifiles, Buttons;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    btncreatedb: TBitBtn;
     procedure btncreatedbClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}




procedure TForm1.btncreatedbClick(Sender: TObject);
Var
 strcon,server,user,psw,dbpath,Restrostr:string;
 MyInifile:Tinifile;
begin

  MyInifile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'db_login.ini');
  server:=MyInifile.ReadString('sysdb','hostname','localhost');
  user:=MyInifile.ReadString('sysdb','UserName','sa');
  psw:=MyInifile.ReadString('sysdb','PassWord','1234');
  strcon:='Provider=SQLOLEDB.1;Password='+trim(psw)+ ';Persist Security Info=True;User ID='+trim(user) + ';Initial Catalog=;Data Source='+trim(server);
  ADOConnection1.close;
  ADOConnection1.ConnectionString:=strcon;
  try
  ADOConnection1.Open;
  //MessageBox(0,'数据库连接成功','提示',MB_OK+MB_iconinformation);
except
begin
  MessageBox(0,'数据库连接失败','警告',MB_OK+MB_iconwarning);
  exit;
end;
end;
//----------------创建数据库
try

  dbpath:=ExtractFilePath(Application.ExeName);
  //RestorStr := 'restore DATABASE children from disk= ''' + trim(dbpath) + ''' ';
  Restrostr:='begin';
  ADOQuery1.SQL.Text:=ADOQuery1.SQL.Text+'begin';
  Restrostr:='exec sp_attach_db @dbname = N' + '''' +'children'+ '''' +',';
  ADOQuery1.SQL.Text:=ADOQuery1.SQL.Text+Restrostr;

             //@filename1 = N'F:\msdeCon\data\corrosion_Data.mdf',
  Restrostr:='@filename1 = N'+''''+ dbpath+'data\children_Data.mdf'+'''' +',';
  ADOQuery1.SQL.Text:=ADOQuery1.SQL.Text+Restrostr;


  Restrostr:='@filename2 = N'+''''+ dbpath+'data\children_log.ldf'+'''';
  ADOQuery1.SQL.Text:=ADOQuery1.SQL.Text+Restrostr;


  ADOQuery1.SQL.Text:=ADOQuery1.SQL.Text+'end';

  ADOQuery1.ExecSQL;
  MessageBox(0,'数据库创建成功','提示',MB_OK+MB_iconinformation);
  close;
except
begin
  MessageBox(0,'数据库创建失败','警告',MB_OK+MB_iconwarning);
  exit;
end;
end; //try


end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -