新报到!大家好,请多多关照。带来一个问题
在做用vb2005和Access进行连接的练习中遇到这样的情况:DataGrid中的表格和Access中的表格不一样。比如在做删除的时候,按下了删除的button,Access中的数据被删掉了。而且每次运行程序的时候,Datagrid中的数据与Access中的数据多。确认的路径没有错。请问这回是什么原因呢?Imports System.Data.OleDb
Imports System.data
Public Class Form1
Dim mycmd As New System.Data.OleDb.OleDbCommand
Dim myada As New System.Data.OleDb.OleDbDataAdapter
Dim myds As New DataSet
Dim mycon As New System.Data.OleDb.OleDbConnection
Dim ps As CurrencyManager
Dim myrow As DataRow
Dim mytable As DataTable = New DataTable()
Dim str As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 这行代码将数据加载到表“DEMODataSet.Student”中。您可以根据需要移动或移除它。
Me.StudentTableAdapter.Fill(Me.DEMODataSet.Student)
mycon.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\桌面\DEMO.mdb"
mycon.Open()
mycmd.CommandText = "select * from student"
mycmd.Connection = mycon
myds.Clear()
myada = New System.Data.OleDb.OleDbDataAdapter(mycmd)
Try
myada.Fill(DEMODataSet, "student")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
ps = Me.BindingContext(DEMODataSet, "Student")
DataGrid1.Refresh()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ps.Position = 0
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ps.Position -= 1
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ps.Position += 1
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ps.Position = ps.Count - 1
End Sub
Private Sub Ad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ad.Click
str = "insert into student values('" & TextBox5.Text & "','" & TextBox1.Text & "','" & ComboBox1.Text & "','" & TextBox4.Text & "','" & TextBox2.Text & "')"
mycmd.CommandText = str
mycmd.Connection = mycon
Try
mycmd.ExecuteNonQuery()
myada.Fill(DEMODataSet, "student")
Catch ex As Exception
MsgBox("有重复值")
End Try
DataGrid1.Refresh()
MessageBox.Show("add success")
'myrow = DEMODataSet.Student.NewRow()
'myada.InsertCommand = New OleDbCommand("Insert Into Student (sno, sname,s易做图 sdept, sage) Values(textbox5.text,textbox1.text,combobox1.text,textbox2.text,textbox4.text;", mycon)
'myrow("Sno") = TextBox5.Text
'myrow("Sage") = TextBox4.Text
'myrow("S易做图") = ComboBox1.Text
'myrow("Sdept") = TextBox2.Text
'myrow("Sname") = TextBox1.Text
'DEMODataSet.Student.Rows.Add(myrow)
'mycon.Close()
End Sub
Private Sub Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Del.Click
str = "delete * from student where Sno='" & TextBox5.Text & "'"
mycmd.CommandText = str
mycmd.Connection = mycon
Try
mycmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
myada.Fill(DEMODataSet, "student")
DataGrid1.Refresh()
MsgBox("delete success")
'DEMODataSet.Student.Rows.RemoveAt(ps.Position)
End Sub
Private Sub Rn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rn.Click
myada.Update(DEMODataSet.Student)
MessageBox.Show("Update success")
End Sub
Private Sub ext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ext.Click
Me.Close()
End Sub
End Class
程序还没做完,这个问题始终解决不了。希望有人帮助我一下。先谢谢了 --------------------编程问答-------------------- 转到VBNET 你照着书抄写的代码么?
居然连Dim mycmd As New System.Data.OleDb.OleDbCommand 这样的声明都写
你前面写了imports,后面不必要这么写
Datagrid中的数据与Access中的数据多//这句话是datagrid比access的多?这么理解? --------------------编程问答-------------------- 疑惑ING。。。。 --------------------编程问答--------------------
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test.mdb;Persist Security Info=False"
Dim da As OleDbDataAdapter 'da声明
Dim ds As DataSet
Dim Conn As OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'声明连接字符串
Conn = New OleDbConnection(strConn)
Conn.Open()
da = New OleDbDataAdapter("select * from tableA", Conn)
ds = New DataSet
da.Fill(ds, "tcsdn")
DataGridView1.DataSource = ds.Tables("tt")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'删除
Dim strSQL As String = "Delete from tableA where ID=6"
Dim cmd As New OleDbCommand
cmd.CommandText = strSQL
cmd.Connection = Conn
cmd.ExecuteNonQuery()
da = New OleDbDataAdapter("select * from tableA", Conn)
ds = New DataSet
da.Fill(ds, "tcsdn")
DataGridView1.DataSource = ds.Tables("tt")
End Sub
End Class
补充:.NET技术 , VB.NET