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:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...