We help IT Professionals succeed at work.

How to use delphi to backup sqlserver database?

Sam80
Sam80 asked
on
3,031 Views
Last Modified: 2007-12-19
I use delphi for developing application and  sqlserver for database server.
and now I want to add one more  function that can backup the database automatically without operations in SQLserver console

any solutions or  codes samples?

Regards,
Sam
Comment
Watch Question

Backing up an entire database:

{u can write this in a query component-sql property, call it by "Query1.ExecSql" not "Query1.open"}

BACKUP DATABASE {database_name | @database_name_var}
TO <backup_device> [,...n]
[WITH
    [BLOCKSIZE = {blocksize | @blocksize_variable}]
    [[,] DESCRIPTION = {text | @text_variable}]
    [[,] DIFFERENTIAL]
    [[,] EXPIREDATE = {date | @date_var}
        | RETAINDAYS = {days | @days_var}]
    [[,] FORMAT | NOFORMAT]
    [[,] {INIT | NOINIT}]
    [[,] MEDIADESCRIPTION = {text | @text_variable}]
    [[,] MEDIANAME = {media_name | @media_name_variable}]
    [[,] [NAME = {backup_set_name | @backup_set_name_var}]
    [[,] {NOSKIP | SKIP}]
    [[,] {NOUNLOAD | UNLOAD}]
    [[,] [RESTART]
    [[,] STATS [= percentage]]
]

Backing up specific files or filegroups:

BACKUP DATABASE {database_name | @database_name_var}
    <file_or_filegroup> [,...n]
TO <backup_device> [,...n]
[WITH
    [BLOCKSIZE = {blocksize | @blocksize_variable}]
    [[,] DESCRIPTION = {text | @text_variable}]
    [[,] EXPIREDATE = {date | @date_var}
        | RETAINDAYS = {days | @days_var}]
    [[,] FORMAT | NOFORMAT]
    [[,] {INIT | NOINIT}]
    [[,] MEDIADESCRIPTION = {text | @text_variable}]
    [[,] MEDIANAME = {media_name | @media_name_variable}]
    [[,] [NAME = {backup_set_name | @backup_set_name_var}]
    [[,] {NOSKIP | SKIP}]
    [[,] {NOUNLOAD | UNLOAD}]
    [[,] [RESTART]
    [[,] STATS [= percentage]]
]

Backing up a transaction log:

BACKUP LOG {database_name | @database_name_var}
{
    [WITH
        { NO_LOG | TRUNCATE_ONLY }]
}
|
{
    TO <backup_device> [,...n]
    [WITH
        [BLOCKSIZE = {blocksize | @blocksize_variable}]
        [[,] DESCRIPTION = {text | @text_variable}]
        [[,] EXPIREDATE = {date | @date_var}
            | RETAINDAYS = {days | @days_var}]
        [[,] FORMAT | NOFORMAT]
        [[,] {INIT | NOINIT}]
        [[,] MEDIADESCRIPTION = {text | @text_variable}]
        [[,] MEDIANAME = {media_name | @media_name_variable}]
        [[,] [NAME = {backup_set_name | @backup_set_name_var}]
        [[,] NO_TRUNCATE]
        [[,] {NOSKIP | SKIP}]
        [[,] {NOUNLOAD | UNLOAD}]
        [[,] [RESTART]
        [[,] STATS [= percentage]]
    ]
}


<backup_device> ::=
    {
        {backup_device_name | @backup_device_name_var}
        |
        {DISK | TAPE | PIPE} =
            {'temp_backup_device' | @temp_backup_device_var}
    }


<file_or_filegroup> ::=
    {
        FILE = {logical_file_name | @logical_file_name_var}
        |
        FILEGROUP = {logical_filegroup_name | @logical_filegroup_name_var}
    }

Author

Commented:
yes ,well , thanks .
I want to back up the entire database, what is the back up database output format ? a file ?

I want to save those backup file in a specifed dir ,

and also , I 'd like to know how to revert  to the database that backuped before,according to your method.


Regards,
Sam
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Dear Sam,
i believe peter's method is very much effective and more powerful.. unless u cant install the SQL tools on the client PC.. other than that..it's just perfect!..

cheers,

Delphi_76

Author

Commented:
Thanks Delphi_76, Peter. thank you for all your helps.

Commented:
I see I forgot to explain FAppName which is used when doing the connection.
This is simply a string that is to hold a description/name of your application, this can also be empty.
It is included if the server administrator choose view connections.

If you want to be able to access the interface components (GUI) of Enterprise Manager,
then you need to use the SQLNamespace (like in my sample). SQLNamespace is then navigated to gain access
to the proper DMO object.

But if you want access to "under the hood" functions only (like making a backup
without showing any GUI), you may choose to create and access the SQLDMO objects directly instead.

There are plenty of documentation and samples on the internet featuring both cases. The language though is mostly C++ or Visual Basic.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.