• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

自动创建数据库(DELPHI+SQLSERVER)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  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;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
生成蜂窝结构的Matlab程序 - elapsetor发布时间:2022-07-18
下一篇:
LibSvm添加到Matlab发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap