ODP.NET 存储过程解惑 关于关键字

evil812677 2013-01-23 01:49:47
下面代码能实现把数组 WELL_ID,REF_WELL_ID 插入到 td_bas_well 表中,
请问各位,怎样以同样的方式实现更新呢?
网上说可以执行更新,但是我试了很多次都失败了,
这是网址:http://blog.csdn.net/hawksoft/article/details/7528384

string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

OracleCommand theCmmd = new OracleCommand("insert into td_bas_well (WELL_ID,REF_WELL_ID) values(:WELL_ID,:REF_WELL_ID)", ConnPool.GetTargetConn().Conn);
theCmmd.ArrayBindCount = WELL_ID.Length;//关键点
theCmmd.Parameters.Add(new OracleParameter("WELL_ID", OracleDbType.Varchar2, WELL_ID, System.Data.ParameterDirection.Input));
theCmmd.Parameters.Add(new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input));
int count = theCmmd.ExecuteNonQuery();
...全文
245 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
evil812677 2013-01-23
  • 打赏
  • 举报
回复
对了如果以后有人碰到了相同的问题,还有一点要提醒大家: 在创建参数时,参数顺序必须与SQL语句的顺序相同 否则不会执行,且会返回0
evil812677 2013-01-23
  • 打赏
  • 举报
回复
引用 8 楼 qldsrx 的回复:
你INSERT和UPDATE的SQL语句难道是一样的?顶楼还说那样执行不行,现在又可以了,我真搞不懂你的问题代码倒底是哪个了。
搞定了,其实我代码是对的,没睡觉,脑子有点糊涂 正其他数据库上了 谢谢你的耐心指导!
evil812677 2013-01-23
  • 打赏
  • 举报
回复
以下是Insert,插入成功 string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; OracleCommand theCmmd = new OracleCommand("insert into td_bas_well (WELL_ID,REF_WELL_ID) values(:WELL_ID,:REF_WELL_ID)", ConnPool.GetTargetConn().Conn); theCmmd.ArrayBindCount = WELL_ID.Length;//关键点 theCmmd.Parameters.Add(new OracleParameter("WELL_ID", OracleDbType.Varchar2, WELL_ID, System.Data.ParameterDirection.Input)); theCmmd.Parameters.Add(new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input)); 在以下是更新,更新失败: string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; OracleCommand cmd = new OracleCommand("update td_bas_well set REF_WELL_ID = :REF_WELL_ID where WELL_ID = :WELL_ID", ConnPool.GetTargetConn().Conn); cmd.ArrayBindCount = WELL_ID.Length; cmd.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("REF_WELL_ID",OracleDbType.Varchar2)); cmd.Parameters["REF_WELL_ID"].Direction = System.Data.ParameterDirection.Input; cmd.Parameters["REF_WELL_ID"].Value = REF_WELL_ID; cmd.Parameters.Add(new Oracle.DataAccess.Client.OracleParameter("WELL_ID", OracleDbType.Char)); cmd.Parameters["WELL_ID"].Direction = System.Data.ParameterDirection.Input; cmd.Parameters["WELL_ID"].Value = WELL_ID; int i = cmd.ExecuteNonQuery();
qldsrx 2013-01-23
  • 打赏
  • 举报
回复
你INSERT和UPDATE的SQL语句难道是一样的?顶楼还说那样执行不行,现在又可以了,我真搞不懂你的问题代码倒底是哪个了。
evil812677 2013-01-23
  • 打赏
  • 举报
回复
引用 6 楼 qldsrx 的回复:
由于官方只给了int[]类型的参数举例,而没有给varchar2类型的参数类型,而我也没有用过这中怪异的方式,所以具体测试你得自己来,对于非定长数据类型,处理方式只有文字性描述而没有示例,只能自己测试了,你要是只想成功运行一次,将参数改为定长数据类型,应该能成功。
可是我执行insert成功了啊?下面是代码,插入的也是string[]型的,类型也是Varchar2,成功运行,并返回结果10,这样是怎么回事儿?是不是问题出现在其他地方?麻烦了 string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; OracleCommand theCmmd = new OracleCommand("insert into td_bas_well (WELL_ID,REF_WELL_ID) values(:WELL_ID,:REF_WELL_ID)", ConnPool.GetTargetConn().Conn); theCmmd.ArrayBindCount = WELL_ID.Length;//关键点 theCmmd.Parameters.Add(new OracleParameter("WELL_ID", OracleDbType.Varchar2, WELL_ID, System.Data.ParameterDirection.Input)); theCmmd.Parameters.Add(new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input));
qldsrx 2013-01-23
  • 打赏
  • 举报
回复
由于官方只给了int[]类型的参数举例,而没有给varchar2类型的参数类型,而我也没有用过这中怪异的方式,所以具体测试你得自己来,对于非定长数据类型,处理方式只有文字性描述而没有示例,只能自己测试了,你要是只想成功运行一次,将参数改为定长数据类型,应该能成功。
qldsrx 2013-01-23
  • 打赏
  • 举报
回复
错了,我的意思是,那个 string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; 要改为定长的字符串,比如都2位,不足补空格: string[] WELL_ID = new string[] { "1 ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10" }; 你也可以试试指定OracleParameter的Size属性,但总之定长是必须的。而那个ArrayBindSize必须是int类型的,没让你用数组。
evil812677 2013-01-23
  • 打赏
  • 举报
回复
引用 3 楼 qldsrx 的回复:
关键点不是那个ArrayBindCount,而是那个字符串数组的给定,由于最终传说的是字节数组,而字节数组是没有字符串的分割标志的,它不知道数组中每个字符串有多长,那么它就根据字符串数组的长度(ArrayBindCount指定),自动推测每个字符串的长度(定长),但是你给的字符串长短不一(那个“10”是2位的),因此怎么都不可能成功了。
你好,你的意思是只要把 theCmmd.Parameters.Add(new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input)); 改成: OracleParameter op = new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input); op.ArrayBindSize = new int []{2,2,2,2,2,2,2,2,2,2}; theCmmd.Parameters.Add(op); 可是即使给OracleParameter 设置了ArrayBindSize ,也执行不了啊?
qldsrx 2013-01-23
  • 打赏
  • 举报
回复
关键点不是那个ArrayBindCount,而是那个字符串数组的给定,由于最终传说的是字节数组,而字节数组是没有字符串的分割标志的,它不知道数组中每个字符串有多长,那么它就根据字符串数组的长度(ArrayBindCount指定),自动推测每个字符串的长度(定长),但是你给的字符串长短不一(那个“10”是2位的),因此怎么都不可能成功了。
evil812677 2013-01-23
  • 打赏
  • 举报
回复
这是我改后的Update代码 可是返回值为0,百思不得其解 string[] WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; string[] REF_WELL_ID = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; OracleCommand theCmmd = new OracleCommand("update td_bas_well set REF_WELL_ID = :REF_WELL_ID where WELL_ID = :WELL_ID", ConnPool.GetTargetConn().Conn); theCmmd.ArrayBindCount = WELL_ID.Length;//关键点 theCmmd.Parameters.Add(new OracleParameter("WELL_ID", OracleDbType.Varchar2, WELL_ID, System.Data.ParameterDirection.Input)); theCmmd.Parameters.Add(new OracleParameter("REF_WELL_ID", OracleDbType.Varchar2, REF_WELL_ID, System.Data.ParameterDirection.Input)); int count = theCmmd.ExecuteNonQuery();
evil812677 2013-01-23
  • 打赏
  • 举报
回复
Hello? 有人能帮帮我么?

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧