在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 public int CreateDB_KillProc_proc()
但执行过程中出现以下错误
以下是我在查询分析器中能正常使用的脚本代码。
1 USE [RadarDataBase] 2 GO 3 SET ANSI_NULLS ON 4 GO 5 SET QUOTED_IDENTIFIER ON 6 GO 7 create proc [dbo].[P_KillConnections] 8 @dbname varchar(200) 9 as 10 declare @sql nvarchar(500) 11 declare @spid nvarchar(20) 12 declare #tb cursor for 13 select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) 14 open #tb 15 fetch next from #tb into @spid 16 while @@fetch_status=0 17 begin 18 exec('kill '+@spid) 19 fetch next from #tb into @spid 20 end close #tb deallocate #tb ";
所以将带有GO的语句分成多条sql语句,执行多条SQL语句,实现数据库事务,代码如下:
1 public int CreateDB_KillProc_proc() 2 { 3 List<string> strSqls = new List<string>(); 4 int result = 0; 5 string SqlStr = ""; 6 //组合Sql语句 7 SqlStr += "USE [RadarDataBase]"; 8 strSqls.Add(SqlStr); 9 SqlStr = "SET ANSI_NULLS ON "; 10 strSqls.Add(SqlStr); 11 SqlStr = "SET QUOTED_IDENTIFIER ON "; 12 strSqls.Add(SqlStr); 13 SqlStr = @"create proc [dbo].[P_KillConnections] 14 @dbname varchar(200) 15 as 16 declare @sql nvarchar(500) 17 declare @spid nvarchar(20) 18 declare #tb cursor for 19 select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) 20 open #tb 21 fetch next from #tb into @spid 22 while @@fetch_status=0 23 begin 24 exec('kill '+@spid) 25 fetch next from #tb into @spid 26 end close #tb deallocate #tb"; 27 strSqls.Add(SqlStr); 28 //执行Sql语句 29 try 30 { 31 result = DbHelperSQL.ExecuteSqlTran(strSqls);//这个函数功能是执行多条sql语句实现数据库事务 32 } 33 catch (Exception e) 34 { 35 ErrStr = e.Message; 36 return -2; 37 } 38 return result; 39 } 40
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论