如何使用 C# 从存储在数据库中的记录创建 SQL 服务器存储过程,保留换行符和制表符
How create SQL Server stored procedure, keeping line breaks and tabs, from record stored in database with C#
SQL服务器:我已经复制并保存了一个存储过程到数据库中。在此过程中,它在整个存储过程文本中将换行符和制表符转义为 \n 和 \t 字符,因此它看起来像:
SQL = "create PROCEDURE ScrambleNames\n\t@removeAdmins bit \nAS\nBEGIN\n\t-- SET NOCOUNT ON added to prevent..." just shortened for clarity
using (DbConnection dbcn = dbf.CreateConnection())
{
dbcn.ConnectionString = cnxn;
dbcn.Open();
using (DbCommand dbcmd = dbcn.CreateCommand())
{
dbcmd.CommandType = CommandType.Text;
dbcmd.CommandText = SQL;
dbcmd.ExecuteNonQuery();
}
}
我想用 C# 读取 SQL 语句,并使用它通过执行该命令在另一个数据库中创建一个实际的存储过程。显然,当我尝试在不做任何更改的情况下执行它时,它说 \ 附近存在语法错误并且失败了。我尝试用字符串中的实际二进制字符替换 \n 和 \t 但失败并出现更多错误:
Incorrect syntax near '1'... etc.
那么,如何从数据库中读取文本并在我尝试创建的存储过程中保留换行符和制表符?
使用逐字字符串和@来支持多行字符串,像这样
SQL = @"create PROCEDURE ScrambleNames
@removeAdmins bit
AS BEGIN
-- comment here
SELECT * FROM";
using (DbConnection dbcn = dbf.CreateConnection())
{
dbcn.ConnectionString = cnxn;
dbcn.Open();
using (DbCommand dbcmd = dbcn.CreateCommand())
{
dbcmd.CommandType = CommandType.Text;
dbcmd.CommandText = SQL;
dbcmd.ExecuteNonQuery();
}
}
解释逐字字符串的示例:
normal_string = "Line1 \n Line2 \n line3";
same_as_verbatim_string = @"Line1
Line2
line3";
SQL服务器:我已经复制并保存了一个存储过程到数据库中。在此过程中,它在整个存储过程文本中将换行符和制表符转义为 \n 和 \t 字符,因此它看起来像:
SQL = "create PROCEDURE ScrambleNames\n\t@removeAdmins bit \nAS\nBEGIN\n\t-- SET NOCOUNT ON added to prevent..." just shortened for clarity
using (DbConnection dbcn = dbf.CreateConnection())
{
dbcn.ConnectionString = cnxn;
dbcn.Open();
using (DbCommand dbcmd = dbcn.CreateCommand())
{
dbcmd.CommandType = CommandType.Text;
dbcmd.CommandText = SQL;
dbcmd.ExecuteNonQuery();
}
}
我想用 C# 读取 SQL 语句,并使用它通过执行该命令在另一个数据库中创建一个实际的存储过程。显然,当我尝试在不做任何更改的情况下执行它时,它说 \ 附近存在语法错误并且失败了。我尝试用字符串中的实际二进制字符替换 \n 和 \t 但失败并出现更多错误:
Incorrect syntax near '1'... etc.
那么,如何从数据库中读取文本并在我尝试创建的存储过程中保留换行符和制表符?
使用逐字字符串和@来支持多行字符串,像这样
SQL = @"create PROCEDURE ScrambleNames
@removeAdmins bit
AS BEGIN
-- comment here
SELECT * FROM";
using (DbConnection dbcn = dbf.CreateConnection())
{
dbcn.ConnectionString = cnxn;
dbcn.Open();
using (DbCommand dbcmd = dbcn.CreateCommand())
{
dbcmd.CommandType = CommandType.Text;
dbcmd.CommandText = SQL;
dbcmd.ExecuteNonQuery();
}
}
解释逐字字符串的示例:
normal_string = "Line1 \n Line2 \n line3";
same_as_verbatim_string = @"Line1
Line2
line3";