Solved

How to connect SQL database to a certain Server

Posted on 2008-11-03
3
195 Views
Last Modified: 2010-04-21
Hi,

After installing my application including SQL Server 2005, the database will be created.
How can I connect the created SQL database to the SQL Sever from with Delphi (by code)?

Regards,
Stef
0
Comment
Question by:Delphiwizard
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Johnjces earned 500 total points
ID: 22867064
Here is some code  I got somewhere, (credit is noted) that uses ADO. Hopefully you are using ADO.

John


/************************************

//Attach and detach an MSSQL database

//or MSDE using ADO in Delphi 7.

//

//by Joseph Fuchaka Barasa

//jfuchakab@yahoo.com

//

//Helps one to work with database objects

//(tables, queries, stored procedures

//etc) using free version of MSDE.

//without using the costly Enterprise

//version of the database.

************************************/

procedure detach_database;

var

strcommand, detachedDB: string;

begin
 

  detachedDB:=trim(EditDetachedBName.Text);//database name

  strcommand:='EXEC sp_detach_db @dbname = '+detachedDB+'';//detaching

  with AttachConn do begin//AttachConn is ADOConnection

    close;//attach on the master system database.

    //computer_name is the name of your computer

    ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='+'master'+';Data Source='+computer_name+'';

    open;

  end;//with attachconn

   with qryDetach do begin//qryDetach is AdoQuery

        close;

        sql.Clear;

        sql.Text:=strcommand;

        try

          ExecSQL;

          ShowMessage('Detach was Successfull');

        Except on E:Exception do begin

          Showmessage('Error Dettaching the Database');

          Application.Terminate;

        end;

        end;//try - except

   end;

   end;//detach
 
 
 

procedure attach_database;

var

  strcommand, dbase_Name, file1, file2: string;
 

begin

  //database name

  dbase_Name:=trim(EditAttachedDBase.Text);

  file1:=trim(EditDatabaseFIle.Text);//database file

  file2:=trim(EditLogFile.Text);//log file

  with AttachConn do begin

    close;//connect to master system database

    ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='+'master'+';Data Source='+computer_name+'';

    open;

  end;//with attachconn
 

  //sql commands for attaching

  strcommand:='EXEC sp_attach_db @dbname = '+dbase_Name+', ';

  strcommand:=strcommand + '@filename1 = "'+File1+'"'+', ';

  strcommand:=strcommand + '@filename2 = "'+File2+'"'+'';

   with qryAttach do begin

        close;

        sql.Clear;

        sql.Text:=strcommand;

        try

          ExecSQL;

          ShowMessage('Database Successfully attached');

        Except on E:Exception do begin

          Showmessage('Error Attaching the Database');

          Application.Terminate;

        end;

        end;//try - except

   end;//with query attach
 

end; 

Open in new window

0
 

Author Closing Comment

by:Delphiwizard
ID: 31512660
Thank you very much!
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 22867347
Glad I could help this AM! Thanks!

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 50
Working with hours 3 46
MS SQL order by with "over" statement and row_number() 11 39
Unable to save view in SSMS 21 57
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now