vb.netmdb碎片的简单介绍
用vb.net操作access数据库
mports System.Drawing
创新互联专业为企业提供瑞昌网站建设、瑞昌做网站、瑞昌网站设计、瑞昌网站制作等企业网站建设、网页设计与制作、瑞昌企业网站模板建站服务,十年瑞昌做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Inherits Form
Private components As System.ComponentModel.Container = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase
Public Sub New ( )
MyBase.New ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用过的资源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
Public Sub GetConnected ( )
'创建一个数据连接
Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConn.ConnectionString = strCon
Dim strCom As String = " SELECT * FROM person "
'创建一个 DataSet
myDataSet = New DataSet ( )
myConn.Open ( )
'通过OleDbDataAdapter对象得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "person" )
'关闭此数据连接
myConn.Close ( )
End Sub
'插入数据记录操作代码
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As System.EventArgs) Handles button2.Click
'判断所有字段是否添完,添完则执行,反之弹出提示
If ( t_id.Text "" And t_xm.Text ""
And t_xb.Text "" And t_nl.Text ""
And t_books.Text "" ) Then
Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = db.mdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConn.Open ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " _
t_id.Text + " , '" _
t_xm.Text + "' , '" _
t_xb.Text + "' , " _
t_nl.Text + " , " _
t_books.Text + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
inst.ExecuteNonQuery ( )
myConn.Close ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )
myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )
myDataSet.Tables ( "person" ).AcceptChanges ( )
Else
MessageBox.Show ( "必须填满所有字段值!" , "错误!" )
End If
End Sub
vb.net 更新数据时出现错误
我感觉没有错啊!我用insert就对!为什么这个会不对呢!要不你改用这个试试!
Private Sub update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\test.mdb"
Dim Con As New OleDb.OleDbConnection(ConnStr)
Com.connection=con
Com.commandtext="update [user] set password='" TextBox2.Text "' where username='" TextBox1.Text "'"
Con.Open()
Com.ExecuteNonQuery()
Con.Close()
MsgBox("更新成功")
End Sub
如果问题依旧那可能是你更新语句写的有问题。。。
vb.net读取Access数据库的方法
建立数据集,绑定到窗口。不会的话,把要用的表从数据集拖到窗口,再删除就行了
提取方法
数据及名称.表格名称.rows(2).item(3).tostring 就是了
vb.net2008中怎样调用资源文件中的MDB数据库
在此本人使用ADO对象访问ACCESS数据库,但ADO对象在使用前你先要进行添加,启动VB6后,单击菜单“工程-引用”,打开引用对话框,选择“Microsoft ActiveX Data Objects 2.0 Library”,将其勾选,(以2.0版本为例,实际可以选择系统中的最高版本如本人的就是2.6版本),然后在当前目录下建立一个名为“学生”的ACCESS数据库,其中表名为“表”,字段共有:学号,姓名,年龄,性别四个字段,接下来就是设计界面了(自己设计)
现在介绍如何使用代码访问ACCESS数据库:
在代码窗口中编写一个返回字符型的函数
Function Connection() As String
'数据库的连接设置配置
Connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" App.Path + "\学生.mdb"
End Function
该函数用于连接数据库用
Private Form_Load()
Dim cnn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
cnn.Open Connection
Set Rst = New ADODB.Recordset
Rst.Open "select * from 表", cnn, adOpenKeyset, adLockOptimistic
End Sub
注:
Dim cnn As New ADODB.Connection创建一个ADO数据库连接对象
Dim Rst As New ADODB.Recordset为创建一个ADO数据库记录对象
select * from 表 是SQL查询语言,这里为数据源,意思就是打开数据库“学生”中的表“表”
下面是将数据库表中的字段值显示出来的一个过程
Sub View()
Dim i As Integer
For i = 0 To 2
Text1(i) = Rst.Fields(i)'text1为控件数组
Next
If (Rst.Fields(3) = True) Then
Text1(3) = "男"
Else
Text1(3) = "女"
End If
End Sub
要访问ACCESS数据库就这样可以实现,访问SQL也差不多,稍有不同,回答完毕,如还有问题请继续和我联系,本人是专使用VB+SQL/Delphi+SQL开发数据库管理系统的程序员。
vb.net 写入mdb数据库
1、首先,你得做一个oledbconnection对象,这个对象有个连接字符串的属性,你需要设置
2、做一个oledbcommand对象,这个对象有个操作字符串(SQL语句)需要设置
3、打开oledbconnection对象
4、执行oledbcommand对象
5、关闭oledbconnection对象
程序结束,希望对你有帮助
在vb.net中如何查询MDB数据库的数据
以下是完整模块
Imports
System.Data
Imports
System.IO
Imports
System.Data.OleDb
Module
Module1
Public
cn
As
New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Application.StartupPath
"\mdb数据库名字.mdb")
'定义连接
Public
DataBaseRST
As
Integer
'用来返回数据库执行结果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'进行数据库修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用来返回执行的结果
cn.Close()
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
Public
Function
Search(ByVal
str
As
String,
ByVal
DGV
As
DataGridView)
As
Boolean
'查询
str---查询命令,DGV---DataGridView,用来显示数据的控件
Dim
tb
As
New
DataTable
Try
Dim
ap
As
New
OleDb.OleDbDataAdapter(str,
cn)
ap.Fill(tb)
DGV.DataSource
=
tb
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
End
Module
'以下是调用方法
DataModify("
insert
into
aa
values
('1','2')")'-------这里是数据库更新操作
Search("select
bb
from
aa",DataGridView1)'-----------这里是数据表查询操作
网站栏目:vb.netmdb碎片的简单介绍
文章位置:http://cdiso.cn/article/doddchj.html