首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

将一个SQL条件字符串转换成DbParameter,该怎么解决

2013-01-22 
将一个SQL条件字符串转换成DbParameter已经str是一个字符串格式是string str aa111 and b222 and

将一个SQL条件字符串转换成DbParameter
已经str是一个字符串格式是
string str = "a='a111' and b='222' and ccc=200";
是样的一个格式,可能条件会更多

现在想根据这个字条串条件生成DbParameter应该怎么写啊?
private DbParameter[] fun1(string str)
{
  如果传一个str过来(str的格式和上面的差不多,可能条件数量不固定),这里如何转换成DbParameter[]
}
[解决办法]

 private DbParameter[] fun1(string str)
        {
            //string str = "a='a111' and b='222' and ccc=200";
            var array = Regex.Matches(str, @"([\w_]+)=([""']?)(.*?)\2(?=\s
[解决办法]
$)").OfType<Match>().Select(t => new { field = t.Groups[1].Value, value = t.Groups[3].Value }).ToArray();
            if (array.Count() == 0)
                return null;

            DbParameter[] dbParameter = new DbParameter[array.Count()];

            for (int i = 0; i < array.Count(); i++)
            {
                dbParameter[i] = new DbParameter(array[i].field, array[i].value);
            }
            return dbParameter;
        }

热点排行