日期:2014-05-20  浏览次数:20760 次

数据还原
我的代码: 可以系统说该数据库目前已有其他连接,故操作失败.
请问怎么去杀死或断开与数据库连接的操作?????public int restore() {
  int a=0;
  Connection con = null;
  Statement sta = null;
try {
 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动
 con = DriverManager.getConnection(loginFrame.link.trim(), loginFrame.user.trim(), loginFrame.password.trim());
 sta = con.createStatement();
sta.executeUpdate("restore database stock from stock") ;
  }
  catch (Exception e) {
  a=1;
  try {
  sta.close();
  con.close();
  }
  catch (SQLException e1) {
  }
  }
  finally {
  try {
  sta.close();
  con.close();
  }
  catch (SQLException e) {
  a=1;
  }
  }
  return a;
 }

------解决方案--------------------
断开的存储过程 代码中调用改过程即可~传的参数是你数据库名称
SQL code

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[killspid]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

create  proc  killspid  (@dbname  varchar(20))  
as  
begin  
declare  @sql  nvarchar(500),@temp varchar(1000)
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 =0
begin  
  set @temp='kill  '+rtrim(@spid)
  exec(@temp)
fetch  next  from  getspid  into  @spid  
end  
close  getspid  
deallocate  getspid  
end

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO