none
VB2008如何把DataGridView1里的数据写到ACCESS表里 RRS feed

  • 问题

  • 如何把DataGridView1里的数据写到ACCESS表里,我用VB2008建了个DataGridView1的控件,用数据源把已建的ACCESS数据表已连接上,但不知道如何能把DataGridView1里的数据存到数据表里,请指点,本人刚开始用VB2008很不习惯,但还想学一下
    2009年8月6日 10:46

答案

  • 你好!
         可以参考这个实例,希望对你有帮助:
    1. Public Function CreateCmdsAndUpdate(ByVal connectionString As String, ByVal queryString As String) As DataSet
    2.     Using connection As New OleDbConnection(connectionString)
    3.         Dim adapter As New OleDbDataAdapter()
    4.         adapter.SelectCommand = New OleDbCommand(queryString, connection)
    5.         Dim builder As New OleDbCommandBuilder(adapter)
    6.        
    7.         connection.Open()
    8.        
    9.         Dim customers As New DataSet()
    10.         adapter.Fill(customers)
    11.        
    12.         'code to modify data in dataset here
    13.        
    14.         adapter.Update(customers)
    15.        
    16.         Return customers
    17.     End Using
    18. End Function

    周雪峰
    • 已标记为答案 Kira Qian 2009年8月10日 9:03
    2009年8月6日 13:57
    版主
  • 请参考 OleDbDataAdapter 类的相关用法。
    下面是个例子你参考下。

    Public Function CreateDataAdapter(ByVal selectCommand As String, _
        ByVal connection As OleDbConnection) As OleDbDataAdapter
    
        Dim adapter As OleDbDataAdapter = _
            New OleDbDataAdapter(selectCommand, connection)
    
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
        ' Create the commands.
        adapter.InsertCommand = New OleDbCommand( _
            "INSERT INTO Customers (CustomerID, CompanyName) " & _
             "VALUES (?, ?)")
    
        adapter.UpdateCommand = New OleDbCommand( _
            "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
            "WHERE CustomerID = ?")
    
        adapter.DeleteCommand = New OleDbCommand( _
            "DELETE FROM Customers WHERE CustomerID = ?")
    
        ' Create the parameters.
        adapter.InsertCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID")
        adapter.InsertCommand.Parameters.Add( _
            "@CompanyName", OleDbType.VarChar, 40, "CompanyName")
    
        adapter.UpdateCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID")
        adapter.UpdateCommand.Parameters.Add( _
            "@CompanyName", OleDbType.VarChar, 40, "CompanyName")
        adapter.UpdateCommand.Parameters.Add( _
            "@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
            DataRowVersion.Original
    
        adapter.DeleteCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
            DataRowVersion.Original
    
        Return adapter
    End Function

    I am Rayman Zhai
    • 已标记为答案 Kira Qian 2009年8月10日 9:03
    2009年8月7日 4:28

全部回复

  • 你好!
         可以参考这个实例,希望对你有帮助:
    1. Public Function CreateCmdsAndUpdate(ByVal connectionString As String, ByVal queryString As String) As DataSet
    2.     Using connection As New OleDbConnection(connectionString)
    3.         Dim adapter As New OleDbDataAdapter()
    4.         adapter.SelectCommand = New OleDbCommand(queryString, connection)
    5.         Dim builder As New OleDbCommandBuilder(adapter)
    6.        
    7.         connection.Open()
    8.        
    9.         Dim customers As New DataSet()
    10.         adapter.Fill(customers)
    11.        
    12.         'code to modify data in dataset here
    13.        
    14.         adapter.Update(customers)
    15.        
    16.         Return customers
    17.     End Using
    18. End Function

    周雪峰
    • 已标记为答案 Kira Qian 2009年8月10日 9:03
    2009年8月6日 13:57
    版主
  • 请参考 OleDbDataAdapter 类的相关用法。
    下面是个例子你参考下。

    Public Function CreateDataAdapter(ByVal selectCommand As String, _
        ByVal connection As OleDbConnection) As OleDbDataAdapter
    
        Dim adapter As OleDbDataAdapter = _
            New OleDbDataAdapter(selectCommand, connection)
    
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    
        ' Create the commands.
        adapter.InsertCommand = New OleDbCommand( _
            "INSERT INTO Customers (CustomerID, CompanyName) " & _
             "VALUES (?, ?)")
    
        adapter.UpdateCommand = New OleDbCommand( _
            "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
            "WHERE CustomerID = ?")
    
        adapter.DeleteCommand = New OleDbCommand( _
            "DELETE FROM Customers WHERE CustomerID = ?")
    
        ' Create the parameters.
        adapter.InsertCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID")
        adapter.InsertCommand.Parameters.Add( _
            "@CompanyName", OleDbType.VarChar, 40, "CompanyName")
    
        adapter.UpdateCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID")
        adapter.UpdateCommand.Parameters.Add( _
            "@CompanyName", OleDbType.VarChar, 40, "CompanyName")
        adapter.UpdateCommand.Parameters.Add( _
            "@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
            DataRowVersion.Original
    
        adapter.DeleteCommand.Parameters.Add( _
            "@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
            DataRowVersion.Original
    
        Return adapter
    End Function

    I am Rayman Zhai
    • 已标记为答案 Kira Qian 2009年8月10日 9:03
    2009年8月7日 4:28