تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
كل ما يخص SqlServer
#1
كود :
procedure CreateDatabase(WindowsSecurity: Boolean; Username, Password: String);
var
 ConnectionString: String;
 CommandText: String;
begin
 if WindowsSecurity then
   ConnectionString := 'Provider=SQLOLEDB.1;' +
                       'Integrated Security=SSPI;' +
                       'Persist Security Info=False;' +
                       'Initial Catalog=master'
 else
   ConnectionString := 'Provider=SQLOLEDB.1;' +
                       'Password=' + Password + ';' +
                       'Persist Security Info=True;' +
                       'User ID=' + Username + ';' +
                       'Initial Catalog=master';

 try

   try
     ADOConnection.ConnectionString := ConnectionString;
     ADOConnection.LoginPrompt := False;
     ADOConnection.Connected := True;


     CommandText := 'CREATE DATABASE test ON ' +
                    '( NAME = test_dat,    ' +
                    'FILENAME = ''c:\program files\microsoft sql server\mssql\data\test.mdf'', ' +
                    'SIZE = 4, ' +
                    'MAXSIZE = 10, ' +
                    'FILEGROWTH = 1 )';

     ADOCommand.CommandText := CommandText;
     ADOCommand.Connection := ADOConnection;
     ADOCommand.Execute;
     MessageDlg('Database succesfully created.', mtInformation, [mbOK], 0);

   except
     on E: Exception do MessageDlg(E.Message, mtWarning, [mbOK], 0);
   end;

 finally
   ADOConnection.Connected := False;
   ADOCommand.Connection := nil;
 end;

end;

Create tables on MS SQL Server


كود :
procedure TLocal.CreateTables(WindowsSecurity: Boolean; Username, Password: String);
var
 ConnectionString: String;
begin
 if WindowsSecurity then
   ConnectionString := 'Provider=SQLOLEDB.1;' +
                       'Integrated Security=SSPI;' +
                       'Persist Security Info=False;' +
                       'Initial Catalog=test'
 else
   ConnectionString := 'Provider=SQLOLEDB.1;' +
                       'Password=' + Password + ';' +
                       'Persist Security Info=True;' +
                       'User ID=' + Username + ';' +
                       'Initial Catalog=test';
 try

   try
     ADOConnection.ConnectionString := ConnectionString;
     ADOConnection.LoginPrompt := False;
     ADOConnection.Connected := True;

     ADOQuery.Connection := ADOConnection;

     ADOQuery.SQL.Clear;
     with ADOQuery.SQL do
     begin
       Add('create table Klijent(');
       Add('JMBG     char(13) not null,');
       Add('Ime      char(30) not null,');
       Add('Adresa   char(30) not null,');
       Add('Telefon  char(15) not null,');
       Add('Primanja numeric(6,2) not null,');
       Add('primary key (JMBG))');
     end;
     ADOQuery.ExecSQL;

     ADOQuery.SQL.Clear;
     with ADOQuery.SQL do
     begin
       Add('create table Kredit(');
       Add('Sifra    numeric not null,');
       Add('Tip      char(15) unique not null,');
       Add('Kamata   numeric not null,');
       Add('primary key (Sifra))');
     end;
     ADOQuery.ExecSQL;

     ADOQuery.SQL.Clear;
     with ADOQuery.SQL do
     begin
       Add('create table Operator(');
       Add('JMBG     char(13) unique not null,');
       Add('Ime      char(30) not null,');
       Add('Sifra    char(30) not null,');
       Add('Adresa   char(30) not null,');
       Add('Telefon  char(15) not null,');
       Add('Prioritet smallint not null check (Prioritet>0),');
       Add('primary key (JMBG))');
     end;
     ADOQuery.ExecSQL;

     ADOQuery.SQL.Clear;
     with ADOQuery.SQL do
     begin
       Add('create table Kreditiranja (');
       Add('Sifra          numeric not null,');
       Add('Sifra_kredita  numeric not null,');
       Add('Datum          datetime,');
       Add('Iznos_kredita  numeric(10,2) check (Iznos_kredita>0),');
       Add('Broj_rata      numeric,');
       Add('JMBG_klijenta  char(13),');
       Add('JMBG_operatora char(13),');
       Add('primary key(Sifra),');
       Add('foreign key(Sifra_kredita) references Kredit(Sifra) on delete cascade on update cascade,');
       Add('foreign key(JMBG_klijenta) references Klijent(JMBG) on delete cascade on update cascade,');
       Add('foreign key(JMBG_operatora) references Operator(JMBG) on delete cascade on update cascade)');
     end;
     ADOQuery.ExecSQL;

     ADOQuery.SQL.Clear;
     with ADOQuery.SQL do
     begin
       Add('create table Rata (');
       Add('Broj_rate    numeric not null,');
       Add('Broj_sifre   numeric not null,');
       Add('Datum        datetime,');
       Add('Iznos_rate   numeric(10,2) check (Iznos_rate>0),');
       Add('primary key (Broj_rate),');
       Add('foreign key (Broj_sifre) references Kreditiranja(Sifra) on delete cascade on update cascade)');
     end;
     ADOQuery.ExecSQL;

     MessageDlg('Tabele su uspjesno kreirane.', mtInformation, [mbOK], 0);
   except
     on E: Exception do MessageDlg(E.Message, mtWarning, [mbOK], 0);
   end;

 finally
   ADOConnection.Connected := False;
 end;
end;

Delete a User from adatabase in Sql Server

كود :
procedure TForm1.Button1Click(Sender: TObject);
begin
 ADOCommand1.CommandText := 'Use DataBaseName';
 ADOCommand1.Execute;
 ADOCommand1.CommandText := 'Exec SP_DropUser ' + QuotedStr('Username');
 ADOCommand1.Execute;
end;

Get List of Database in Sql Server


كود :
procedure TForm1.Button1Click(Sender: TObject);
begin
 ADOQuery1.SQL.Add('Exec SP_DATABASES');
 ADOQuery1.Active := True;
end;
Get List of Tables in Sql Server


كود :
procedure TForm1.Button1Click(Sender: TObject);
begin
 ADOQuery1.SQL.Add('Exec SP_Tables');
 ADOQuery1.Active := True;
end;
الرد }}}


الردود في هذا الموضوع
كل ما يخص SqlServer - بواسطة viv - 20-06-19, 02:21 AM
RE: Create database on local MS SQL Server - بواسطة viv - 20-06-19, 02:27 AM

التنقل السريع :


يقوم بقرائة الموضوع: