我有这段代码:
Private Sub bbSaveloc_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbSaveloc.ItemClick
gvLocations.CloseEditor()
gvLocations.UpdateCurrentRow()
'standorte speichern
fb.Connect()
fb.ReadyForData()
fb.Command.Parameters.AddWithValue("@company_id", company_id)
'evtl. vorhandene location_id-s holen
fb.Command.CommandText = "select location_id from locations where company_id=@company_id order by location_id"
Try
fb.Datareader = fb.Command.ExecuteReader
Catch ex As FirebirdSql.Data.FirebirdClient.FbException
XtraMessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
fb.Close()
Exit Sub
End Try
Dim index As Integer = 0
If fb.Datareader.HasRows Then
While fb.Datareader.Read
fb.Dataset.Tables(0).Rows(index).Item(0) = fb.Datareader.Item(0)
index = index + 1
End While
End If
'insert
fb.Command.Parameters.Add("@location_id", FirebirdSql.Data.FirebirdClient.FbDbType.Integer, 0, "LOCATION_ID")
fb.Command.Parameters.Add("@name", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 50, "NAME")
fb.Command.Parameters.Add("@postal_code", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 10, "POSTAL_CODE")
fb.Command.Parameters.Add("@road_nr", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 100, "ROAD_NR")
fb.Command.CommandText = "insert into locations values ((SELECT NEXT VALUE FOR GEN_LOCATION_ID FROM RDB$DATABASE), @company_id, @name, @postal_code, " & _
"@road_nr)"
fb.Dataadapter.InsertCommand = fb.Command
If Not savefirst Then
'update
fb.Command.CommandText = "update locations set name=@name, postal_code=@postal_code, road_nr=@road_nr where location_id=@location_id and company_id=@company_id"
fb.Dataadapter.UpdateCommand = fb.Command
'delete
fb.Command.CommandText = "delete from locations where company_id=@company_id"
fb.Dataadapter.DeleteCommand = fb.Command
End If
fb.Dataadapter.Update(fb.Dataset.Tables(0))
savefirst = False
End Sub
我有以下问题:
我在gridview中插入一行,然后保存它 - >好。 然后我在gridview中修改了这一行的一些值,然后我保存了它 - >该行将被删除,不会更新为例外。
我已检查此行的RowState
,已对其进行了修改。所以我不会解散,为什么它会删除该行。
如果我没有初始化DeleteCommand
,则更新将作为例外工作。
修改
我现在重写了代码:
'standort datasource initialisieren
fb_loc.Connect()
fb_loc.ReadyForData()
'select
fb_loc.Command.Parameters.AddWithValue("@company_id", company_id)
fb_loc.Command.CommandText = "select * from locations where company_id=@company_id"
fb_loc.Dataadapter.SelectCommand = fb_loc.Command
Try
fb_loc.Dataadapter.Fill(fb_loc.Dataset, "locations")
Catch ex As FirebirdSql.Data.FirebirdClient.FbException
XtraMessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
fb_loc.Close()
Exit Sub
End Try
gcLocations.DataSource = fb_loc.Dataset.Tables(0)
'insert
Dim insert_command As New FbCommand("insert into locations values ((SELECT NEXT VALUE FOR GEN_LOCATION_ID FROM RDB$DATABASE), @company_id, @name, @postal_code, " & _
"@road_nr)", fb_loc.Connection)
insert_command.Parameters.AddWithValue("@company_id", company_id)
insert_command.Parameters.Add("@location_id", FirebirdSql.Data.FirebirdClient.FbDbType.Integer, 0, "LOCATION_ID")
insert_command.Parameters.Add("@name", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 50, "NAME")
insert_command.Parameters.Add("@postal_code", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 10, "POSTAL_CODE")
insert_command.Parameters.Add("@road_nr", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 100, "ROAD_NR")
fb_loc.Dataadapter.InsertCommand = insert_command
'update
Dim update_command As New FbCommand("update locations set name=@name, postal_code=@postal_code, road_nr=@road_nr where location_id=@location_id and company_id=@company_id", _
fb_loc.Connection)
update_command.Parameters.AddWithValue("@company_id", company_id)
update_command.Parameters.Add("@location_id", FirebirdSql.Data.FirebirdClient.FbDbType.Integer, 0, "LOCATION_ID")
update_command.Parameters.Add("@name", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 50, "NAME")
update_command.Parameters.Add("@postal_code", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 10, "POSTAL_CODE")
update_command.Parameters.Add("@road_nr", FirebirdSql.Data.FirebirdClient.FbDbType.VarChar, 100, "ROAD_NR")
fb_loc.Dataadapter.UpdateCommand = update_command
'delete
Dim delete_command As New FbCommand("delete from locations where company_id=@company_id", fb_loc.Connection)
delete_command.Parameters.AddWithValue("@company_id", company_id)
fb_loc.Dataadapter.DeleteCommand = delete_command
fb_loc.Close()
然后是保存方法:
Private Sub bbSaveloc_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbSaveloc.ItemClick
gvLocations.CloseEditor()
gvLocations.UpdateCurrentRow()
fb_loc.Dataadapter.Update(fb_loc.Dataset.Tables(0))
End Sub
插入有效,但是f。即更新不是。我得到了一个dbconcurrency异常。
我假设,我应该将生成的location_id
自动增量值返回到数据集。由于缺少location_id
值,因此无法执行更新。但是如何在数据表中找回这些值?
答案 0 :(得分:1)
您正在使用一个命令进行更新和删除命令:
'update
fb.Command.CommandText = "update locations set name=@name, postal_code=@postal_code, road_nr=@road_nr where location_id=@location_id and company_id=@company_id"
fb.Dataadapter.UpdateCommand = fb.Command
'delete
fb.Command.CommandText = "delete from locations where company_id=@company_id"
fb.Dataadapter.DeleteCommand = fb.Command
所以最后的作业"胜出",删除行动。
相反,您需要为每个操作创建不同的命令。