创建通过C#的存储过程 [英] Creating a stored procedure via C#
问题描述
我想创造我的web应用程序的种子DB,我是能够创建数据库,填充表,我只是停留在让存储过程的工作。这里是我到目前为止,但即时得到CREATE / ALTER PROCEDURE'必须在查询batch.\r\\\
Incorrect语法附近的第一条语句'GO'。我试图删除GO的太多,并且还加入USE语句并没有运气的创建过程之间\r\\\
的。 。任何帮助,将不胜感激。
的StringBuilder SBSP =新的StringBuilder();
sbSP.AppendLine(使用[+ txtDBName.Text +]);
sbSP.AppendLine(GO);
sbSP.AppendLine(CREATE PROCEDURE [spInsertADAuthorization] @AD_Account VARCHAR(255),@ AD_SID VARCHAR(255),@ AD_EmailAddress VARCHAR(255),@ DateImported日期时间,@有效位AS BEGIN SET NOCOUNT ON; INSERT INTO AD_Authorization(AD_Account,AD_SID,AD_EmailAddress,DateImported,活动)VALUES(@ AD_Account,@ AD_SID,@ AD_EmailAddress,@ DateImported,@活动)END);
sbSP.AppendLine(GO);
使用(SqlConnection的连接=新的SqlConnection(的ConnectionString))使用
{
(CMD的SqlCommand =新的SqlCommand(sbSP.ToString(),连接))
{
connection.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
的Connection.close();
}
}
GO是一个批处理分隔符。这不是一个T-SQL语句。去掉使用和两个GO行,然后重试。
I'm trying to create a seed DB in my web app, and i was able to create the DB, populate the tables, i'm just stuck at getting the stored procedures to work. Here is what i have so far, but im getting CREATE/ALTER PROCEDURE' must be the first statement in a query batch.\r\nIncorrect syntax near 'GO'. I tried with removing the GO's too, and also adding \r\n's between the USE statement and the create procedure with no luck. Any help would be appreciated.
StringBuilder sbSP = new StringBuilder();
sbSP.AppendLine("USE [" + txtDBName.Text + "]");
sbSP.AppendLine("GO");
sbSP.AppendLine("CREATE PROCEDURE [spInsertADAuthorization] @AD_Account varchar(255),@AD_SID varchar(255),@AD_EmailAddress varchar(255),@DateImported datetime,@Active bit AS BEGIN SET NOCOUNT ON; INSERT INTO AD_Authorization (AD_Account, AD_SID, AD_EmailAddress, DateImported, Active) VALUES (@AD_Account,@AD_SID,@AD_EmailAddress,@DateImported,@Active) END");
sbSP.AppendLine("GO");
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sbSP.ToString(), connection))
{
connection.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
connection.Close();
}
}
"GO" is a batch separator. It's not a T-SQL Statement. Remove the "USE" and both "GO" lines and try again.
这篇关于创建通过C#的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!