扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Imports System.Data.OleDb
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站建设、外贸网站建设、银海网络推广、重庆小程序开发、银海网络营销、银海企业策划、银海品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供银海建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
Public Class Form1
Dim cn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\persons.mdb;Persist Security Info=False"
cn = New OleDbConnection(cnStr)
Dim sql As String = "select * from grade"
da = New OleDbDataAdapter(sql, cn)
ds = New DataSet
da.Fill(ds, "grade")
DataGridView1.DataSource = ds.Tables(0)
End Sub
End Class
解释:
Dim cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\persons.mdb;Persist Security Info=False"
这段是创建链接字符串,你可以根据自己数据库实际存放位置进行修改
Dim sql As String = "select * from grade"
这是数据库查询语句,你可以根据实际情况修改下
Dim sql As String = "select * from grade"
da = New OleDbDataAdapter(sql, cn)
ds = New DataSet
da.Fill(ds, "grade")
DataGridView1.DataSource = ds.Tables(0)
这是执行查询语句后,将查询的结果存入datagridview中
给你写个例子,不明白,再问!!
'引入OLEDB命令空间
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'定义一个OLEDB连接并实例化它
Dim con As New OleDbConnection
'定义一个OLEDB命令并实例化他
Dim cmd As New OleDbCommand
'定义一个OLEDBReader方法来读取数据库
Dim dr As OleDbDataReader
'初始化con的连接属性,使用OLEDB模式,数据源为:你指定下路径,我的是在D盘
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dataSample.mdb"
'打开OLEDB数据连接
con.Open()
'初始化OLEDB命令的连接属性为con,这个需要你理解下
cmd.Connection = con
'初始化OLEDB命令的语句 就是查询 什么字段从什么表 条件是ID等于你在t1中输入的内容
cmd.CommandText = "select keyss from table1 where ID=" t1.Text ""
'执行OLEDB命令以ExecuteReader()方式,并返回一个OLEDBReader,赋值给dr
dr = cmd.ExecuteReader()
'判断下dr中是否有数据。如果有就把第一个值赋值给t2的值
If dr.Read() Then
t2.Text = dr(0)
End If
'完成后关闭dr.con等释放资源
dr.Close()
con.Close()
End Sub
End Class
建立数据集,绑定到窗口。不会的话,把要用的表从数据集拖到窗口,再删除就行了
提取方法
数据及名称.表格名称.rows(2).item(3).tostring 就是了
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
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流