Automatically create the database (DELPHI + SQL SERVER)

  Procedure TForm1.Btn_OKClick (Sender: TObject);
 var sqlConn: String ;
 the begin 
  sqlConn: = ' Provider = SQLOLEDB.1; ' + ' password = ' + Edit_Password.Text
   + ' ; = the Persist Security Info to true; the User ID = ' + Edit_Name.Text
   + ' ; the Source the Data = ' + Edit_Server.Text;
   IF Edit_Server.Text = EmptyStr the then  the begin 
    ShowMessage ( ' Please enter the name or IP address of the SQL server! ' );  
    Edit_Server.SetFocus;
    Exit; 
  End;
   IF Edit_Name.Text = EmptyStr the then  the begin 
    ShowMessage ( ' Please enter your user login name database! ' ); 
    Edit_Name.SetFocus; 
    Exit; 
  End ; 

  with ADO_Test do 
    the begin 
      use Close; 
      the ConnectionString: = sqlConn;
     End ; 
   the Try 
    the Try 
      Ado_Test.LoginPrompt: = to false; 
      Ado_Test.Connected: = to true; 
      Messagebox to (the Handle, " the connection is successful. ' , ' prompt ' , MB_OKor 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: = to true; 
      the CommandText: = ' the Use ToolManage ' ; 
      Execute; 
      the ShowMessage ( ' initialize the database successfully! ' );
      The finally 
      screen.Cursor: = crDefault;
      End ;
     the except 
    ON E: Exception do 
     the ShowMessage ( ' initialization failed database! The reason is: '   +   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;

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11229026.html