public News getNews(string webtype, string classinfo, string type)
{
try
{
string sql = "select top 1 * from " + table + " where 1=1";
if (webtype != null && webtype.Length > 0)
{
sql = sql + " and n_webtype = '" + webtype + "' ";
}
if (classinfo != null && classinfo.Length > 0)
{
sql = sql + " and n_class = '" + classinfo + "' ";
}
if (type != null && type.Length > 0)
{
sql = sql + " and n_type = '" + type + "' ";
}
sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc ";
这是项目中一些代码,新人,所以贴出来请教一下。
string sql = "select top 1 * from " + table + " where 1=1";
1.这个里面select top 1 * 这是怎么理解?另外这里有where 1=1在这里起作用。
{
try
{
string sql = "select top 1 * from " + table + " where 1=1";
if (webtype != null && webtype.Length > 0)
{
sql = sql + " and n_webtype = '" + webtype + "' ";
}
if (classinfo != null && classinfo.Length > 0)
{
sql = sql + " and n_class = '" + classinfo + "' ";
}
if (type != null && type.Length > 0)
{
sql = sql + " and n_type = '" + type + "' ";
}
sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc ";
这是项目中一些代码,新人,所以贴出来请教一下。
string sql = "select top 1 * from " + table + " where 1=1";
1.这个里面select top 1 * 这是怎么理解?另外这里有where 1=1在这里起作用。
解决方案 »
- 正则表达式 从字符串中提取数组,遇到换行就提取不了
- C#如何让系统注销时不关闭程序?
- 急!!通过反射IList<T>取值问题
- 从数据库里提取题目的C#代码
- 【请教】ASP.NET(c#)源代码错误,麻烦看看这是什么原因。谢谢。
- gridview空行填满多余空间
- Card[] newDeck = new Card[52]; 与 bool[] assigned = new Card[52]; 的区别疑问
- freetextbox 工具栏的图片是怎么封装的?
- dataAdapter多表update的问题
- 软件打包出现"不可恢复的生成错误",其显示的一个文件是setup.vdproj!!请问是什么原因啊???
- 托管C++包装第三方 API dll,在C#调用时出现问题(在线等)
- 怎么样才有钱
WHERE 1=1 没什么只是恒成立,为了后面连接AND 条件
where 1=1 我也不知道什么意思
但是加与不加这句查询出来的东西没什么改变
关注
where 1=1 --可能是为了防止SQL注入为什么不用parameter呢?
这就是一个完整的SQL语句!!字符相加
top 1应该是找的结果的第一行吧
where 1=1 在這里 其實更多的是代碼上的一個處理而已
可以看他的代碼里面的邏輯
因為這個Sql是拼接字符串而來的 所以 在select ..... from table 后 不知道要不要加where 如果if 條件里的都不成立 那就不需要加where 如果if 條件里有成立的就需要加where 所以這個判斷就有點繁
因此就不管三七二十一 先加上where 用select .... from table where 1=1 然后后面滿足一個if條件就拼上去一個and ....
public News getNews(string webtype, string classinfo, string type)
{
try
{
string sql = "select top 1 * from " + table + " //返回结果集中的第一行
where 1=1"; //方便连接后面的sql条件
if (webtype != null && webtype.Length > 0)
{
sql = sql + " and n_webtype = '" + webtype + "' ";
}
if (classinfo != null && classinfo.Length > 0)
{
sql = sql + " and n_class = '" + classinfo + "' ";
}
if (type != null && type.Length > 0)
{
sql = sql + " and n_type = '" + type + "' ";
}
sql = sql + " and n_state= " + DictState.OPENING + " order by n_news_id desc ";
就是里面的if判断夹在SQL中间,是不是不符合SQL语法呀~
偏于后面接 And 字符。 不然的话还要写一堆逻辑来确定是否要加 And还是不加。
而且1=1 这个条件不影响执行效率的,所以偷懒有理。反正我Pro c/c++ for oracle & .net + sqlserver & php+mysql 都是这么干的。Top 1 / Top n 是取顺序结果的前1(N)条记录。
, table
, DictState.OPENING
, String.IsNullOrEmpty(webtype) ? "and n_webtype = '" + webtype + "'" : ""
, String.IsNullOrEmpty(classinfo) ? "and n_class = '" + classinfo + "'" : ""
, String.IsNullOrEmpty(type) ? "and n_type = '" + type + "'" : "");
, table
, DictState.OPENING
, !String.IsNullOrEmpty(webtype) ? "and n_webtype = '" + webtype + "'" : ""
, !String.IsNullOrEmpty(classinfo) ? "and n_class = '" + classinfo + "'" : ""
, !String.IsNullOrEmpty(type) ? "and n_type = '" + type + "'" : "");
里面就有sql编辑语句,只不过出c#用到了这一功能,他们是等效的。