系统外挂一子模块--数据库管理器主要实现的对数据库管理,功能应满足:新建数据库 引入数据库 导出数据库,修改口令
以及对数据库进行备份,恢复,删除等搞到一段代码,请大家分析一下先:如何利用delphi在sql server中建立数据库 //初始化数据库
procedure TCreateDBFrm.OKBtnClick(Sender: TObject);
var
RightConnection: Boolean;
StrLst: TStringList;
CreateDBSQL, tmpstr: string;
index: integer;
begin
with Self.ADOConnection do
begin
if Connected then
begin
Close;
end;
ConnectionString := Format(IniADOConnectionstring,
[Trim(UserNameComb.Text), QuotedStr(Trim(PasswordEdt.Text)),
Trim(ServersComb.Text)]);
RightConnection := True;
try
Open;
except
RightConnection := False;
Close;
sm.MessageDlg('用户名或密码错误,请重试!',
mtWarning, [mbOk], 0);
end;
end;
if RightConnection then
begin
WriteToReg(Trim(ServersComb.Text), Trim(UserNameComb.Text),
Trim(PasswordEdt.Text));
if CTChkBox.Checked then
begin
if not DirectoryExists(FFilePath + 'data') then
CreateDir(FFilePath + 'data');
CreateDBSQL := 'CREATE DATABASE [COMS] ON PRIMARY (NAME = N' +
QuotedStr('COMS') + ', FILENAME = N' + QuotedStr(FFilePath
+ 'data\COMS.MDF') + ', SIZE = 2, FILEGROWTH = 10%) ' +
'LOG ON (NAME = N' + QuotedStr('COMS_Log') + ', FILENAME' +
' = N' + QuotedStr(FFilePath + 'data\COMS_Log.LDF') +
', SIZE = 2, FILEGROWTH = 10%)';
try
try
Self.Caption := '正在初始化数据库...';
Self.Timer.Enabled := True;
Self.ADOCommand.CommandText := CreateDBSQL;
Self.ADOCommand.Execute;
except
if sm.MessageDlg('警告:数据库初始化将删除已有数据库' +
'的全部数据。' + #10#13 + '若想初始化数据库,请单击"确定"。' +
'若想退出,请单击"取消"', mtWarning, [mbOk, mbCancel], 0) = mrOk then
begin
Self.Refresh;
//如果数据库文件、日志文件和数据库没有分离,则将其分离;否则如果
//数据库文件、日志文件如果存在,则删除
try
//将数据库和数据库文件、日志文件分离
with Self.ADOCommand do
begin
CommandText := 'use master';
Execute;
CommandText := 'EXEC sp_detach_db ' + QuotedStr('COMS') + ', true';
Execute;
end;
except
//删除数据库文件、日志文件
if FileExists(FFilePath + 'data\COMS.MDF') then
DeleteFile(FFilePath + 'data\COMS.MDF');
if FileExists(FFilePath + 'data\COMS_Log.LDF') then
DeleteFile(FFilePath + 'data\COMS_Log.LDF');
end;
//删除数据库文件、日志文件
if FileExists(FFilePath + 'data\COMS.MDF') then
DeleteFile(FFilePath + 'data\COMS.MDF');
if FileExists(FFilePath + 'data\COMS_Log.LDF') then
DeleteFile(FFilePath + 'data\COMS_Log.LDF');
Self.ADOCommand.CommandText := CreateDBSQL;
Self.ADOCommand.Execute;
end;
CreateDBFrm.Refresh;
end;
finally
//通过读取COMS.SQl文件内容建立数据表,如果文件不存在,
//则按默认方式建立数据表
try
StrLst := TStringList.Create;
try
StrLst.LoadFromFile(FFilePath + 'SQL\COMS.SQL');
with Self.CreateTable do
begin
CommandText := StrLst.Text;
Execute;
end;
except
Self.CreateTable.Execute;
end;
//通过读取COMS.SQl文件内容建立数据表,如果文件不存在,
//则按默认方式建立数据表
StrLst.LoadFromFile(FFilePath + 'SQL\VIEW.SQL');
with Self.CreateTable do
begin
tmpstr := '';
for index := 0 to StrLst.Count - 1 do
begin
if Trim(StrLst.Strings[index]) <> '' then
begin
if (Pos('--', Trim(StrLst.Strings[index])) = 0) and
(index <= StrLst.Count - 1) then
tmpstr := tmpstr + ' ' + Trim(StrLst.Strings[index])
else if Trim(tmpstr) <> '' then
begin
CommandText := Trim(tmpstr);
Execute;
tmpstr := '';
end;
end;
end;
if Trim(tmpstr) <> '' then
begin
CommandText := Trim(tmpstr);
Execute;
tmpstr := '';
end;
end;
finally
StrLst.Free;
end;
Self.ADOConnection.Close;
end;
end else
Self.ADOConnection.Close;
Self.Hide;
sm.MessageDlg('初始化数据库成功!', mtInformation, [mbOk], 0);
Self.Close;
end;
//Self.Caption := '';
end;
以及对数据库进行备份,恢复,删除等搞到一段代码,请大家分析一下先:如何利用delphi在sql server中建立数据库 //初始化数据库
procedure TCreateDBFrm.OKBtnClick(Sender: TObject);
var
RightConnection: Boolean;
StrLst: TStringList;
CreateDBSQL, tmpstr: string;
index: integer;
begin
with Self.ADOConnection do
begin
if Connected then
begin
Close;
end;
ConnectionString := Format(IniADOConnectionstring,
[Trim(UserNameComb.Text), QuotedStr(Trim(PasswordEdt.Text)),
Trim(ServersComb.Text)]);
RightConnection := True;
try
Open;
except
RightConnection := False;
Close;
sm.MessageDlg('用户名或密码错误,请重试!',
mtWarning, [mbOk], 0);
end;
end;
if RightConnection then
begin
WriteToReg(Trim(ServersComb.Text), Trim(UserNameComb.Text),
Trim(PasswordEdt.Text));
if CTChkBox.Checked then
begin
if not DirectoryExists(FFilePath + 'data') then
CreateDir(FFilePath + 'data');
CreateDBSQL := 'CREATE DATABASE [COMS] ON PRIMARY (NAME = N' +
QuotedStr('COMS') + ', FILENAME = N' + QuotedStr(FFilePath
+ 'data\COMS.MDF') + ', SIZE = 2, FILEGROWTH = 10%) ' +
'LOG ON (NAME = N' + QuotedStr('COMS_Log') + ', FILENAME' +
' = N' + QuotedStr(FFilePath + 'data\COMS_Log.LDF') +
', SIZE = 2, FILEGROWTH = 10%)';
try
try
Self.Caption := '正在初始化数据库...';
Self.Timer.Enabled := True;
Self.ADOCommand.CommandText := CreateDBSQL;
Self.ADOCommand.Execute;
except
if sm.MessageDlg('警告:数据库初始化将删除已有数据库' +
'的全部数据。' + #10#13 + '若想初始化数据库,请单击"确定"。' +
'若想退出,请单击"取消"', mtWarning, [mbOk, mbCancel], 0) = mrOk then
begin
Self.Refresh;
//如果数据库文件、日志文件和数据库没有分离,则将其分离;否则如果
//数据库文件、日志文件如果存在,则删除
try
//将数据库和数据库文件、日志文件分离
with Self.ADOCommand do
begin
CommandText := 'use master';
Execute;
CommandText := 'EXEC sp_detach_db ' + QuotedStr('COMS') + ', true';
Execute;
end;
except
//删除数据库文件、日志文件
if FileExists(FFilePath + 'data\COMS.MDF') then
DeleteFile(FFilePath + 'data\COMS.MDF');
if FileExists(FFilePath + 'data\COMS_Log.LDF') then
DeleteFile(FFilePath + 'data\COMS_Log.LDF');
end;
//删除数据库文件、日志文件
if FileExists(FFilePath + 'data\COMS.MDF') then
DeleteFile(FFilePath + 'data\COMS.MDF');
if FileExists(FFilePath + 'data\COMS_Log.LDF') then
DeleteFile(FFilePath + 'data\COMS_Log.LDF');
Self.ADOCommand.CommandText := CreateDBSQL;
Self.ADOCommand.Execute;
end;
CreateDBFrm.Refresh;
end;
finally
//通过读取COMS.SQl文件内容建立数据表,如果文件不存在,
//则按默认方式建立数据表
try
StrLst := TStringList.Create;
try
StrLst.LoadFromFile(FFilePath + 'SQL\COMS.SQL');
with Self.CreateTable do
begin
CommandText := StrLst.Text;
Execute;
end;
except
Self.CreateTable.Execute;
end;
//通过读取COMS.SQl文件内容建立数据表,如果文件不存在,
//则按默认方式建立数据表
StrLst.LoadFromFile(FFilePath + 'SQL\VIEW.SQL');
with Self.CreateTable do
begin
tmpstr := '';
for index := 0 to StrLst.Count - 1 do
begin
if Trim(StrLst.Strings[index]) <> '' then
begin
if (Pos('--', Trim(StrLst.Strings[index])) = 0) and
(index <= StrLst.Count - 1) then
tmpstr := tmpstr + ' ' + Trim(StrLst.Strings[index])
else if Trim(tmpstr) <> '' then
begin
CommandText := Trim(tmpstr);
Execute;
tmpstr := '';
end;
end;
end;
if Trim(tmpstr) <> '' then
begin
CommandText := Trim(tmpstr);
Execute;
tmpstr := '';
end;
end;
finally
StrLst.Free;
end;
Self.ADOConnection.Close;
end;
end else
Self.ADOConnection.Close;
Self.Hide;
sm.MessageDlg('初始化数据库成功!', mtInformation, [mbOk], 0);
Self.Close;
end;
//Self.Caption := '';
end;
解决方案 »
- 系统热键 的问题
- 改变其他窗口透明度~
- 新手问什么是OLE
- 为什么我的fastreport显示不了总页数
- 请问delphi+ado+sqlserver2000的程序怎么打包发布?
- 关于文件夹改名的问题(程序有点问题):
- 打开窗体时提示Can't Create From "未指定的错误",Toggle Form/Unit时窗体无法显示,请大虾帮忙啊,谢谢!
- 有人用过TClientDataSet的RefreshRecord吗?我一用就出错!
- 如何与IE进行交互????
- 请教string和widestring的区别
- VCLSkin控件的菜单使用中文名称出奇的难看!有解决办法吗?
- 请问怎么刷新dbchart里的图形呀!!急!!在线等待!!
给我先