码迷,mamicode.com
首页 > 数据库 > 详细

自动创建数据库(DELPHI+SQL SERVER)

时间:2019-07-23 00:28:07      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:lse   focus   and   原因   ted   prompt   led   server   建立数据库   

  procedure TForm1.Btn_OKClick(Sender: TObject);
var sqlconn:string;
begin
  Sqlconn:=Provider=SQLOLEDB.1;+password=+Edit_Password.Text
  +;Persist Security Info=true;User ID=+Edit_Name.Text
  +;Data Source=+Edit_Server.Text;
  if Edit_Server.Text=EmptyStr then begin
    showmessage(请输入SQL服务器的名称或者IP地址!);
    Edit_Server.SetFocus;
    exit;
  end;
  if Edit_Name.Text=EmptyStr then begin
    ShowMessage(请输入登陆数据库的用户名!);
    Edit_Name.SetFocus;
    exit;
  end;

  with ADO_Test do
    begin
      Close;
      ConnectionString :=Sqlconn;
    end;
   Try
    Try
      Ado_Test.LoginPrompt:=false;
      Ado_Test.Connected:=true;
      Messagebox(Handle,连接成功。,提示,MB_OK or MB_ICONINFORMATION);
      Button2.Click;
    except
      Messagebox(Handle,Pchar(连接[+Edit_Server.Text+]失败!),警告,MB_OK or MB_ICONWARNING);
    end;
   Finally
    Ado_Test.Connected:=false;
   end;
end;

procedure TForm1.Button2Click(Sender: TObject);
Var
  memExec,s:TStringList;
  I,J:integer;
  strSql,sqlstr,sqlconnstr:string;

begin
 Sqlconnstr:=IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N‘‘
   + ToolManage‘‘+ ) DROP DATABASE [ToolManage];
//不使用路径选择用缺省的

 sqlstr:=create database ToolManage ON (NAME = N‘‘+ToolManage_Data‘‘
     +, FILENAME = N‘‘+ExtractFilePath(Application.ExeName)+data\ToolManage_Data.MDF‘‘
     + , SIZE = 1, FILEGROWTH = 10%) LOG ON (NAME = N‘‘+ToolManage_Log‘‘+, FILENAME = N‘‘
     + ExtractFilePath(Application.ExeName)+data\ToolManage_Log.LDF‘‘
     + , SIZE = 1, FILEGROWTH = 10%) COLLATE Chinese_PRC_CI_AS;
 Try
//   Ado_CreateSQL.SQL.Clear; //清除创建的ado的sql
//   //建立数据库Edit_database
   Ado_CreateSQL.Connection:=Ado_Test;
   Ado_CreateSQL.Close;
   Ado_CreateSQL.SQL.Add(use master);
   Ado_CreateSQL.ExecSQL; //执行

    Ado_CreateSQL.Close;
    Ado_CreateSQL.SQL.Add(Sqlconnstr);
    Ado_CreateSQL.ExecSQL; //执行

    Ado_CreateSQL.Close;
    Ado_CreateSQL.SQL.Add(sqlstr);
    Ado_CreateSQL.ExecSQL; //执行

  Ado_CreateSQL.Close;
  Ado_CreateSQL.SQL.Add(use ToolManage);
  Ado_CreateSQL.ExecSQL; //执行

  memExec := TStringList.Create;
  s := TStringList.Create;
  try
   memExec.LoadFromFile(ExtractFilePath(Application.ExeName) + \data\sql.sql);
   s.Clear;
   for i := 0 to memExec.Count - 1 do
   begin
    if UpperCase(Trim(memExec.Strings[i])) <> ‘‘ then
    begin
     if UpperCase(Trim(memExec.Strings[i])) <> GO then
      s.Add(memExec.Strings[i])
     else if UpperCase(Trim(memExec.Strings[i])) = GO then
     begin
      try
       strSql := s.Text;
       with QryAll do
       begin
        Close;
        SQL.Text := strSql;
        ExecSQL;
       end;
       s.Clear;
      except
       s.Clear;
       ShowMessage(strSql);
      end;
     end;
    end;
   end;
   ShowMessage(创建 ToolManage 数据库成功!);


    with adocommand1 do
    try
     screen.Cursor := crSqlWait;
     try
      Ado_Test.Connected := false;

      CommandTExt := use master;
      execute;
      CommandText := ALTER DATABASE ToolManage SET OFFLINE WITH ROLLBACK IMMEDIATE;
      execute;
      CommandText := restore DataBase ToolManage from disk=‘‘
        + ExtractFilePath(Application.ExeName)+backup\2008-6-5(nil).bak‘‘
        +  with Replace;
      execute;
      CommandText := ALTER DATABASE ToolManage SET ONLINE WITH ROLLBACK IMMEDIATE;
      execute;
      CommandText:= Use ToolManage;
      execute;

      Ado_Test.Connected := true;
      CommandText :=Use ToolManage;
      execute;
      ShowMessage(初始化数据库成功!);
     finally
      screen.Cursor := crDefault;
     end;
    except
    on e: exception do
     ShowMessage(初始化数据库失败!原因是:  +  e.Message);
    end;

   DataM.UserName := Trim(Edit_Name.Text);
   DataM.UserPassword := Trim(Edit_Password.Text);
   DataM.Server := Trim(Edit_Server.Text);
   Ado_Test.Connected :=false;
  finally
   memExec.Free;
   s.Free;
  end;
 except
  DataM.UserName := ‘‘;
  DataM.UserPassword := ‘‘;
  DataM.Server := ‘‘;
  close;
  Exit;
 End;
 close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Form1:=nil;
 Action := caFree;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
 Edit_Server.Text := 127.0.0.1;
end;

 

自动创建数据库(DELPHI+SQL SERVER)

标签:lse   focus   and   原因   ted   prompt   led   server   建立数据库   

原文地址:https://www.cnblogs.com/jijm123/p/11229026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!