在vb.net中对访问数据库进行更新

时间:2015-03-17 20:28:06

标签: vb.net ms-access

好, 所以我正在尝试对我的访问数据库进行更新,并且它一直在失败并且我不知道为什么,我使用我自己的类来做sql相关的东西。

sqlmngr class:

Imports System.Data.OleDb

Public Class sqlmngr
    Public Shared qrydataset As DataSet
    Public Shared updateset As DataTable
    Public Shared cb As OleDbCommandBuilder
    Public Shared Sub doquery(constring As String, query As String, adapter As OleDbDataAdapter, Optional table As String = "")
        Dim connection As New OleDb.OleDbConnection(constring)
        connection.Open()
        Dim querycommand As New OleDbCommand
        querycommand.CommandType = CommandType.Text
        querycommand.CommandText = query
        querycommand.Connection = connection


        qrydataset = New DataSet("qryresults")
        adapter.SelectCommand = querycommand
        cb = New OleDbCommandBuilder(adapter)

        If table = "" Then
            adapter.Fill(qrydataset)
        Else
            qrydataset.Tables.Add(table)
            adapter.Fill(qrydataset, table)
        End If
        connection.Close()
    End Sub
    Public Shared Sub dononquery(constring As String, query As String)
        Try
            Dim connection As New OleDb.OleDbConnection(constring)
            Dim querycommand As New OleDbCommand
            connection.Open()
            querycommand.Connection = connection
            querycommand.CommandText = query
            querycommand.ExecuteNonQuery()
            MsgBox("hij doet iets")
            connection.Close()
        Catch e As Exception
            MsgBox(e.ToString)
        End Try
    End Sub

    Public Shared Sub doupdate(da As OleDbDataAdapter, ds As DataSet, constring As String)
        Try

            Dim cb As New OleDbCommandBuilder(da)
            da.UpdateCommand = cb.GetUpdateCommand()
            da.Update(ds.Tables("llngegevens"))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Shared Function getdataset()
        Return qrydataset
    End Function
End Class

必须进行更新的表单代码:

Imports System.Data
Public Class secretaressescherm
    Dim dataset As New DataSet
    Dim datatable2 As New DataTable
    Dim da As New OleDb.OleDbDataAdapter
    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
        Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\leerlingenvolgsysteem.accdb"

        sqlmngr.doquery(constring, "SELECT * FROM leerlingen", da)
        dataset = sqlmngr.qrydataset.Copy
        sqlmngr.qrydataset.Reset()
        DataGridView1.DataSource = dataset.Tables(0)

        'Dim da2 As New OleDb.OleDbDataAdapter
        'sqlmngr.doquery(constring, "SELECT DISTINCT klas FROM leerlingen", da2, "klassen")
        'dataset = sqlmngr.qrydataset.Copy
        'datatable2 = dataset.Tables("klassen")
        'ComboBox1.DataSource = datatable2
        'ComboBox1.DisplayMember = "klas"
    End Sub


    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        dataset.Tables(0).DefaultView.RowFilter = String.Format("achternaam like '" & TextBox1.Text & "%'")
        DataGridView1.DataSource = dataset.Tables(0)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\leerlingenvolgsysteem.accdb"
        Dim con As New OleDb.OleDbConnection(constring)
        con.Open()

        'sqlmngr.updateset = datatable
        'sqlmngr.doupdate(da, dataset, constring)\
        da.AcceptChangesDuringUpdate = True


        sqlmngr.cb.GetUpdateCommand()

        da.Update(dataset)

        con.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    End Sub
End Class

它抛出了这个错误:

System.Data.OleDb.OleDbException was unhandled
  ErrorCode=-2147217904
  HResult=-2147217904
  Message=No value given for one or more required parameters.
  Source=Microsoft Access Database Engine
  StackTrace:
       at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
       at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
       at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
       at po_leerlingvolgsysteem.secretaressescherm.Button2_Click(Object sender, EventArgs e) in C:\Users\david\Desktop\po leerlingvolgsysteem\po leerlingvolgsysteem\secretaressescherm.vb:line 56
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at po_leerlingvolgsysteem.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我不知道它丢失了什么以及它为什么会抛出这个错误。 如果有人可以帮助我,那就太好了!

2 个答案:

答案 0 :(得分:0)

  

我不知道它缺少什么

这很奇怪,因为错误信息很清楚:

  

消息=没有给出一个或多个必需参数的值。

因此,即使您的更新能够正常运行,也无法进行处理。

答案 1 :(得分:0)

结果我的数据库出了问题,

我有一个名为' E-mail'的列,' - '由于某种原因导致错误。 将我的专栏重命名为' email'它运作得很好。