通过 C# 将回车符传递给 SQL SP [英] Passing carriage return to SQL SP through C#

查看:31
本文介绍了通过 C# 将回车符传递给 SQL SP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用 SQL 存储过程的 c# 程序,并将一些字符串传递给它,例如

I have a c# program that calls a SQL Stored proc, and passes some strings to it, e.g.

string str1 = "Some String 1";
string str1 = "Some String 2";
using (var command = new SqlCommand(MySP, conn)
        {
            CommandType = CommandType.StoredProcedure
        }
        )
{
    command.Parameters.AddWithValue("@Par1", str1);
    command.Parameters.AddWithValue("@Par2", str2);
    conn.Open();
    command.ExecuteNonQuery();
    conn.Close();
}

现在我想在 str1 中添加一个回车符.
但是像 default (str1 = "Some \r\n string";) 一样添加它不会向 SQL 发送回车,而是发送带有转义字符的完整字符串.

Now I would like to add a carriage return into str1.
But adding it like default (str1 = "Some \r\n string";) does not send the carriage return to SQL, but it sends the full string with the escape characters.

我还能如何实现这一目标?

How else can I achieve this?

推荐答案

尝试使用 Environment.NewLine:

string str1 = "Some" + Environment.NewLine + "String";

这篇关于通过 C# 将回车符传递给 SQL SP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆