使用代码创建数据库
时间:2011-03-16 来源:巅枫
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, DBTables; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Query1: TQuery; Database1: TDatabase; Memo1: TMemo; Label1: TLabel; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin try with Query1 do begin close; SQL.clear; SQL.Add('Create Database newdata on'); //创建数据库 SQL.Add('(Name = SSS, Filename = "f:\db2010\newdata.mdf")'); ExecSQL; //执行SQL语句 end; Application.MessageBox('创建成功','提示',mb_ok); Except Application.MessageBox('创建失败','提示',mb_ok); close; end; end; procedure TForm1.Button2Click(Sender: TObject); begin close; end; procedure TForm1.FormCreate(Sender: TObject); begin Label1.Caption:='使用代码创建数据库'; Database1.AliasName:='DB2010'; //数据库别名 Database1.DatabaseName:='DB2010'; //指定数据库的名称,任意 Database1.Params.Add('User name=sa'); //用户名 Database1.Params.Add('Password='); //口令 Query1.DatabaseName:='DB2010'; //指定数据库的名称 end; end.
相关阅读 更多 +