restoring and restoring SQL Server databases with asp.net

Source: Internet
Author: User
Tags microsoft sql server
asp.net|server| Recovery | data | database

It is important to note that restore, restore time is the most problematic, the other users can not use the database to restore, the solution is to add a stored procedure in the master database:

Create proc killspid (@dbname varchar (20))
As
Begin
declare @sql nvarchar (500)
DECLARE @spid int
Set @sql = ' Declare getspid cursor for
Select spid from sysprocesses where dbid=db_id (' + @dbname + ') '
EXEC (@sql)
Open Getspid
FETCH NEXT from Getspid into @spid
While @ @fetch_status <>-1
Begin
EXEC (' kill ' + @spid)
FETCH NEXT from Getspid into @spid
End
Close Getspid
Deallocate getspid
End
Go

Execute this stored procedure before restoring, and you need to pass dbname, which is the name of your database. Below is the original code for the class: (The database connection string in Web.config is Constr)

Using System;

Using System.Configuration;

Using System.Data.SqlClient;

Using System.Data;

Namespace Web.base_class

{

<summary>



Dboper class, which primarily applies sqldmo to backup and restore Microsoft SQL Server databases

<summary>


public class Dboper

{

private string server;

private string uid;

private string pwd;

private string database;

private String Conn;

<summary>


Constructors for Dboper classes

<summary>


Public Dboper ()

{

conn=system.configuration.configurationsettings.appsettings["Constr"]. ToString ();

Server=cut (conn, "server=", ";");

Uid=cut (conn, "uid=", ";");

Pwd=cut (conn, "pwd=", ";");

Database=cut (conn, "database=", ";");

}

public string Cut (String str,string bg,string ed)

{

String Sub;

Sub=str. Substring (str. INDEXOF (BG) +bg. Length);

Sub=sub. Substring (0,sub. IndexOf (";"));

return sub;

}

[1] [2] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.