c#解决数据库用in的时候大于1000报错问题
问题:
//oracle数据库报错
delete from DD_ORDER_DETAIL where ORDERID=in(0,1,2,3,4,5,6,7.....1001);
c#写了一个方法解决
/// <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 > )
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < ids.Length; i++)
{
if ((i % ) == && i > )
{
sb.Remove(sb.Length - , );
sb.Append(") or " + str + "in(" + ids[i] + ",");
}
else
{
sb.Append(ids[i] + ",");
}
}
sb.Remove(sb.Length - , ); return str + "in(" + sb.ToString() + ")";
}
return "";
}
调用如下
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 = ; i < ; i++)
{
list.Add(i.ToString());
}
s = list.ToArray(); ;
Console.WriteLine(s.Length);
string ss = get(str, s);
Console.ReadKey();
}
效果如下


第二种:
//ids要插入in中的数据,sqlstr原始sql,num多少条执行一次
public static string[] CreateSQL(string[] ids, string sqlStr, int num)
{
string[] sqls = null;
StringBuilder sb = new StringBuilder();
sb.Append(sqlStr);
List<string> lt = new List<string>();
for (int i = ; i < ids.Length; i++)
{
if (i != && i % num == )
{
string s1 = sb.ToString();
s1 = s1.Remove(s1.Length - ) + ")";
lt.Add(s1);
sb.Length = ;
sb.Append(sqlStr);
sb.Append(ids[i] + ",");
if (i == ids.Length - )
{
string s2 = sb.ToString();
s2 = s2.Remove(s2.Length - ) + ")";
lt.Add(s2);
}
}
else
{
sb.Append(ids[i] + ",");
if (i == ids.Length - )
{
string s = sb.ToString();
s = s.Remove(s.Length - ) + ")";
lt.Add(s);
}
}
}
sqls = lt.ToArray();
return sqls;
}
调用
static void Main(string[] args)
{
List<string> list = new List<string>();
for (int i = ; i < ; i++)
{
list.Add(i.ToString());
}
string[] sqls = CreateSQL(list.ToArray(), "select * from tb_spkc where id in(", );
Console.ReadKey();
}
效果

c#解决数据库用in的时候大于1000报错问题的更多相关文章
- 解决Windows下运行php Composer出现SSL报错的问题
		解决Windows下运行php Composer出现SSL报错的问题 2015-01-14 20:05 在windows下运行composer却出现SSL报错: E:\www>php -f ... 
- 解决Homestead yarn , npm run dev, 命令报错问题!
		解决Homestead yarn , npm run dev, 命令报错问题! 2018年06月01日 11:50:51 偶尔发发颠 阅读数:1654 版权声明:本文为博主原创,未经博主同意,不 ... 
- 如何解决spring boot 项目导入依赖后代码报错问题
		如何解决spring boot 项目导入依赖后代码报错问题 2020-08-15 14:17:18 代码截图如图所示(由于本人问题已经解决,没来得及截图,所以在网上找了一张图片) 
- Sqlite 数据库出现database disk image is malformed报错的解决方法
		软件用的是Sqlite数据库,昨天还好好的,今天开机登录软件报错:database disk image is malformed 用Sqlite Expert Personal 重建索引,发现其中一 ... 
- mysql数据库表字段使用DESC等关键字报错及解决方法
		<!-- desc是MySQL数据库的关键字,作为字段名直接使用会报错 --><sql id="Base_Column"> id,mol,ip,port,n ... 
- 解决 U盘安装Windows Server 2012 R2 报错 Windows 无法打开所需的文件 Sources\install.wim
		报错原因: 使用UltraISO等软件刻录镜像时默认使用FAT32文件系统,该系统不支持大于4G的文件, 而Server 2012 R2的安装文件install.wim为5.12G,固安装失败. 解决 ... 
- 安装完oracle11g_2x64位数据库后使用PL Developer链接oracle报错“请确认是否安装了32位oracle和TNS错误”解决方案
		解决使用PL Developer登录oracle报错没有配置TNS错误.首先安装好oracle11g数据库,安装PL developer32位 1,下载“instantclient-basic-win ... 
- 解决Mysql搭建成功后执行sql语句报错以及区分大小写问题
		刚搭建完mysql 8.0以后会: 一.表区分大小写, 二.执行正确的sql语句成功且会报:[Err] 1055 - Expression #1 of ORDER BY clause is not i ... 
- 解决小米手机USB安装apk时AS报错:INSTALL_FAILED_USER_RESTRICTED
		今天,直接用AS在小米手机上运行安装的时候总是报错:INSTALL_FAILED_USER_RESTRICTED,于是乎,通过以下方式解决: 在开发者选项将USB安装打开,然后,哈,解决了.记录一下. 
随机推荐
- [struts2]开启struts2开发模式
			<constant name="struts.devMode" value="true" /> 
- git 秘钥的生成
			在命令查看自己的秘钥还是公钥 cat .ssh/id_rsa.pub/cat .ssh/id_rsa 
- ABAP程序相互调用--SUBMIT
			(1) 调用其他程序: * Trigger the IDOC SUBMIT zpcppmd001_idoc AND RETURN. (2) 调用其他程序参数传递参数: *当被调用的程序的屏幕有输入参数 ... 
- ApkTool动态打包
			引言: APK在推广的时候可能会须要动态打包APK.比方公布到不同渠道的时候,须要在manifest文件里改动渠道信息.或者app在推广的时候.须要在apk包里面加上推广人信息等. 环境变量: 1.J ... 
- Citrix服务器虚拟化之三十 XenApp 6.5发布流式应用程序
			Citrix服务器虚拟化之三十 XenApp 6.5发布流式应用程序 XenApp可发布以下类型的资源向用户提供信息访问,这 ... 
- <BEA-141281> <unable to get file lock, will retry ...>  --reference
			I ran into this error the first time I restarted Weblogic on one of my installs, the only reference ... 
- 如何在 Objective-C 的环境下实现 defer
			关注仓库,及时获得更新:https://github.com/draveness/iOS-Source-Code-Analyze Follow: https://github.com/Dravenes ... 
- Cookie中用户登录信息的提示
			public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSe ... 
- input 的 placeholder属性在IE8下的兼容处理
			placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿 ... 
- sql 随机生成中文名字
			,) )) -- 姓氏 ,) )) -- 名字 INSERT @fName VALUES ('赵'),('钱'),('孙'),('李'),('周'),('吴'),('郑'),('王'),('冯'),( ... 
