简体   繁体   English

更新没有主键的表的一组行

[英]Update a set of rows of a table having no primary key

I want to update set of rows using ODP.net.我想使用 ODP.net 更新行集。 I want to get list of employees based on increasing asscending order of PRIORITY and update PRIORITY column with value 1 and increment by 1. I tried below approach and it gives error "Dynamic SQL generation failed. No key information found"我想根据 PRIORITY 的升序获取员工列表,并用值 1 更新 PRIORITY 列并以 1 递增。我尝试了以下方法,但出现错误“动态 SQL 生成失败。未找到关键信息”

StrQuery = "SELECT * FROM EMPLOYEE WHERE DEPT ='" + dept + "' ORDER BY PRIORITY";
DataTable dt = new DataTable();
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = new OracleCommand(StrQuery, ora);
OracleCommandBuilder cb = new OracleCommandBuilder(da);
da.Fill(dt);
intNpriority = 1;
foreach (DataRow row in dt.Rows)
{
    row["PRIORITY"] = intNpriority;
    intNpriority = intNpriority + 1;
}
da.UpdateCommand = cb.GetUpdateCommand();
da.Update(dt);

The table has no primary key and i cannot add one now.该表没有主键,我现在无法添加。 Can i add custom update query and how?我可以添加自定义更新查询吗?如何添加? Is there anyalternative way to acheive same?有没有其他方法可以实现相同的目标?

table strucure:表结构:

column name   |  (data type)
Employee_name |  (varchar2)
dept          |  (varchar2)
PRIORITY      | (NUMBER)

I've spent two days to find some way to workaround.我花了两天时间寻找解决方法。 Lucky that I could get it done.幸运的是我能完成它。 The idea is to add a dump primary key in the DataTable, this new dump column does not affect the real database table.思路是在DataTable中增加一个dump主键,这个新的dump列不影响真实的数据库表。 Here's my code sample这是我的代码示例

Dim currentRow = m_DataTable.Rows(m_RowPosition) Dim tmpRows() As DataRow = {currentRow} Dim currentRow = m_DataTable.Rows(m_RowPosition) Dim tmpRows() As DataRow = {currentRow}

        Dim cmdBuilder As OracleCommandBuilder = New OracleCommandBuilder(m_Adapter)
        
        **If m_DataTable.PrimaryKey.Count = 0 Then
            m_DataTable.Columns.Add(New DataColumn("_ID_", System.Type.GetType("System.String")))
            m_DataTable.Columns("_ID_").Unique = True
        End If**

        m_Adapter.Update(tmpRows)

Note: The currentRow is the row is being edited .注意:currentRow 是正在编辑的行。 Please consider carefully to apply this tip because it might update all data of table.请慎重考虑应用此提示,因为它可能会更新表的所有数据。 It's suitable for case your table has only one row ( some kind of configuration data )它适用于您的表只有一行(某种配置数据)的情况

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

相关问题 使用唯一随机值更新 sql 表中的所有行,而不使用 c# 中的主键或唯一键 - Update all rows in sql table with unique random value without using primary key or unique key in c# 使用在多个列上具有主键的实体框架更新数据库 - Update Database using Entity Framework having primary key on multiple Columns 如何用petapoco更新另一个表中的外键主键? - how to update a primary key that is also a foreign key in another table with petapoco? 删除行时更新其他行的主键列 - Update primary key column of other rows when a row is deleted 实体框架核心读取和更新多行-复合主键 - Entity Framework Core Read & Update Multiple rows - Composite Primary Key 是否可以将非主键设置为另一个表中的外键? - Is it possible to set a non primary key as foreign key in another table? 实体框架6即使具有主键也不会在表中插入行 - Entity Framework 6 not inserting Rows in table even though has primary key 无法更新,因为没有主键 - Cannot update as there is no primary key 如何使用具有相同主键的本地数据库表更新远程表? - How to update remote tables with local database table with same primary key? 根据主键以外的ID更新表 - Update table based on ID other than Primary Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM