[英]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.