在vb.net的Microsoft Access 2007中插入记录时出现问题 [英] Problem when insert a record into microsoft access 2007 in vb.net
问题描述
我在执行vb.net中的记录时遇到了一些问题.我正在使用Microsoft Access 2007作为数据库.
我运行我的项目并插入一条记录,按下保存"按钮,并在gridview中显示我的记录,然后停止运行我的项目.
当我再次运行该项目时,我刚才插入到gridview中的记录消失了.
有什么问题吗?
这是我的语法:
I''ve some problem in execute the record in vb.net. I''m using microsoft access 2007 as database.
I run my project and insert a record, I press the button "save" and my record shown in gridview, then I stop running my project.
When I run the project again, the record that I just insert in gridview was gone.
What''s the problem??
This my syntax:
Imports System.Data
Imports System.Data.OleDb
Public Class OlahData
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim reader As OleDbDataReader
Dim constring As String
Dim SqlString As String
Dim numAffected As Integer
Private Sub OlahData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "/FKIP.mdb ;Persist Security Info=True"
con = New OleDbConnection(constring)
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlString = "Insert Into MataKuliah values (''" & id.Text.Trim & "'',''" & nama.Text.Trim & "'',''" & jurusan.Text.Trim & "'',''" & Val(sks.Text) & "'')"
cmd = New OleDbCommand(SqlString, con)
numAffected = cmd.ExecuteNonQuery
MessageBox.Show(numAffected.ToString & "record berhasil di simpan")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "/FKIP.mdb;"
Dim Conn As OleDbConnection
Dim Ds As DataSet
Dim Da As OleDbDataAdapter
Dim SQL As String = "Select * From MataKuliah"
Try
Conn = New OleDbConnection(ConnStr)
Ds = New DataSet
Da = New OleDbDataAdapter(SQL, Conn)
Da.Fill(Ds, "MataKuliah")
gridview.DataSource = Ds.Tables("MataKuliah")
gridview.Dock = DockStyle.Fill
Catch
MessageBox.Show("tabel tidak dapat ditampilkan....!", "Database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Me.Close()
End Try
End Sub
End Class
[edit]添加了代码块,取消选择了忽略HTML ..."选项-OriginalGriff [/edit]
[edit]Code block added, "Ignore HTML..." option deselected - OriginalGriff[/edit]
推荐答案
当您停止并启动项目时,您就可以了"会丢失gridview上的视图状态数据.
您需要再次加载数据,就像单击Button2_Click时一样.
添加以下内容以执行数据绑定.
When you stop and start a project, you''ll lose the view state data on your gridview.
You need to load up the data again, which it loks like you''re doing when you click Button2_Click.
Add the following to perform the databind.
gridview.DataSource = Ds.Tables("MataKuliah")
gridview.DataBind();
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx [ ^ ]
单击您的按钮2,现在可以正常加载数据了吗?
如果您希望它自动加载,则需要在屏幕加载时调用此代码
除此之外,我还会发现您的代码还有很多其他问题!
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx[^]
Click your button2, is the data loading OK now?
If you want it to autoload, you''ll need to call this code when your screen loads
Other than this, I can see lots of other problems with your code!
如果您将mdb文件包含在项目中,请确保未将(属性中)Copy to Output Directory
设置为.
Copy Always
Copy if newer
似乎是一个不错的选择.
如果使用Copy Always
,它将把数据库从项目目录(可能为空)复制到输出目录.
If you have included your mdb file in project, make sure that (in properties)Copy to Output Directory
is not set to.
Copy Always
Copy if newer
seems a good option there.
If you useCopy Always
, it will copy the database from project directory (which is possibly empty) to output directory.
这篇关于在vb.net的Microsoft Access 2007中插入记录时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!