拼装SQL

public static string GetSQL()
{
    string sql = "SELECT * FROM TUses WHERE 1=1 ";

    #region AND id IN (1, 2, 3, 4, 5)
int[] idList = new int[] { 1, 2, 3, 4, 5 }; //int[] idList = new int[] { 10 }; if (idList != null && idList.Length > 0) { if (idList.Length == 1) { sql += String.Format(" AND id = {0} ", idList[0]); } else { string str = " AND id IN ( "; foreach (var i in idList) { str += i + ","; } str = str.TrimEnd(',');//去掉最后的 , str += " ) "; sql += str; } } Console.WriteLine(sql); #endregion #region AND name IN ('AA','BB','CC','DD')
string[] names = new string[] { "AA", "BB", "CC", "DD" }; //string[] names = new string[] { "ROSE" }; if (names != null && names.Length > 0) { if (names.Length == 1) { sql += String.Format(" AND name = '{0}' ", names[0]); } else { string str = String.Format(" AND name IN ( "); foreach (var s in names) { str += String.Format("'{0}',", s); } str = str.TrimEnd(','); str += " ) "; sql += str; } } Console.WriteLine(sql); #endregion return sql; }

 

posted @ 2018-06-11 00:52  茗::流  阅读(133)  评论(0)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。