各位老师,能不能在delphi  中通过sql 语句 建立sql server2000 的数据库 bb 和 表t1

解决方案 »

  1.   

    1.
    with     query1     do   
      begin   
              close;   
              sql.clear;   
              sql.add('create   database     你的数据库名或变量');   
      //parambyname('变量名').asstring:=edit1.text;   
              execsql;   
      end;   
      如要检查数据库是否已存在,则需在第二行加入以下程序   
          close;   
          sql.Clear;   
          sql.Add('Select   *   from   SysDatabases   Where   Name=:pname');   
          parameters.ParamByName('pname').value:=edb.Text;   
          open;   
          if   recordcount>0   then   
          begin   
                  if     application.MessageBox('数据库已经存在,要强行删除原有数据库吗?',   
                            '警告',mb_iconwarning+mb_yesno)<>mryes   then     abort();   
                  close;   
                  sql.Clear;   
                  sql.Add('drop   database   '+edb.Text);   
                  execsql;   
          end;   
      2.
    unit   Unit3;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
          Dialogs,   StdCtrls,   DB,   ADODB;   
        
      const   
          TDATATYPE   :   array   [0..3]   of   string   =   ('char',   'numeric',   'datatime',   'ntext');   
          TDATATYPENAME   :   array   [0..3]   of   string   =('字符型',   '数值型',   '日期型',   '备注型');   
      type   
          TForm3   =   class(TForm)   
              Label1:   TLabel;   
              Label2:   TLabel;   
              Label3:   TLabel;   
              Edit1:   TEdit;   
              Edit2:   TEdit;   
              ComboBox1:   TComboBox;   
              Label4:   TLabel;   
              Edit3:   TEdit;   
              Label5:   TLabel;   
              Edit4:   TEdit;   
              Button1:   TButton;   
              Button2:   TButton;   
              Button3:   TButton;   
              ADOQuery1:   TADOQuery;   
              procedure   FormCreate(Sender:   TObject);   
              procedure   ComboBox1Change(Sender:   TObject);   
              procedure   Button1Click(Sender:   TObject);   
              procedure   Button2Click(Sender:   TObject);   
              procedure   Button3Click(Sender:   TObject);   
          private   
              {   Private   declarations   }   
              function   GetType:   String;   
              function   GetQuerStr:   String;   
          public   
              {   Public   declarations   }   
          end;   
        
      var   
          Form3:   TForm3;   
        
      implementation   
        
      uses   Unit2;   
        
      {$R   *.dfm}   
        
      procedure   TForm3.FormCreate(Sender:   TObject);   
      var   
          i:   Integer;   
      begin   
          for   i   :=   0   to   3   do   
          begin   
              ComboBox1.Items.Add(tdatatypename[i])   
          end;   
          ADOQuery1.Connection   :=   DM.ADOConnection1;   
          Label5.Visible   :=   False;   
          Edit4.Visible   :=   False;   
      end;   
        
      function   TForm3.GetType:   String;//此函数用的是SQL   Server数据库   
      begin   
          case   ComboBox1.ItemIndex   of   
              0:   Result   :=   'varchar';   
              1:   Result   :=   'numeric';   
              2:   Result   :=   'datetime';   
              3:   Result   :=   'ntext';   
          end;   
      end;   
        
      function   TForm3.GetQuerStr:   String;   //     此函数用的是ACCESS数据库   
      begin   
          case   ComboBox1.ItemIndex   of   
              0:   Result   :=   'Alter   table   diary   add   '   +   Edit1.Text   +   '   varchar('   +   Edit3.Text   +   ')'   +   'null';   
              1:   Result   :=   'alter   table   diary   add   '   +   Edit1.Text   +   '   float   '   +   '   null';   
              2:   Result   :=   'alter   table   diary   add   '   +   Edit1.Text   +   '   datetime'   +   'null';   
              3:   Result   :=   'alter   table   diary   add   '   +   Edit1.Text   +   '   text   '   +   'null';   
          end;   
          //删除字段的话得先把字段取出   然后用     alter   table   diary   drop   column   字段名   
      end;   
        
      procedure   TForm3.ComboBox1Change(Sender:   TObject);   
      begin   
          if   ComboBox1.ItemIndex   =   1   then   
          begin   
              Label5.Visible   :=   True;   
              Edit4.Visible   :=   True;   
          end   
          else   
          begin   
              Label5.Visible   :=   False;   
              Edit4.Visible   :=   False;   
          end;   
          if   (ComboBox1.ItemIndex   =   2)   or   (ComboBox1.ItemIndex   =   3)   then   
          begin   
              Label4.Visible   :=   False;   
              Edit3.Visible   :=   False;   
          end   
          else   
          begin   
              Label4.Visible   :=   True;   
              Edit3.Visible   :=   True;   
          end;   
      end;   
        
      procedure   TForm3.Button1Click(Sender:   TObject);   
      var   
          QuerStr:   String;   
      begin   
          QuerStr   :=   GetQuerStr;   
          with   ADOQuery1   do   
          begin   
              Close;   
              SQL.Clear;   
              SQL.Add(QuerStr);   
              try   
                  ExecSQL;   
              except   
                  ShowMessage('创建字段出错,请重新创建');   
                  Exit;   
              end;   
              if   Application.MessageBox('字段创建完成,是否继续','问题',MB_YESNO   +   MB_ICONQUESTION)   =   idyes   then   
              begin   
                  Edit1.Text   :=   '';   
                  Edit2.Text   :=   '';   
                  Edit3.Text   :=   '';   
                  Edit4.Text   :=   '';   
                  ComboBox1.Items.Text   :=   '';   
                  Edit1.SetFocus;   
              end;   
          end;   
      end;   
        
      procedure   TForm3.Button2Click(Sender:   TObject);   
      begin   
          Edit1.Text   :=   '';   
          Edit2.Text   :=   '';   
          Edit3.Text   :=   '';   
          Edit4.Text   :=   '';   
          ComboBox1.Items.Text   :=   '';   
          Edit1.SetFocus;   
      end;   
        
      procedure   TForm3.Button3Click(Sender:   TObject);   
      begin   
          Close;   
      end;   
        
      end.
    我在网上找的,自己捉么下
    http://topic.csdn.net/t/20030826/14/2188999.html