首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

数据库传入‘的有关问题

2012-12-27 
数据库传入‘的问题本帖最后由 xzgspchina 于 2012-12-01 08:56:23 编辑string.Format(@select Station_ID

数据库传入‘的问题
本帖最后由 xzgspchina 于 2012-12-01 08:56:23 编辑


string.Format(@"select Station_ID from tbStation 
where Station_Name='{0}',"'";



string str="select Station_ID from tbStation 
where Station_Name='''";



string str="select Station_ID from tbStation 
where Station_Name=@name";
Parameter[] pa=new Parameter[]{new Parameter("@name",dbtype.string,20)};
pa[0].value=';

只有第三个才能传进去值。其他不行。。。为什么。怎么能成功用其他的
[最优解释]
这是因为'是转义字符,需要用''两个'表示一个',而第3种传参的方式是把'当成一个整体处理,不存在转义一说
1、正确写法
string.Format(@"select Station_ID from tbStation where Station_Name='{0}',"''");

2、正确写法
string str="select Station_ID from tbStation where Station_Name=''''";(其中中间两个'起到转义成一个'的作用
[其他解释]
引用:
不可以这样写,关键是插入的时候。也不知道什么时候是’
不能每次都这样写吧 
以后提出问题时尽量不要写一个误导别人的问题然后又说“不可以这样写”(明明是你自己先这样表达的)。

string.Format(@"select Station_ID from tbStation where Station_Name='{0}', x.Replace("'","''");

[其他解释]
用你第三种方式就ok啊,还可以有效的防止sql注入
[其他解释]
引用:
这是因为'是转义字符,需要用''两个'表示一个',而第3种传参的方式是把'当成一个整体处理,不存在转义一说
1、正确写法
string.Format(@"select Station_ID from tbStation where Station_Name='{0}',"''");

2、正确写法
string str="select Station_ID from tbStation ……

可以这样写,关键是插入的时候。也不知道什么时候是’
不能每次都这样写吧
[其他解释]

我最后的50分。。不能这么黄了
[其他解释]
string.Format(@"select Station_ID from tbStation where Station_Name='{0}', x.Replace("'","''"))

[其他解释]
引用:
引用:
不可以这样写,关键是插入的时候。也不知道什么时候是’
不能每次都这样写吧 以后提出问题时尽量不要写一个误导别人的问题然后又说“不可以这样写”(明明是你自己先这样表达的)。



C# code??



1

string.Format(@"select Station_ID from tbStation where Stati……
谢谢了。。

热点排行