虽然数据库语句在直接出现在程序中很少(都以存储过程代替),但是在搜索时会出现部分Sql语句的拼装问题。
SqlHelper.cs
using
System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Text.RegularExpressions;
namespace Beasyer.Lib
... {
/**//// <summary>
/// 包装SQL的操作
/// </summary>
public static class SqlHelper
...{
/**//// <summary>
/// //追加查询条件
/// </summary>
/// <param name="sql">原本的SQL语句</param>
/// <param name="where">要追加的where语句,不能带where</param>
/// <returns></returns>
public static string AppendWhereToSql(string sql, string where)
...{
if (string.IsNullOrEmpty(sql))
...{
return string.Empty;
}
else
...{
if(string.IsNullOrEmpty(where))
...{
return sql;
}
else
...{
if(sql.ToLower().IndexOf("where")>0)
...{
int whereint=sql.ToLower().IndexOf("where");
return sql.Substring(0,whereint+6)+where+sql.Substring(10,sql.Length-whereint-6);
}
else
...{
return sql+" where "+where;
}
}
}
}
/**//// <summary>
/// 组合两个SQL中的条件语句反回带WHERE的语句
/// </summary>
/// <param name="where1"></param>
/// <param name="where2"></param>
/// <returns></returns>
public static string UnionTwoWhereString(string where1, string where2)
...{
where1.Trim();
where2.Trim();
if (where1 == null) where1 = "";
if (where2 == null) where2 = "";
string retStr;
if (where1 != "" && where2 != "")
...{
retStr = " WHERE" + " (" + where1 + ")and(" + where2 + ")";
}
else if (where1 == "" && where2 == "")
...{
retStr = "";
}
else
...{
retStr = " WHERE" + " " + where1 + where2;
}
return retStr;
}
/**//// <summary>
/// 更换查询语句中的第一个查询字段列表
/// </summary>
/// <param name="insql">查询语句</param>
/// <param name="newlist">替换列表</param>
public static string ReplaceFirstList(string insql, string newList)
...{
string sql = String.Copy(insql);
CheckObjectHelper.ArgumentNotNullOrEmptyString(sql, "sql");
CheckObjectHelper.ArgumentNotNullOrEmptyString(newList, "newList");
int startPoint;
int endPoint;
string upSql = sql.ToUpper();
startPoint = upSql.IndexOf("SELECT");
if (startPoint == -1)
...{
throw new ApplicationException("你提供了不正确的查询语句。(" + sql + ")");
}
else
...{
startPoint = startPoint + 6;
}
endPoint = upSql.IndexOf("FROM");
if (endPoint == -1)
...{
throw new ApplicationException("你提供了不正确的查询语句。(" + sql + ")");
}
return sql.Replace(sql.Substring(startPoint, endPoint - startPoint), " " + newList + " ");
}
}
}
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Text.RegularExpressions;
namespace Beasyer.Lib
... {
/**//// <summary>
/// 包装SQL的操作
/// </summary>
public static class SqlHelper
...{
/**//// <summary>
/// //追加查询条件
/// </summary>
/// <param name="sql">原本的SQL语句</param>
/// <param name="where">要追加的where语句,不能带where</param>
/// <returns></returns>
public static string AppendWhereToSql(string sql, string where)
...{
if (string.IsNullOrEmpty(sql))
...{
return string.Empty;
}
else
...{
if(string.IsNullOrEmpty(where))
...{
return sql;
}
else
...{
if(sql.ToLower().IndexOf("where")>0)
...{
int whereint=sql.ToLower().IndexOf("where");
return sql.Substring(0,whereint+6)+where+sql.Substring(10,sql.Length-whereint-6);
}
else
...{
return sql+" where "+where;
}
}
}
}
/**//// <summary>
/// 组合两个SQL中的条件语句反回带WHERE的语句
/// </summary>
/// <param name="where1"></param>
/// <param name="where2"></param>
/// <returns></returns>
public static string UnionTwoWhereString(string where1, string where2)
...{
where1.Trim();
where2.Trim();
if (where1 == null) where1 = "";
if (where2 == null) where2 = "";
string retStr;
if (where1 != "" && where2 != "")
...{
retStr = " WHERE" + " (" + where1 + ")and(" + where2 + ")";
}
else if (where1 == "" && where2 == "")
...{
retStr = "";
}
else
...{
retStr = " WHERE" + " " + where1 + where2;
}
return retStr;
}
/**//// <summary>
/// 更换查询语句中的第一个查询字段列表
/// </summary>
/// <param name="insql">查询语句</param>
/// <param name="newlist">替换列表</param>
public static string ReplaceFirstList(string insql, string newList)
...{
string sql = String.Copy(insql);
CheckObjectHelper.ArgumentNotNullOrEmptyString(sql, "sql");
CheckObjectHelper.ArgumentNotNullOrEmptyString(newList, "newList");
int startPoint;
int endPoint;
string upSql = sql.ToUpper();
startPoint = upSql.IndexOf("SELECT");
if (startPoint == -1)
...{
throw new ApplicationException("你提供了不正确的查询语句。(" + sql + ")");
}
else
...{
startPoint = startPoint + 6;
}
endPoint = upSql.IndexOf("FROM");
if (endPoint == -1)
...{
throw new ApplicationException("你提供了不正确的查询语句。(" + sql + ")");
}
return sql.Replace(sql.Substring(startPoint, endPoint - startPoint), " " + newList + " ");
}
}
}