//在插入语句中检查
private void FindInsertField(string sql, SqlParameter[] paras)
{
MatchCollection match = Regex.Matches(sql, "\\((.*?)\\)", RegexOptions.IgnoreCase);
string[] fields = new string[2];
for (int i = 0; i < 2; i++)
{
fields[i] = match[i].Groups[1].Value;
}
string[] fieldArr = fields[0].Split(‘,‘);
string sqlReplace = sql.Replace(fields[0], "{0}").Replace(fields[1], "{1}");
foreach (string s in fieldArr)
{
try
{
string sqlNew = string.Format(sqlReplace, s, "@" + s);
DbHelperSQL.ExecuteSql(sqlNew, paras);
}
catch (Exception ex)
{
throw ex;
}
}
}
//在更新语句中检查
private void FindUpdateField(string sql, SqlParameter[] paras)
{
Match match = Regex.Match(sql, "set ([\\w\\W]*) where", RegexOptions.IgnoreCase);
string fields = string.Empty;
if (match != null && match.Groups.Count > 1)
fields = match.Groups[1].Value;
else
return;
string[] fieldArr = fields.Split(‘,‘);
string sqlReplace = sql.Replace(fields, "{0}");
foreach (string s in fieldArr)
{
try
{
string sqlNew = string.Format(sqlReplace, s);
DbHelperSQL.ExecuteSql(sqlNew, paras);
}
catch (Exception ex)
{
throw ex;
}
}
}
原文:http://www.cnblogs.com/dbolodb/p/5127768.html