c#动态创建存储过程中,提示go 附近有语法错误解决方案
2010-09-30 22:45:55 来源:WEB开发网查询分析器是sql客户端,它可以识别go用来分批提交,但go不是sql语句,sql服务器不能识别所以不能用在程序中。
所以将带有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
更多精彩
赞助商链接