0

我对数据库保存更改有疑问:

    coon1.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & _
    "magazyn.mdb"
     sql = "INSERT INTO magazyn   (ID_Towaru,Kod_Towaru,Nazwa_Towaru,Ilość_w_magazynie,ilość_minimalna,ALERT) VALUES ('" & jakiid & "','" & kodtowaru & "','" & nazwatowaru & "','" & iloscwmagazynie & "','" & iloscminimalna & "',0)"


    Dim MyConnection As New OleDbConnection(conn)
    Dim command1 As New OleDbCommand(sql, MyConnection)

    command1.Connection.Open()
    command1.ExecuteNonQuery()
    MyConnection.Close()

我尝试将新记录添加到表中magazyn,但是当使用 Access 打开数据库时,我没有看到与magazyn表中相关的任何新记录。但是ViewGrid在我关闭并重新打开程序之前,我会向我展示这个新元素。

有人知道问题出在哪里吗?

4

2 回答 2

2

始终使用参数而不是字符串连接。应绝对遵守此规则

sql = "INSERT INTO magazyn  " + 
       "(Kod_Towaru,Nazwa_Towaru,Ilość_w_magazynie,ilość_minimalna,ALERT) " + 
       "VALUES (?, ?, ?, ?,0)"
Using MyConnection As New OleDbConnection(conn)
Using command1 As New OleDbCommand(sql, MyConnection)
    command1.Connection.Open()
    command1.Parameters.AddWithValue("@Kod", kodtowaru)
    command1.Parameters.AddWithValue("@naz", nazwatowaru)
    command1.Parameters.AddWithValue("@ilo", iloscwmagazynie)
    command1.Parameters.AddWithValue("@mini", iloscminimalna)
    command1.ExecuteNonQuery()
End Using
End Using

当然,这要求用作参数值的变量具有正确的数据类型。

于 2012-11-14T11:58:23.193 回答
0

请在模块中的代码下方插入此代码。

导入 System.Data.OleDb 模块 Module1

Public OleCn As New OleDbConnection()

Public Function StrConnection() As String

    StrConnection = "Provider=Microsoft.Ace.Oledb.12.0; Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\BackUp\Testing.Accdb;"

    Return StrConnection
End Function

在此之后在保存按钮中插入下面的编码。

Dim msg As DialogResult = MessageBox.Show("你想保存这条记录吗?", "Response", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

    If (msg = vbYes) Then

        If RequiredEntry() = True Then
            Return
        End If

        Try
            With OleCn
                If .State <> ConnectionState.Open Then
                    .ConnectionString = StrConnection()
                    .Open()
                End If
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Try

            Dim sSQL As String = "insert into Vendor values(@VendorCode,@VendorName,@Address,@City,@LandPhone,@Mobile,@EmailID,@Balance)"

            Dim cmd As OleDbCommand = New OleDbCommand(sSQL, OleCn)

            'VendorCode
            Dim VendorCode As OleDbParameter = New OleDbParameter("@VendorCode", OleDbType.VarChar, 10)
            VendorCode.Value = txtVendorCode.Text.ToString()
            cmd.Parameters.Add(VendorCode)

            'VendorName
            Dim VendorName As OleDbParameter = New OleDbParameter("@VendorName", OleDbType.VarChar, 25)
            VendorName.Value = txtVendorName.Text.ToString()
            cmd.Parameters.Add(VendorName)

            'Address
            Dim Address As OleDbParameter = New OleDbParameter("@Address", OleDbType.VarChar, 50)
            Address.Value = txtAddress.Text.ToString()
            cmd.Parameters.Add(Address)

            'City
            Dim City As OleDbParameter = New OleDbParameter("@City", OleDbType.VarChar, 25)
            City.Value = txtCity.Text.ToString()
            cmd.Parameters.Add(City)

            'LandPhone
            Dim LandPhone As OleDbParameter = New OleDbParameter("@LandPhone", OleDbType.VarChar, 50)
            LandPhone.Value = txtLandPhone.Text.ToString()
            cmd.Parameters.Add(LandPhone)


            'Mobile
            Dim Mobile As OleDbParameter = New OleDbParameter("@Mobile", OleDbType.VarChar, 15)
            Mobile.Value = txtMobile.Text.ToString()
            cmd.Parameters.Add(Mobile)

            'EmailID
            Dim EmailID As OleDbParameter = New OleDbParameter("@EmailID", OleDbType.VarWChar, 25)
            EmailID.Value = txtEmailID.Text.ToString()
            cmd.Parameters.Add(EmailID)


            'Balance
            Dim Balance As OleDbParameter = New OleDbParameter("@Balance", OleDbType.VarWChar, 10)
            Balance.Value = txtBalance.Text.ToString()
            cmd.Parameters.Add(Balance)

            If cmd.ExecuteNonQuery() Then

                OleCn.Close()
                MessageBox.Show("New Record is Added Successfully.", "Record Saved")
                Call clear()

            Else

                MsgBox("Record Addition Failed ", MsgBoxStyle.Critical, "Addition Failed")

                Return
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Data Error")
            Exit Sub
        End Try
    End If 
于 2013-03-27T07:50:07.627 回答