Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2006, 12:31 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 176
Thanks: 0
Thanked 0 Times in 0 Posts
Default back up sql data base

hi
i want to back up my data and save it to my hard disk and restore it again by C#
please help me

best regards
kiani mehdi
 
Old February 9th, 2007, 03:35 PM
Authorized User
 
Join Date: Oct 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to evandro.paula
Default

Hi,

You can create a command and execute the "BACKUP DATABASE". Take a look at the following sample:

            System.Data.SqlClient.SqlConnection connection = null;

            try
            {
                connection = new System.Data.SqlClient.SqlConnection();
                connection.ConnectionString = "Server=<server>;Initial Catalog=<db>;Integrated Security=SSPI; User Id=<user>;Password=<password>";

                System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = @"BACKUP DATABASE <db> TO DISK='C:\filename.bak'";
                command.Connection = connection;
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            finally
            {
                if ((connection != null) && (connection.State != System.Data.ConnectionState.Closed))
                {
                    connection.Close();
                    connection.Dispose();
                }
            }

Regards.
 
Old February 10th, 2007, 01:54 PM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 176
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
thank's so much for your usefull help

can you tell me how i can restore it from c#.net ?

bet regards
kiani
 
Old February 11th, 2007, 10:49 AM
Friend of Wrox
 
Join Date: Dec 2006
Posts: 176
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
thank you
and about restoring database i can restor database in fllowing steps
1)i set the data base to single_user then
2)i restor database with restore database SP
3)and finally i set database to multiple user

but in this way i have to write Application.Exitthread() at the end of step 3 becaus my app began hang up

what is the problem?
is there a better way for restoring database?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Data base Format virus220 ASP.NET 2.0 Professional 0 August 10th, 2007 09:37 AM
Data base connectivity watashi C# 2005 8 July 18th, 2007 08:05 PM
Transfering data from csv file to data base g_vamsi_krish ASP.NET 1.0 and 1.1 Professional 2 May 16th, 2006 11:58 PM
Data Base Connection ashraf Javascript 4 November 27th, 2004 06:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.