我创建了一个项目。我想当我运行form2表单时,菜单栏记录会自动显示。
access database
vb.net 2010
connecting through coding
use connecting module
------------------------------------
in form load event
call connection
call showdata '---*I want to when I run the form2 form menu bar record will show automatically.
----------------------------------------------
call showdata coding
cnn = New OleDb.OleDbConnection
With cnn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb;Persist Security Info=False;"
.Open()
End With
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDb.OleDbDataAdapter
da = New OleDb.OleDbDataAdapter("select * from cominfo", cnn)
da.Fill(dt)
cnn.Close()
If dt.Rows.Count = 0 Then
MsgBox("No Company Name Registerd. Please Registered The Company....", vbOKOnly)
Cominfo.Show()
Cominfo.ComnameTextBox.Focus()
Else
Cominfo.IDTextBox.Text = dt.Rows(0).Item(0)
Cominfo.ComnameTextBox.Text = dt.Rows(0).Item(1)
End If
在添加数据后显示数据完美的编码
但是当我在其他任何东西改变编码时(例如我添加一个用于显示日期时间的文本框)并保存然后运行应用程序并转到form2所有记录都会自动删除。然后我添加记录并停止应用然后运行100次正常工作但是当我在删除后按下保存按钮/添加一些编码窗口的空白时,我运行应用程序并打开form2时没有记录。
我使用此代码插入数据
基础模块
Imports System.Data.OleDb
Module Modconnection
Public cnn, cn As New OleDb.OleDbConnection
Public cm As New OleDb.OleDbCommand
Public dr As OleDbDataReader
Public Sub connection()
cn = New OleDb.OleDbConnection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb;Persist Security Info=False;"
.Open()
End With
End Sub
添加数据按钮:
Call connection()
Try
cm = New OleDb.OleDbCommand
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "INSERT INTO cominfo (Comname,Add1,Add2,Phone,Vat,cst_tin,Email,Website) VALUES (@Comname,@Add1,@Add2,@Phone,@Vat,@cst_tin,@Email,@Website)"
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Comname", System.Data.OleDb.OleDbType.VarChar, 255, Me.ComnameTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Add1", System.Data.OleDb.OleDbType.VarChar, 255, Me.Add1TextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Add2", System.Data.OleDb.OleDbType.VarChar, 255, Me.Add2TextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Phone", System.Data.OleDb.OleDbType.VarChar, 255, Me.PhoneTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Vat", System.Data.OleDb.OleDbType.VarChar, 255, Me.VatTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@cst_tin", System.Data.OleDb.OleDbType.VarChar, 255, Me.Cst_tinTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Email", System.Data.OleDb.OleDbType.VarChar, 255, Me.EmailTextBox.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Website", System.Data.OleDb.OleDbType.VarChar, 255, Me.WebsiteTextBox.Text))
' RUN THE COMMAND
cm.Parameters("@Comname").Value = Me.ComnameTextBox.Text
cm.Parameters("@Add1").Value = Me.Add1TextBox.Text
cm.Parameters("@Add2").Value = Me.Add2TextBox.Text
cm.Parameters("@Phone").Value = Me.PhoneTextBox.Text
cm.Parameters("@Vat").Value = Me.VatTextBox.Text
cm.Parameters("@cst_tin").Value = Me.Cst_tinTextBox.Text
cm.Parameters("@Email").Value = Me.EmailTextBox.Text
cm.Parameters("@Website").Value = Me.WebsiteTextBox.Text
cm.ExecuteNonQuery()
MsgBox("Record saved.", MsgBoxStyle.Information)
Me.ComnameTextBox.Text = ""
Me.Add1TextBox.Text = ""
Me.Add2TextBox.Text = ""
Me.PhoneTextBox.Text = ""
Me.VatTextBox.Text = ""
Me.Cst_tinTextBox.Text = ""
Me.EmailTextBox.Text = ""
Me.WebsiteTextBox.Text = ""
Exit Sub
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
答案 0 :(得分:1)
我只需将db.accdb属性副本更改为输出目录“不要复制”
答案 1 :(得分:0)
您缺少的是您只编辑本地DataTable中的数据,因此,如果您退出并返回该程序,它就会消失!
所以你需要做的是创建一个OleDb命令并创建一个“Insert”语句或一个“Update”语句。我将为你留下一个包含更多信息的链接。
https://msdn.microsoft.com/en-us/library/5yeecx59(v=vs.110).aspx
编辑:一个展示工作代码(不是我的代码)的好视频: https://www.youtube.com/watch?v=MWP8geR76kI