| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2195 人关注过本帖
标题:datagridview 更新数据库问题
只看楼主 加入收藏
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:4 
datagridview 更新数据库问题
通过代码将access数据库绑定到datagridview上,现在想单击保存,将datagridview的更改保存到数据库,书上说用dataadapter
dataadapter.update(dataset)
现在运行时有点问题,我把项目传上来,请帮忙看一下
练习.rar (430.96 KB)

搜索更多相关主题的帖子: 数据库 datagridview 
2010-04-19 09:04
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
在线等!!
有相关项目或代码的的传上来,看一看
2010-04-19 09:32
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
2010-04-19 17:35
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:10 
程序代码:
Imports System.Data.SqlClient
Imports System.Data
Public Class frm_data
    Private ds As New Data.DataSet()
    Dim dbBlder As OleDb.OleDbCommandBuilder
    Dim da As OleDb.OleDbDataAdapter
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click

        Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim tabname As String = cmbtable.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
        Dim sql As String = "select * from " & tabname
        conn.Open() '打开连接
        cmd.Connection = conn
        da = New OleDb.OleDbDataAdapter(sql, conn)
        ds.Tables.Clear()
        da.Fill(ds, "pylj")
        DataGridView1.DataSource = ds.Tables(0) '更换datagridview1的数据源
        'DataGridView1.Refresh() '这句不要datagridview1也可更新
        '--------------
        '获取表所含字段并更新至cmbfiled
        Dim n As Integer = ds.Tables(0).Columns.Count - 1
        cmbfiled.Items.Clear()
        For i As Integer = 0 To n
            cmbfiled.Items.Add(ds.Tables(0).Columns(i).ColumnName)
        Next
        cmbfiled.SelectedIndex = 0
    End Sub
    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        MsgBox(DataGridView1.CurrentCell.Value)
    End Sub
    Private Sub frm_data_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtdba.Text = "\data\py.mdb"
    End Sub
    Private Sub butconn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butconn.Click
        cmbtable.Items.Clear()
        '显示数据所含表
        For i As Int16 = 0 To GetAllTableName.Count - 1
            cmbtable.Items.Add(GetAllTableName.Item(i).ToString)
        Next
        cmbtable.SelectedIndex = 0
    End Sub
    Private Sub cmbfiled_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbfiled.SelectedIndexChanged
        Dim i As Integer = cmbfiled.SelectedIndex
        '显示所选字段数据类型
        txtfieldtype.Text = ds.Tables(0).Columns(i).DataType.Name
    End Sub

    Function GetAllTableName() As System.Collections.ArrayList
        Dim t As New System.Collections.ArrayList()
        Dim Rs As New ADODB.Recordset()
        Dim dbaname As String = txtdba.Text  '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim conn As New ADODB.Connection
        conn.Open(connStr)
        Rs = conn.OpenSchema(ADODB.SchemaEnum.adSchemaTables)
        Do While Not Rs.EOF
            If Rs("TABLE_TYPE").Value = "TABLE" Then
                t.Add(Rs("TABLE_NAME").Value)
            End If
            Rs.MoveNext()
            If Rs.EOF Then Exit Do
        Loop
        Rs.Close()
        Rs = Nothing
        Return t
    End Function

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '保存的代码写在这里
        dbBlder = New OleDb.OleDbCommandBuilder(da)'此变量定义见代码开头
        Try
            da.Update(ds.Tables(0))
            MsgBox("数据库更新结束")
        Catch
            MsgBox("数据库更新失败")
        End Try
    End Sub


End Class

用的人的确并不多,呵呵

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2010-04-19 19:46
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
问题解决!!
用的人的确并不多,老是你一个人的回答有价值!!!
能个其它的联系方式吗?我把我的QQ发给你

[ 本帖最后由 shenhaoli 于 2010-4-19 20:53 编辑 ]
2010-04-19 20:52
快速回复:datagridview 更新数据库问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047046 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved