Не добавляются записи в таблицу БД Access

Xellera

Помогите решить проблему, при отладке не добавляются записи в таблицу, а вот изменять можно Imports System.Windows.Forms
Public Class Form1
    Public strCon As String
    Public con As OleDb.OleDbConnection
    Public daPar As OleDb.OleDbDataAdapter
    Public daChd As OleDb.OleDbDataAdapter
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\bd.accdb'"
        con = New OleDb.OleDbConnection(strCon)
        Try
            con.Open()
            Console.WriteLine("Соединение установлено успешно")
        Catch ex As Exception
            Console.WriteLine("Соединение не установлено")
        End Try
 
 
        Dim SelectCommand1 As New OleDb.OleDbCommand("Select CID,Name, Sname, Oname from Customers", con)
        Dim SelectCommand2 As New OleDb.OleDbCommand("Select CID,OID,Order_Date from Orders", con)
 
        daPar = New OleDb.OleDbDataAdapter(SelectCommand1)
        daChd = New OleDb.OleDbDataAdapter(SelectCommand2)
 
 
 
        Dim InsertCommand1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("Insert into Customers(CID,Name, Sname, Oname) values(:CID ,:Name, :Sname, :Oname) ", con)
        daPar.InsertCommand = InsertCommand1
 
        InsertCommand1.Parameters.Add(New OleDb.OleDbParameter("sname", _
        System.Data.OleDb.OleDbType.VarChar, 30, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "sname", System.Data.DataRowVersion.Current, Nothing))
 
        InsertCommand1.Parameters.Add(New OleDb.OleDbParameter("name", _
        System.Data.OleDb.OleDbType.VarChar, 30, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "name", System.Data.DataRowVersion.Current, Nothing))
 
        InsertCommand1.Parameters.Add(New OleDb.OleDbParameter("oname",
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input,
        True, CType(0, Byte), CType(0, Byte), "oname", System.Data.DataRowVersion.Current, Nothing))
 
        InsertCommand1.Parameters.Add(New OleDb.OleDbParameter("CID",
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input,
        True, CType(0, Byte), CType(0, Byte), "CID", System.Data.DataRowVersion.Current, Nothing))
 
 
        Dim UpdateCommand1 As New OleDb.OleDbCommand("Update Customers set name = :name,sname = :sname,oname = :oname where CID = :CID", con)
        daPar.UpdateCommand = UpdateCommand1
 
        UpdateCommand1.Parameters.Add(New OleDb.OleDbParameter("sname", _
        System.Data.OleDb.OleDbType.VarChar, 30, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "sname", System.Data.DataRowVersion.Current, Nothing))
 
        UpdateCommand1.Parameters.Add(New OleDb.OleDbParameter("name", _
        System.Data.OleDb.OleDbType.VarChar, 30, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "name", System.Data.DataRowVersion.Current, Nothing))
 
        UpdateCommand1.Parameters.Add(New OleDb.OleDbParameter("oname", _
        System.Data.OleDb.OleDbType.VarChar, 30, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "oname", System.Data.DataRowVersion.Current, Nothing))
 
        UpdateCommand1.Parameters.Add(New OleDb.OleDbParameter("CID",
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input,
        True, CType(0, Byte), CType(0, Byte), "CID", System.Data.DataRowVersion.Current, Nothing))
 
 
        Dim InsertCommand2 As OleDb.OleDbCommand = New OleDb.OleDbCommand("Insert into Orders(CID,Order_Date) values(:CID,:Order_Date)", con)
        daChd.InsertCommand = InsertCommand2
 
        InsertCommand2.Parameters.Add(New OleDb.OleDbParameter("cid", _
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "cid", System.Data.DataRowVersion.Current, Nothing))
 
        InsertCommand2.Parameters.Add(New OleDb.OleDbParameter("ORDER_DATE", _
        System.Data.OleDb.OleDbType.Date, 10, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "ORDER_DATE", System.Data.DataRowVersion.Current, Nothing))
 
        InsertCommand2.Parameters.Add(New OleDb.OleDbParameter("OID",
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input,
        True, CType(0, Byte), CType(0, Byte), "OID", System.Data.DataRowVersion.Current, Nothing))
 
        Dim UpdateCommand2 As New OleDb.OleDbCommand("Update Orders set Name = :name , SName = :sname,FatherID = :FatherID,MotherID = :MotherID,GrouCID = :GrouCID where OID = :OID", con)
        daChd.UpdateCommand = UpdateCommand2
 
        UpdateCommand2.Parameters.Add(New OleDb.OleDbParameter("cid", _
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "cid", System.Data.DataRowVersion.Current, Nothing))
 
        UpdateCommand2.Parameters.Add(New OleDb.OleDbParameter("ORDER_DATE", _
        System.Data.OleDb.OleDbType.Date, 10, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "ORDER_DATE", System.Data.DataRowVersion.Current, Nothing))
 
        UpdateCommand2.Parameters.Add(New OleDb.OleDbParameter("OID",
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input,
        True, CType(0, Byte), CType(0, Byte), "OID", System.Data.DataRowVersion.Current, Nothing))
 
        Dim DeleteCommand1 As New OleDb.OleDbCommand("Delete from Customers where CID = :CID", con)
 
        DeleteCommand1.Parameters.Add(New OleDb.OleDbParameter("CID", _
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "CID", System.Data.DataRowVersion.Current, Nothing))
        daPar.DeleteCommand = DeleteCommand1
 
        Dim DeleteCommand2 As New OleDb.OleDbCommand("Delete from Orders where OID = :OID", con)
 
        DeleteCommand2.Parameters.Add(New OleDb.OleDbParameter("OID", _
        System.Data.OleDb.OleDbType.Integer, 5, System.Data.ParameterDirection.Input, _
        True, CType(0, Byte), CType(0, Byte), "OID", System.Data.DataRowVersion.Current, Nothing))
        daChd.DeleteCommand = DeleteCommand2
 
        daPar.Fill(DataSet1, "Customers")
 
        daChd.Fill(DataSet1, "Orders")
        con.Close()
 
    End Sub
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If DataSet1.HasChanges Then
                daPar.Update(DataSet1, "Customers")
                daChd.Update(DataSet1, "Orders")
            Else
                MessageBox.Show("изменения отсутствуют")
            End If
        Catch ex As Exception
            MessageBox.Show("данные обновить не удалось")
 
        End Try
 
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DataSet1.RejectChanges()
    End Sub
 
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
 
    End Sub
 
    Private Sub CustomersBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles CustomersBindingSource.CurrentChanged
 
    End Sub
End Class
Полазив, заметил что добавляется запись, если во всех полях цифры
1 ответ

Xellera

Dim InsertCommand1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("Insert into Customers(CID,Name, Sname, Oname) values(@CID , @Name, @Sname, @Oname) ", con)
        
daPar.InsertCommand = InsertCommand1
 
Dim param as OleDBParameter
 
With  InsertCommand1
 
    param=.Parameters.Add(New OleDb.OleDbParameter("@CID", System.Data.OleDb.OleDbType.Integer, 5)
    param.Direction = ParameterDirection.Input
    param.SourceColumn = "CID"
    param.SourceVersion = DataRowVersion.Current
 
    param=.Parameters.Add(New OleDb.OleDbParameter("@Name", System.Data.OleDb.OleDbType.VarChar, 30)
    param.Direction = ParameterDirection.Input
    param.SourceColumn = "Name"
    param.SourceVersion = DataRowVersion.Current
 
    param=.Parameters.Add(New OleDb.OleDbParameter("@Sname", System.Data.OleDb.OleDbType.VarChar, 30)
    param.Direction = ParameterDirection.Input
    param.SourceColumn = "Sname"
    param.SourceVersion = DataRowVersion.Current
 
    param=.Parameters.Add(New OleDb.OleDbParameter("@OName", System.Data.OleDb.OleDbType.Integer, 5)
    param.Direction = ParameterDirection.Input
    param.SourceColumn = "OName"
    param.SourceVersion = DataRowVersion.Current