C#使用Oracle.ManagedDataAccess.Client如何获得执行后的影响行数?

save____ 2014-08-27 03:53:31
我的C#代码:

int count = 0;
OracleCommand cmd = new OracleCommand();
OracleConnection oracleConn = null;
try
{
oracleConn = new OracleConnection(conn);
cmd.Connection = oracleConn;
oracleConn.Open();

//string dateString = date.ToString();

string sql = @"
Declare
COUNTER number;
Begin
SELECT count(0) into COUNTER FROM tb WHERE
pubtime = '{0}' and name='ABC';
if COUNTER < 1 then
INSERT INTO tb(name, text, pubtime, time)
values('ABC','{1}', '{0}', to_date('{2}', 'yyyy-mm-dd hh24:mi:ss'));
end if;
END;";

cmd.CommandText = string.Format(sql, ((DateTime)date).ToString("yyyy-MM-dd HH:mm:ss"), text, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

int ret = cmd.ExecuteNonQuery();
if (ret > 0) count++;
}
catch (Exception ex)
{

}
finally
{
oracleConn.Close();
}
return count;


使用ExecuteNonQuery 返回的都是-1, 是因为使用的Declare Begin End;的原因吗? 有办法获得影响行数吗?
求大神们给小弟解答下。
...全文
1189 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
save____ 2014-08-28
  • 打赏
  • 举报
回复
大神好厉害
引用 3 楼 bw555 的回复:
调整下语句,不要begin end了,估计就好了
string sql = @"
INSERT INTO tb(name, text, pubtime, time)
select 'ABC','{1}', '{0}', to_date('{2}', 'yyyy-mm-dd hh24:mi:ss') from dual
where 1>(SELECT count(0)  FROM tb
      WHERE pubtime = '{0}' and name='ABC' )“
bw555 2014-08-27
  • 打赏
  • 举报
回复
调整下语句,不要begin end了,估计就好了
string sql = @"
INSERT INTO tb(name, text, pubtime, time)
select 'ABC','{1}', '{0}', to_date('{2}', 'yyyy-mm-dd hh24:mi:ss') from dual
where 1>(SELECT count(0)  FROM tb
      WHERE pubtime = '{0}' and name='ABC' )“
bw555 2014-08-27
  • 打赏
  • 举报
回复
OracleCommand.ExecuteNonQuery 方法 返回值类型:System.Int32 对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。 对于 CREATE TABLE 和 DROP TABLE 语句,返回值为 0。 对于其他所有类型的语句,返回值为 -1。
bw555 2014-08-27
  • 打赏
  • 举报
回复
这功能貌似只能在oracle的存储过程中获取了 参考 http://www.2cto.com/database/201303/196840.html

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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