私はこれを初めてやっています。私はいくつかのガイダンスが必要です。私はDatagridviewsをデータソースに設定しました。今、レコードをDBから適切に更新または挿入するにはどうすればいいですか?Datagridview - 更新/挿入の適切なワット
私の質問:
現在、私のような/アップデートの追加/削除(Datagridviews下のいずれかのボタンを持っていけない - 私は間違いなくそれらを必要としたりせずに、すべてのアクションを処理することが可能であるか - Iドン? TableAdapterManagerを使用しないでください)
MERGEステートメント - 特定のテーブルとDatagridviewを比較し、同じテーブルに更新または挿入を実行するために使用できますか?
手動でINSERTまたはUPDATEを実行する場合は、まず何が行われ、何を行う必要があるかを確認する方法はありますか?
私は出回っ両方を行うためのコードを持っていますが、それが必要として動作していない、問題は、私は1つだけ、フォーム上のデータを保存するためのボタンがあるということです - そしてそのボタンをDBにデータを挿入または更新する必要がありフォーム上の2 Datagridviews + Databinded Textboxから。したがって、基本的に同じフォームで更新/挿入できる3つのテーブルがあります - Datagridviewsは関連データを表示し、Databinded Texboxesはマスターテーブルレコードを表示します。
ご迷惑をおかけして申し訳ございません。
EDIT(ここでは私の完全なコードです - 最初のデータをロードするにはBtn_Saveのコードは、その後、フォームおよびデータグリッドに):
Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types
Imports System.IO
Imports System
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form2
Private da, da1 As OracleDataAdapter
Private cb, cb1 As OracleCommandBuilder
Private ds, ds1 As DataSet
Public OpenedForm1 As Form1 'required for obtaining record ID - this form opens when user double clicks on datagridview record
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BtnSave.Enabled = False 'for eliminating errors when using dataset Update (Oracle recommendation)
Dim SQL1 As String = "SELECT * from TABLE1 WHERE ID_Table1=" & OpenedForm1.DataGridView1.CurrentRow.Cells(0).Value.ToString
Dim DGV1_SQL As String = "SELECT ID_TABLE2, ID_TABLE1, Name, Surname, WHERE ID_TABLE1=" & OpenedForm1.DataGridView1.CurrentRow.Cells(0).Value.ToString
Dim DGV2_SQL As String = "SELECT TABLE3.SERIAL, TABLE3.MODEL," _
& " TABLE3.TYPE FROM TABLE3 INNER JOIN (TABLE1 INNER JOIN TABLE1_TABLE3 ON TABLE1.ID_Table1=TABLE1_TABLE3.ID_Table1_FK) ON " _
& "TABLE3.ID_Table3=TABLE1_TABLE3.ID_Table3_FK WHERE TABLE1_TABLE3.ID_TABLE1_FKK=" & Openedform1.DataGridView1.CurrentRow.Cells(0).Value.ToString
Try
Oracleconn()
'Then I do all DB queries and display results on form
'For SQL1 Databind all textboxes – this is a master record on form
'For DGV2_SQL and DGV2_SQL fill datasets and set Datagrids to datasource
'I'm also hiding all primary key fields and bounding datagrids to Binding sources to enable Binding navigators under them.
End Sub
Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
'First I assign some variables for Oracle parameters, such as dealing with Null Date values
Oracleconn() 'My connection to DB
Using cmd As OracleCommand = New OracleCommand()
cmd.Connection = Oracleconn()
cmd.Parameters.Clear()
'Adding a lot of paramters here for master record like this...
cmd.Parameters.Add(New OracleParameter("ID", TxtID.Text))
))
'Only Update for master record – Textboxes on form – works just fine
cmd.CommandText = "UPDATE TABLE1" _
& " SET ID_TABLE1= : id .etc
If Not (TxtSerial.Text = "" Or TxtInventar.Text = "11111" Or TxtInventar.TextLength < 9) Then
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
'Then checking 1st datagridview for data in It
For Each row As DataGridViewRow In DGV1.Rows
If row.Cells(1).Value <> Nothing Then
'Adding parameters again
'Then my attempt for doing update or Insert with MERGE – doesn't work
cmd.CommandText = "MERGE INTO TABLE2 v" _
& "USING (SELECT * FROM TABLE2 WHERE ID_TABLE2='" & row.Cells(1).Value & "') u" _
& " ON u.ID_TABLE2" _
& " WHEN MATCHED THEN UPDATE SET all fields except ID's " _
' & " WHEN NOT MATCHED BY v" _
' & " THEN INSERT... all fields" _
' & " VALUES.... from parameters"
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
'This was used before, but ofcourse It only updates
da.Update(ds.Tables(0))
BtnSave.Enabled = True
Else
MsgBox("You cannot save without 1st field empty !")
Exit For
End If
Next
'Then check for next Datagrid – this one is hard, It's a joined Table(entitity) from Table1 and Table3, with showing data from that joined Table too
For Each row As DataGridViewRow In DGV2.Rows
'Adding parameters and just performing update – didn't start the Insert so far
MsgBox("Saved.", MsgBoxStyle.Information, "Editing records")
'Refresh Datagridview in Form1 - which is also opened
OpenedForm1.BtnSearch()
Else
MsgBox("1st field is required to be filled If you want to save.", MsgBoxStyle.Exclamation, "Editing records")
End If
End Using
OracleClose() 'close my connection
End Sub
Ple aseはあなたのコードを示しています。たとえそれが動作しなくても。 –
@Alexb、Ok、ちょっと時間が必要です。すぐ戻るでしょう。 – LuckyLuke82
@AlexB。ここには、私はそれを変更するには時間をかけなければならなかった、私の元のコードは投稿するには長すぎます。 – LuckyLuke82