我正在尝试开发一个运行CREATE PROCEDURE脚本的小应用程序。一个简单的例子:
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'ap_get_roles_in_system')
BEGIN
DROP Procedure [dbo].[ap_get_roles_in_system]
END
GO
CREATE PROCEDURE [dbo].[ap_get_roles_in_system]
(
@system_id int
)
AS
SELECT * FROM roles
GO
GRANT EXEC ON [dbo].[ap_get_roles_in_system] TO PUBLIC
GO
如果我在一个字符串中加载这个文本,并通过ExecuteNonQuery()运行它,第一个项目,它删除存储过程,工作正常,但不是运行创建它发现语法错误与参数存储过程,即:它尚未声明。
简而言之,它不是试图运行CREATE,而是以某种方式尝试将脚本作为脚本运行,而不是CREATE。不确定正确的措辞是什么。
如果粘贴到Sql Management Studio中,上面的脚本效果很好。
这是我正在执行的代码:
public string RunSql(string Sql)
{
string result = string.Empty;
_conn.Open();
SqlCommand cmd = new SqlCommand(Sql, _conn);
cmd.CommandType = CommandType.Text;
try
{
cmd.ExecuteNonQuery();
result = "Succeeded";
}
catch (SqlException ex)
{
result = ex.Message;
}
return result;
}
答案 0 :(得分:4)
@ RichardSchneider的回答让我找到了我找到的解决方案,但我想在这个较晚的日期,因为有太多的观点,我应该发布解决问题的代码,理查德'的答案引领我。这是一个名为SqlAction的整个类。请注意,我将整个文本拆分为" GO",然后将组件放入一个数组中,依次执行每个组件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SProcRunner
{
public class SqlAction
{
public SqlAction(string connString)
{
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(connString);
_conn = new SqlConnection(sb.ToString());
}
private SqlConnection _conn;
public string RunSql(string Sql)
{
string result = string.Empty;
// split the sql by "GO"
string[] commandText = Sql.Split(new string[] { String.Format("{0}GO{0}", Environment.NewLine) }, StringSplitOptions.RemoveEmptyEntries);
_conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = _conn;
cmd.CommandType = CommandType.Text;
for (int x = 0; x < commandText.Length; x++)
{
if (commandText[x].Trim().Length > 0)
{
cmd.CommandText = commandText[x];
try
{
cmd.ExecuteNonQuery();
result = "Command(s) completed successfully.";
}
catch (SqlException ex)
{
result = String.Format("Failed: {0}", ex.Message);
break;
}
}
}
if (_conn.State != ConnectionState.Closed) _conn.Close();
return result;
}
}
}
答案 1 :(得分:1)
从SQL中删除“GO”行。