简体   繁体   English

从.net逐行更新db2中的表

[英]update a table in db2 row by row from .net

I perform some actions on a table retrieved from a db2 database row by row in a C# project. 我在C#项目中逐行从db2数据库检索的表上执行一些操作。 but when I try to update it using cursors row by row to the database using the following statement an exception is generated: 但是,当我尝试使用以下语句使用游标将游标逐行更新到数据库时,会生成异常:

selectCommand.CommandText = "DECLARE crsr1 CURSOR FOR select * from " + tableName+" ;" 

From my understanding of this, using cursors statements of db2 is not supported in .net languages. 据我了解,.net语言不支持使用db2的游标语句。 I am connected to the database using IBM DB2 ODBC drivers. 我使用IBM DB2 ODBC驱动程序连接到数据库。

Is there is any way around using cursors , or if someone can tell whether there is something I am missing here. 是否有任何方法可以使用游标,或者是否有人可以告诉我这里是否缺少某些内容。

I did a lot of searching on net , but not much is available on this problem. 我在网上做了很多搜索,但是在这个问题上没有多少可用。

this is the exception I got for the above statement -- 这是我在上述声明中得到的例外-

"ERROR [42601] [IBM][CLI Driver][DB2/NT] SQL0104N  An unexpected token \"DECLARE CRSR1 CURSOR FOR select * from MYTA\" was found following \"BEGIN-OF-STATEMENT\".  
Expected tokens may include:  \"<space>\".  SQLSTATE=42601\r\n"

any help or suggestions are highly appreciated. 任何帮助或建议都将受到高度赞赏。

我认为您可能希望将其放入proc-通常不会在存储的proc之外执行“声明游标..” {除非您在CLI或其他工具中进行调试}

You should just put the "SELECT" part in the command text and use ExecuteResultSet method of DB2Command to return a resultset that wraps a scrollable updatable cursor. 您应该只将“ SELECT”部分放在命令文本中,并使用DB2Command的ExecuteResultSet方法返回包含可滚动可更新游标的结果集。

selectCommand.CommandText = "select * from " + tableName+" ;"
using(var result = selectCommand.ExecuteResultSet(DB2ResultSetOptions.Updatable))
{
   while (result.Read())
   {
       //set value of 2nd column to 'Hello'
       result.SetString(1, "Hello");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM