将带有控制字符的字符串写入数据库中的 nvarchar 字段的简单方法?

发布于 2024-07-26 16:36:58 字数 595 浏览 6 评论 0原文

编辑:接受的答案指出了我的问题是什么。 我添加了另一个答案,它显示了一种更简单的方法。

我有一个多行文本框,从中创建一个字符串:

    Dim wholeThing As String
    Dim line as String 
    For Each line In txtMultiline.Lines
        wholeThing = wholeThing & line & Environment.Newline
    Next

我想做的是通过将 WholeThing 作为参数添加到 SqlCommand.Parameters.AddWithValue,将其按原样写入数据库,但我所有的辛苦插入换行符消失(或看起来消失)。

我看到这个 并希望我不需要关心 CHAR(nnn) SQL 的东西。

有什么建议么? 谢谢!

EDIT: Accepted answer points out what my issue was. I added another answer which shows an even easier way.

I have a multiline textbox from which I create a single string:

    Dim wholeThing As String
    Dim line as String 
    For Each line In txtMultiline.Lines
        wholeThing = wholeThing & line & Environment.Newline
    Next

What I'd like to do is write this to a DB as-is by adding wholeThing as a parameter using to SqlCommand.Parameters.AddWithValue, but all of my painstakingly-inserted newlines go away (or appear to).

I saw this and was hoping I didn't need to concern myself with CHAR(nnn) SQL stuff.

Any suggestions? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-08-02 16:36:58

当你说“似乎”时,你是什么意思? 换行符可以轻松存储在 nvarchar 字段中。 您使用什么工具来验证您的操作结果?

What do you mean when you say "appears to"? A newline character can be easily stored in an nvarchar field. What tools are you using to verify the results of your actions?

一紙繁鸢 2024-08-02 16:36:58

比这更容易。 根据已接受答案中的讨论,我没有看到我认为的所有线条。 放置一个消息框显示了一切。

更重要的是,我不需要逐行处理:

wholeThing = txtMultiline.Text

也可以,并且它保留所有换行符。

It's even easier than this. As per the discussion in the accepted answer, I wasn't seeing all of the lines in my view. Putting up a messagebox showed everything.

What's more, I don't need to take things line by line:

wholeThing = txtMultiline.Text

works, too, and it keeps all of the line breaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文