现在的位置: 首页 > 综合 > 正文

sql :.net还原数据库

2012年08月11日 ⁄ 综合 ⁄ 共 1041字 ⁄ 字号 评论关闭

//杀掉数据库相关进程

create proc killspid


@dbName nvarchar(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

//在asp.net中调用存储过程 
        private void killProc(string dbName)
        {
            SqlConnection con;
            SqlCommand cmd;

            con = new SqlConnection();
            cmd = new SqlCommand();
            con.ConnectionString = "your connectionString"
            cmd.Connection = con;
            cmd.CommandText = "killspid";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@dbName", dbName);

            con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

抱歉!评论已关闭.