c# 添加到数据库的字符串是一个路径,路径中带有\n,结果被认为是转义字符,怎么处理,高手指教!!!

hedabin 2008-11-20 09:21:45
....
string path = this.textBox1.Text.Trim();//值是"d:\pro\new"
string strIns = "INSERT INTO table (DB_NAME,PATH) VALUES('db1','" + path + "')";
DB.DBConnect.ExeSql(strIns);
....

结果插入数据库后,PATH字段的值变成"d:\proew",\n被认为是回车了,怎么解决?
...全文
424 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
止戈而立 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hedabin 的回复:]
有没有有经验的朋友,分享一下,结帐时再加分。
[/Quote]

用传参数的方法,不会有类似的问题,文本里的值是什么,插入到数据库里的就是什么。

拼接字符串的方法有时确实会存在这样那样的问题。。。
net5i 2008-11-20
  • 打赏
  • 举报
回复
替换字符串啊,

string path = this.textBox1.Text.Trim().Replace("\\","/\\");//值是"d:/\pro/\new"
string strIns = "INSERT INTO table(DB_NAME,PATH) VALUES('db1','" + path + "') ESCAPE '/'";
hedabin 2008-11-20
  • 打赏
  • 举报
回复
有没有有经验的朋友,分享一下,结帐时再加分。
hedabin 2008-11-20
  • 打赏
  • 举报
回复
回八楼:
我的text值是从下面的方法得来的

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Microsoft Access(*.mdb)|*.mdb|所有文件(*.*)|*.*";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
this.textBox2.Text = FileName;
}

我怎么处理它成"d:/\pro/\new"
net5i 2008-11-20
  • 打赏
  • 举报
回复
搂主估计要自定义SQL转义符了,使用escape 关键字

string path = this.textBox1.Text.Trim();//值是"d:/\pro/\new"
string strIns = "INSERT INTO table(DB_NAME,PATH) VALUES('db1','" + path + "') ESCAPE '/'";


注意粗体字部分
hedabin 2008-11-20
  • 打赏
  • 举报
回复
都不行,全试了。
还有没处理过这种情况的啊!!!!
三下鱼 2008-11-20
  • 打赏
  • 举报
回复
可以用@
同意7楼
jstzcl007 2008-11-20
  • 打赏
  • 举报
回复
@textbox.text.tostring()
acqy 2008-11-20
  • 打赏
  • 举报
回复

string strIns = "INSERT INTO table (DB_NAME,PATH) VALUES('db1','" + path.Replace("\\", "\\\\") + "')";
hedabin 2008-11-20
  • 打赏
  • 举报
回复
我的路径是从textBox的text属性中获得,@怎么用?
ustbwuyi 2008-11-20
  • 打赏
  • 举报
回复
1 转义: string str="d:\\pro\\new";

2 @ string str=@"d:\pro\new";
donghai83 2008-11-20
  • 打赏
  • 举报
回复
考虑哈@的用法

111,086

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧