none
DataGridView 如何更新数据库?可参考那些资料?谢谢 RRS feed

  • 问题

  • 我在操作 DataGridView 更新数据库时遇到困难,

     

    我的代码为:  在一个Winform中,加载了一个DataGridView(dataGridView1 ),一个Button(UpdateButton )

     

    想在 UpdateButton_Click 事件中更新数据库。

     

    翻查了 MSDN, 可使用如下代码更新,于是我把 dataTable 定义为  Private Shared ,并以下代码放进UpdateButton_Click 事件, 报错 “未将对象引用设置到对象的实例”

     

            Dim instance As DbDataAdapter      //需要 Imports System.Data.Common
            Dim dataTable As DataTable
            Dim returnValue As Integer

            returnValue = instance.Update(dataTable)

     

    请问:

     该如何把DataGridView的数据更新到数据库? 可参考那些资料?谢谢。

     

     

    现在的代码如下:

     

    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Data.OleDb


    Public Class Form6


        Inherits System.Windows.Forms.Form

        Private buttonPanel As New Panel
        Private WithEvents dataGridView1 As New DataGridView
        Private WithEvents UpdateButton As New Button
        Private bindingSource1 As New BindingSource()

     


        Private Sub Form6_Load   (ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

        

            SetupLayout()                         //form的布局,外观。

     

            bindingSource1.DataSource = GetData("select * from 装备库")     //连接数据库,bind数据

     

            Me.dataGridView1.DataSource = bindingSource1

     

            Me.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)

     

        End Sub


     

        Private Shared Function GetData     (ByVal selectCommand As String)   As DataTable

     

            Dim connectionString As String = _
               "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\work\database.mdb"

     

            Dim data As New DataTable()

     

            Dim adapter As New OleDbDataAdapter(selectCommand, connectionString)

     

            data.Locale = System.Globalization.CultureInfo.InvariantCulture

     

            adapter.Fill(data)

     

            Return data

     

        End Function


        Private Sub   UpdateButton_Click    (ByVal sender As Object, _
            ByVal e As EventArgs) Handles UpdateButton.Click

     

            //想在此处更新数据库

     

        End Sub

     

        Private Sub SetupLayout()

            Me.Size = New Size(1024, 768)

            With UpdateButton
                .Text = "Add Row"
                .Location = New Point(10, 10)
            End With

            With buttonPanel
                .Controls.Add(UpdateButton)
                .Height = 50
                .Dock = DockStyle.Bottom
            End With

            Me.Controls.Add(Me.buttonPanel)

            Me.Controls.Add(dataGridView1)
            With dataGridView1
                .AllowUserToAddRows = False
                .Name = "dataGridView1"
                .Location = New Point(8, 8)
                .Size = New Size(800, 600)
                .MultiSelect = False
                .Dock = DockStyle.Fill
            End With

        End Sub

     


        <STAThreadAttribute()> _
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form6())

        End Sub

    End Class

    2008年12月18日 19:20

答案

  • 韦大侠:

     

    我看不懂你的VB源码是 怎么处理update操作的。

     

    自己手动生成sql搞掂了~  如有时间解释一下你的源码。 谢谢了

     

     

     

    2008年12月21日 3:40

全部回复