473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Backup / Restore And Unload database with asp.net

JustRun
127 100+
Hi,

I want to delete all data from tables when the user click on the unload button, I dont wanna make about 50 Delete Command to unload the data from database. is there any way to do this, regarding that i have forign keys.

As for the Backup and Restore to the system database, I mean i want to allow the user to select the path of Backup, and also from where he want to make the Restore.


I think it could be done with uploading form that takes paths from the user but actually i dont know the SQL statement for backup

I use SQL SERVER 2000
ASP.NET 2
C#

Thanks
Sep 23 '08 #1
9 2807
Curtis Rutland
3,256 Expert 2GB
What do you mean that you don't want to make 50 delete statements? Do you have 50 tables, or just 50 rows in one table? If it is the latter, one delete command will solve your problem.
Sep 23 '08 #2
JustRun
127 100+
I have about 50 Tables
Sep 23 '08 #3
JustRun
127 100+
Here is my code:
Expand|Select|Wrap|Line Numbers
  1.     SqlConnection cnn = new SqlConnection(DBconnection.GetConnection("LocalSqlServer"));
  2.     SqlCommand cmd;
  3.  
  4.     protected void Page_Load(object sender, EventArgs e)
  5.     {
  6.         try
  7.         {
  8.             cmd = new SqlCommand("DELETE FROM Table1 DELETE FROM Table2 DELETE FROM Table3 ...... ", cnn);
  9.             cnn.Open();
  10.  
  11.             if (cmd.ExecuteNonQuery() == 1)
  12.             {
  13.                 lblErr.Text = "You have successfully unload the system";
  14.                 //Response.Redirect("../Default.aspx");
  15.             }
  16.             else
  17.             {
  18.                 lblErr.Text = "An Error have been occured";
  19.             }
  20.         }
  21.         catch (Exception ex)
  22.         {
  23.             lblErr.Text = ex.Message.ToString();
  24.         }
  25.  
  26.         finally
  27.         {
  28.             if (cnn != null) cnn.Close();
  29.         }
  30.     }
  31.  
This code works fine and it affects the specified tables, But The browser display the message of Else "An Error have been occured"
I dont know why
Sep 23 '08 #4
Curtis Rutland
3,256 Expert 2GB
I think that is because ExecuteNonQuery returns the number of rows affected by your statement.
Sep 23 '08 #5
mldisibio
190 Expert 100+
Confirming what insertAlias said: ExecuteNonQuery returns number of rows affected, and in the case of your query, only the rows deleted from the last table.

Here are some recommendations, but you need to pull out SqlServer Books online and look this syntax up for yourself: allowing users to delete 50 tables en masse with a click of a web page is asking for trouble if you don't understand the syntax yourself:

1. Put the DELETE statement into a stored procedure. The web page will call only one stored procedure, which you can change as your database requirements change;

2. Your StoredProcedure (SP) can return a success or failure return value. It can return specific errors. It can decide whether to proceed, cancel or ROLLBACK if a table delete fails.

3. If you are just wiping out tables and do not care about ROLLBACK in case of failure, use TRUNCATE TABLE not DELETE, in order to efficiently not over use log files.

4. Within your SP, you can either hardcode your 50 TRUNCATE statements, which in your case you probably should do to avoid errors in using syntax you do not understand. This way you can also verify Foreign Key integrity. However, you could also write a cursor that gets all the table names from the system catalog (this catalog tables/views are different between SQL 2000 and 2005). You can either generate sql statements and execute them, or write a procedure that parameterizes the table names. There are plenty of examples of this on SQL websites. Here is a starter, but you can find scripts written by expert DBA's as well: How to List All Tables

4a. You could, though not recommended, do the same on the server side code: execute a statement which retrieves the table names in question, then dynamically create the DELETE string. However, stored procedure would be the best practice.

5. If you use a cursor, you need to make sure you are deleting only the tables you want, not system tables, not other tables or databases. How are you handling concurrent users?

As far as backup/restore syntax, assuming your web user has permission to execute these statements, and that they are doing so in the "master" db and there are no connections to the database in question, then you can do this via a sql statement. Simply study and test the syntax for BACKUP and RESTORE:
Transact SQL 2000 BACKUP
Sep 23 '08 #6
JustRun
127 100+
Well, I have tried the Stored Procedure but it didnt work for me, any way i managed Unloading the system with Delete command, i know its a bad way but at least it works.

How about the Backup and Restore?
I have no idea about the sql command of them
Sep 23 '08 #7
JustRun
127 100+
Any Reply, I'm stuch with the Internet search :(
Sep 24 '08 #8
mldisibio
190 Expert 100+
The MSDN link I posted not only gives the syntax, but has examples at the bottom of the page.

In MSDN, you will see the contents on the left panel. You can click on RESTORE to get the same syntax explanation and examples.

You would use the example sql just like your DELETE statement above. They are sql text statements that would be executed from a SqlCommand.ExecuteNonQuery method directly, or placed in a stored procedure whose name is executed from a SqlCommand.ExecuteNonQuery method.
Sep 24 '08 #9
JustRun
127 100+
Hi,

I knew how to make the Backup and Restore through SQL statement,
What I need to know is

in the backup page.aspx --> Choose the path of backup from a "Save as window", take this path and pass it to the SQL statement.
The Question is --> how to take the path that user had choose?

and the same in the Restore
Oct 11 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
0
by: Mirko Slavko | last post by:
Hi, I have scheduled backup job on MS SQL server which appends full backup, verifies it and then it should eject tape. But upon finishing it says that job failed. I checked backup and it seems...
3
by: Tim Morrison | last post by:
MSDE2000 I have an application in which I am running a TSQL command of BACKUP DATABASE and RESTORE DATABASE for the backup and restore commands for my application. For testing purposes, i did the...
6
by: Eric Herber | last post by:
I've a question regarding db2 (V8.1) and database backups going to a storage manager like TSM for example. As I can see in the storage manager if I backup the complete database over the TSM API...
4
by: Hardy | last post by:
hi gurus, now I have to backup and restore a 8 T size db2 database. from two s85 to two 670. the partitions,tablespaces of the db should be redesigned then I plan to use redirected restore. but...
0
by: raj.raghavan | last post by:
Hi, I have a database were all the indexes are in a seperate filegroup a few large tables are in a seperate filegroup. We take backup at different times and I have backup of primary, two filegroup...
10
by: Konstantin Andreev | last post by:
Hello. Some time ago I asked in this conference, - How to use an ONLINE BACKUP to restore database onto another system? - but got no answers. Therefore I can conclude it is not possible. But......
5
by: smoi | last post by:
Hi all, My manager ask me to do backup for 3 database and restore them in a new server. I did the backup for the 3 database into BAK file. Then in the new server, when I did the restore in SQL...
1
debasisdas
by: debasisdas | last post by:
This is a sample code for taking backup and restore of access database Dim DBTempSource As Database Dim DBTempDestination As Database Dim RecTempSource As Recordset Dim RecTempDestination...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.