Solved

Run DDL SQL Commands With Delphi

Posted on 2006-11-03
11
402 Views
Last Modified: 2010-04-05
hello
i am working with delphi2005 and SQL server 2000
i want run sql commands (CREATE DATABASE,CREATE TABLE and attach,backup,restore ...) with delphi in run time.
please help me step by step with a example
thanks a lot
0
Comment
Question by:mr_mpz
  • 5
  • 3
  • 3
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
depending on your database and your privileges
you can place the ddl-statment in a query and use the execSQL-method

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
>delphi2005 and SQL server 2000
never used one of them

meikl ;-)
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Use AdoCommand from Ado Components
or the SqlQuery from dbExpress
0
 
LVL 1

Author Comment

by:mr_mpz
Comment Utility
dear frined
i am not a beginer in delphi,i am working with delphi from ver 2 until ver2005.
but i don't need work with sql server,i work with sql server 2000 now and i want backup from database in runtime in delphi or attach or disattach and more.
i test it woth ADO too,but all my request not work correct
with query this command not work correct,please help me with correct sample
thanks
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
No one said in comments that you are beginner ;-)

anyway, as me and meikl told you, any query component will work, also I prefer to use AdoCommand with ado for DLL statement, for example to create database just do

   cmd1.CommandText := 'create database SqlDB';
   cmd1.Execute;

and you can do any other DLL as the same way,

BTW, the adoCommand above attached to connection which connect to sql server without specifing a database

HTH
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
guess a typo

DLL -> DDL

meikl ;-)
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Thanks Meikl :-)
0
 
LVL 1

Author Comment

by:mr_mpz
Comment Utility
dear friends
thanks from you
i test this way (adocommand) for create database but this way doesn't work for other command.
please say to me a example for Attach database and deattach
thanks a lot
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hi,

It's also easy :-)

here's two buttons, one will deattach the database and the other to reattach it again

procedure TForm8.btnDeattachDBClick(Sender: TObject);
begin
  cmd1.CommandText :='sp_detach_db "pubs"';
  cmd1.Execute;
end;

procedure TForm8.btnAttachDBClick(Sender: TObject);
begin
  cmd1.CommandText :='sp_attach_single_file_db @dbname = "pubs", ' +
          ' @physname = "C:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf"';
  cmd1.Execute;
end;

Regards,
Mohammed
0
 
LVL 1

Author Comment

by:mr_mpz
Comment Utility
dear mohammed
thanks from your answer,this work correct
in adocommand ,what is  connectionstring?
i want connect to sql server without ODBC
thanks
0
 
LVL 22

Accepted Solution

by:
mnasman earned 150 total points
Comment Utility
for windows integerated security
Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=pubs;

if you use sql server security

Provider=SQLOLEDB.1;user id=sa;password=password;Initial Catalog=pubs;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

12 Experts available now in Live!

Get 1:1 Help Now