Tuesday, August 17, 2010

How to extract information from SQL query using C#

string from = "from", select = "select", where = "where", groupBy = "group by", orderBy = "order by";
string sql = "SELECT X, Y, Z FROM Table WHERE X=9 AND  Y =10 AND Z BETWEEN 1 AND 6 ORDER BY X GROUP BY Z"; ;
string lSql = sql.ToLower();
int fromIndex = lSql.IndexOf(from) + from.Length;
int selectIndex = lSql.IndexOf(select) + select.Length;
int whereIndex = lSql.IndexOf(where) + where.Length;
int groupByIndex = lSql.IndexOf(groupBy) + groupBy.Length;
int orderByIndex = lSql.IndexOf(orderBy) + orderBy.Length;
string colummExp = sql.Substring(selectIndex, fromIndex - from.Length - selectIndex).Trim();
string fromExp = sql.Substring(fromIndex, 
    (whereIndex > 0 ? (whereIndex - where.Length) : lSql.Length) - fromIndex).Trim();
int s = groupByIndex > orderByIndex ? orderByIndex : groupByIndex;
int l = groupByIndex > orderByIndex ? orderBy.Length : groupBy.Length;
string whereExp = sql.Substring(whereIndex, (s > 0 ? s - l : lSql.Length) - whereIndex).Trim();

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...