vb.net 在datagridview中增加,修改,删除数据,如何使这种改变(就是前面所说的增,修,删)真正的改变数据库
代码如下:ImportsSystem.Data.OleDbImportsSystem.Windows.FormsPublicClassForm1DimconnAsNewS...
代码如下:
Imports System.Data.OleDb
Imports System.Windows.Forms
Public Class Form1
Dim conn As New System.Data.OleDb.OleDbConnection()
Dim strconn As String
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ada As New System.Data.OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
Dim TableIndex As Integer
Dim dt As New DataTable
Dim bs As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\AccessDataBase\test2000.mdb"
conn.ConnectionString = strconn
conn.Open()
lb1.Items.Add("student")
lb1.Items.Add("sc")
lb1.Items.Add("course")
TableIndex = -1
'ada.SelectCommand
End Sub
Private Sub lb1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb1.Click
ds.Clear() '清除databridview1中的数据,而不是累加显示
If (lb1.SelectedIndex = 0) Then
TableIndex = 0
bs.DataSource = dt
cmd.CommandText = "Select * from student"
cmd.Connection = conn
ada.Fill(dt)
ada.Fill(ds, "student")
DataGridView1.DataSource = ds.Tables("student")
ElseIf lb1.SelectedIndex = 1 Then
TableIndex = 1
cmd.CommandText = "select * from sc"
cmd.Connection = conn
ada.Fill(ds, "sc")
DataGridView1.DataSource = ds.Tables("sc")
Else
TableIndex = 2
cmd.CommandText = "select * from course"
cmd.Connection = conn
ada.Fill(ds, "course")
DataGridView1.DataSource = ds.Tables("course")
End If
End Sub
Private Sub addNewRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addNewRowButton.Click
'在这里使datagridview中的所有改变更新到数据库中
End Sub
End Class 展开
Imports System.Data.OleDb
Imports System.Windows.Forms
Public Class Form1
Dim conn As New System.Data.OleDb.OleDbConnection()
Dim strconn As String
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ada As New System.Data.OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
Dim TableIndex As Integer
Dim dt As New DataTable
Dim bs As New BindingSource
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\AccessDataBase\test2000.mdb"
conn.ConnectionString = strconn
conn.Open()
lb1.Items.Add("student")
lb1.Items.Add("sc")
lb1.Items.Add("course")
TableIndex = -1
'ada.SelectCommand
End Sub
Private Sub lb1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb1.Click
ds.Clear() '清除databridview1中的数据,而不是累加显示
If (lb1.SelectedIndex = 0) Then
TableIndex = 0
bs.DataSource = dt
cmd.CommandText = "Select * from student"
cmd.Connection = conn
ada.Fill(dt)
ada.Fill(ds, "student")
DataGridView1.DataSource = ds.Tables("student")
ElseIf lb1.SelectedIndex = 1 Then
TableIndex = 1
cmd.CommandText = "select * from sc"
cmd.Connection = conn
ada.Fill(ds, "sc")
DataGridView1.DataSource = ds.Tables("sc")
Else
TableIndex = 2
cmd.CommandText = "select * from course"
cmd.Connection = conn
ada.Fill(ds, "course")
DataGridView1.DataSource = ds.Tables("course")
End If
End Sub
Private Sub addNewRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addNewRowButton.Click
'在这里使datagridview中的所有改变更新到数据库中
End Sub
End Class 展开
2个回答
展开全部
首先你的数据源就写错了
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\AccessDataBase\test2000.mdb"
应为
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\\AccessDataBase\\test2000.mdb"
绝对路径是"\\",这个要记得。
代码我就不帮你写了,说下我的思路,对于数据库的操作,你只能1步步来做,也就是在dataGridView里做了修改以后就直接在数据库里修改,打个比方,你想在dataGridView里添加数据A再删除数据B,步骤如下:
1.你在dataGridView里添加了数据A
2.打开数据库
3.在相应表下添加A
4.关闭数据库
5.在dataGridView里删除数据B
6.打开数据库
7.在相应表里删除数据B
8.关闭数据库
看起来方法比较复杂,但是数据库的操作必须这样做,而且,记得,打开数据库并操作结束后先关闭数据库再做其他的任务。
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\AccessDataBase\test2000.mdb"
应为
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =E:\\AccessDataBase\\test2000.mdb"
绝对路径是"\\",这个要记得。
代码我就不帮你写了,说下我的思路,对于数据库的操作,你只能1步步来做,也就是在dataGridView里做了修改以后就直接在数据库里修改,打个比方,你想在dataGridView里添加数据A再删除数据B,步骤如下:
1.你在dataGridView里添加了数据A
2.打开数据库
3.在相应表下添加A
4.关闭数据库
5.在dataGridView里删除数据B
6.打开数据库
7.在相应表里删除数据B
8.关闭数据库
看起来方法比较复杂,但是数据库的操作必须这样做,而且,记得,打开数据库并操作结束后先关闭数据库再做其他的任务。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询