码迷,mamicode.com
首页 > 数据库 > 详细

c#解决数据库用in的时候大于1000报错问题

时间:2014-11-05 21:10:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   ar   os   for   sp   数据   

问题:

bubuko.com,布布扣
//oracle数据库报错
delete from DD_ORDER_DETAIL where ORDERID=in(0,1,2,3,4,5,6,7.....1001);
View Code

c#写了一个方法解决

bubuko.com,布布扣
/// <summary>
        /// 拼接sql
        /// </summary>
        /// <param name="str">主sql语句</param>
        /// <param name="ids">要拼接的in里面的参数</param>
        /// <param name="count">每次执行条数</param>
        /// <returns></returns>
        public static string get(string str, string[] ids)
        {

            if (ids.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < ids.Length; i++)
                {
                    if ((i % 1000) == 0 && i > 0)
                    {
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append(") or " + str + "in(" + ids[i] + ",");
                    }
                    else
                    {
                        sb.Append(ids[i] + ",");
                    }
                }
                sb.Remove(sb.Length - 1, 1);

                return str + "in(" + sb.ToString() + ")";
            }
            return "";
        }
View Code

调用如下

bubuko.com,布布扣
 static void Main(string[] args)
        {
            string str = "delete from DD_ORDER_DETAIL where ORDERID =";
            string[] s = { };
            List<string> list = new List<string>();
            for (int i = 0; i < 1001; i++)
            {
                list.Add(i.ToString());
            }
            s = list.ToArray(); ;
            Console.WriteLine(s.Length);
            string ss = get(str, s);
            Console.ReadKey();
        }
View Code

效果如下

bubuko.com,布布扣

bubuko.com,布布扣

c#解决数据库用in的时候大于1000报错问题

标签:style   blog   http   color   ar   os   for   sp   数据   

原文地址:http://www.cnblogs.com/valiant1882331/p/4077149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!