将带有控制字符的字符串写入数据库中的 nvarchar 字段的简单方法?
编辑:接受的答案指出了我的问题是什么。 我添加了另一个答案,它显示了一种更简单的方法。
我有一个多行文本框,从中创建一个字符串:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你说“似乎”时,你是什么意思? 换行符可以轻松存储在
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?比这更容易。 根据已接受答案中的讨论,我没有看到我认为的所有线条。 放置一个消息框显示了一切。
更重要的是,我不需要逐行处理:
也可以,并且它保留所有换行符。
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:
works, too, and it keeps all of the line breaks.