新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
以下是完整模块
创新互联从2013年成立,先为岭东等服务建站,岭东等地企业,进行企业商务咨询服务。为岭东企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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=C:\ACCESS数据库.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)'-----------这里是数据表查询操作
1、首先,你得做一个oledbconnection对象,这个对象有个连接字符串的属性,你需要设置
2、做一个oledbcommand对象,这个对象有个操作字符串(SQL语句)需要设置
3、打开oledbconnection对象
4、执行oledbcommand对象
5、关闭oledbconnection对象
程序结束,希望对你有帮助
参考一下下面这段代码就可以了。
Imports System.Data
'引入数据库操作类命名空间
Imports System.Data.OleDb
'引入ADO.NET操作命名空间
Public Class FrmModifystInfo
Inherits System.Windows.Forms.Form
Public ADOcmd As OleDbDataAdapter
Public ds As DataSet = New DataSet()
'建立DataSet对象
Public mytable As Data.DataTable
'建立表单对象
Public myrow As Data.DataRow
'建立数据行对象
Public rownumber As Integer
'定义一个整型变量来存放当前行数
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗体设计器生成的代码 "
#End Region
'======================================================
Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'窗体的载入
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '设置信息为只读
Dim tablename As String = "student_Info "
SearchSQL = "select * from student_Info "
ExecuteSQL(SearchSQL, tablename) '打开数据库
ShowData() '显示记录
End Sub
Private Sub ShowData()
'在窗口中的textbox中显示数据
myrow = mytable.Rows.Item(rownumber)
TxtSID.Text = myrow.Item(0).ToString
TxtName.Text = myrow.Item(1).ToString
ComboSex.Text = myrow.Item(2).ToString
TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")
TxtClassno.Text = myrow.Item(4).ToString
TxtTel.Text = myrow.Item(5).ToString
TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")
TxtAddress.Text = myrow.Item(7).ToString
TxtComment.Text = myrow.Item(8).ToString
End Sub
Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click
'指向第一条数据
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一条数据
BtNext.Enabled = True
rownumber = rownumber - 1
If rownumber 0 Then
rownumber = 0 '如果到达记录的首部,行号设为零
BtPrev.Enabled = False
End If
ShowData()
End Sub
Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click
'指向上一条数据
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判断是否到达最后一条数据
BtNext.Enabled = False
End If
ShowData()
End Sub
Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click
'指向最后一条数据
rownumber = mytable.Rows.Count - 1
ShowData()
End Sub
Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click
mytable.Rows.Item(rownumber).Delete() '删除记录
If MsgBox( "确定要删除改记录吗? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自动生成的SQL语句
ADOcmd.Update(ds, "student_Info ")
BtNext.PerformClick()
End If
End Sub
Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click
TxtSID.Enabled = False '关键字段只读
TxtName.Enabled = True '可读写
ComboSex.Enabled = True
TxtBornDate.Enabled = True
TxtClassno.Enabled = True
TxtRuDate.Enabled = True
TxtTel.Enabled = True
TxtAddress.Enabled = True
TxtComment.Enabled = True
End Sub
Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click
If Not Testtxt(TxtName.Text) Then
MsgBox( "请输入姓名! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Focus()
Exit Sub
End If
If Not Testtxt(ComboSex.Text) Then
MsgBox( "请选择性别! ", vbOKOnly + vbExclamation, "警告 ")
ComboSex.Focus()
Exit Sub
End If
If Not Testtxt(TxtClassno.Text) Then
MsgBox( "请选择班号! ", vbOKOnly + vbExclamation, "警告 ")
TxtClassno.Focus()
Exit Sub
End If
If Not Testtxt(TxtTel.Text) Then
MsgBox( "请输入联系电话! ", vbOKOnly + vbExclamation, "警告 ")
TxtTel.Focus()
Exit Sub
End If
If Not Testtxt(TxtAddress.Text) Then
MsgBox( "请输入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")
TxtAddress.Focus()
Exit Sub
End If
If Not IsNumeric(Trim(TxtSID.Text)) Then
MsgBox( "请输入数字学号! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
TxtRuDate.Focus()
Exit Sub
End If
myrow.Item(0) = Trim(TxtSID.Text)
myrow.Item(1) = Trim(TxtName.Text)
myrow.Item(2) = Trim(ComboSex.Text)
myrow.Item(3) = Trim(TxtBornDate.Text)
myrow.Item(4) = Trim(TxtClassno.Text)
myrow.Item(5) = Trim(TxtTel.Text)
myrow.Item(6) = Trim(TxtRuDate.Text)
myrow.Item(7) = Trim(TxtAddress.Text)
myrow.Item(8) = Trim(TxtComment.Text)
mytable.GetChanges()
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自动生成的SQL语句
ADOcmd.Update(ds, "student_Info ")
'对数据库进行更新
MsgBox( "修改学籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '重新设置信息为只读
End Sub
Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False
End Sub
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
Try
'建立ADODataSetCommand对象
'数据库查询函数
ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")
'建立ADODataSetCommand对象
ADOcmd.Fill(ds, table) '取得表单
mytable = ds.Tables.Item(0) '取得名为table的表
rownumber = 0 '设置为第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行数据
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
引用 Microsoft ActiveX Data Objects 2.8 Library
或者加入microsoft ado data control
定义数据库连接
Dim Cnn As New ADODB.Connection
定义操作记录
Dim Res As New ADODB.Recordset
开始使用
If Res.State = 1 Then Res.Close
Res.CursorLocation = adUseClient
Res.Open "SELECT * From 发票表", Cnn, adOpenDynamic, adLockReadOnly
删除用res.Delete
添加用res.AddNew
res.Fields("序号") = Trim(Res.Fields("序号"))
...
res.Update
更新用Res.Update "序号", 111
列:
'写入产品信息
Sub WriteFP()
'On Error Resume Next
Dim SyBaseRes As New ADODB.Recordset
PB.value = 0
Res.Open "SELECT Count(*) From ICSaleEntry INNER JOIN t_Item ON ICSaleEntry.FItemID = t_Item.FItemID INNER JOIN t_MeasureUnit ON ICSaleEntry.FUnitID = t_MeasureUnit.FMeasureUnitID", SQLServer, adOpenDynamic, adLockReadOnly
Text2.Text = "当前共 " Val(Res.Fields(0)) "条数据"
PB.Max = Val(Res.Fields(0))
If Res.State = 1 Then Res.Close
Res.Open "SELECT ICSaleEntry.FDetailID AS 序号,ICSaleEntry.FInterID AS 物料编号, t_Item.FName AS 产品名称,ICSaleEntry.FAuxPrice AS 单价, ICSaleEntry.FAuxQty AS 数量,ICSaleEntry.FAmount AS 原币, ICSaleEntry.FStdAmount AS 本币,t_MeasureUnit.FName AS 单位 FROM ICSaleEntry INNER JOIN t_Item ON ICSaleEntry.FItemID = t_Item.FItemID INNER JOIN t_MeasureUnit ON ICSaleEntry.FUnitID = t_MeasureUnit.FMeasureUnitID", SQLServer, adOpenDynamic, adLockReadOnly
Do While Not Res.EOF
If SyBaseRes.State = 1 Then SyBaseRes.Close
SyBaseRes.Open "select * from 物料表 where 序号='" Res.Fields("序号") "'", Cnn, adOpenDynamic, adLockOptimistic
If SyBaseRes.EOF Then
Text2.Text = Text2.Text + vbCrLf + "正在添加: " Trim(Res.Fields("序号"))
SyBaseRes.AddNew
SyBaseRes.Fields("序号") = Trim(Res.Fields("序号"))
SyBaseRes.Fields("产品编号") = Trim(Res.Fields("物料编号"))
SyBaseRes.Fields("产品名称") = Trim(Res.Fields("产品名称"))
SyBaseRes.Fields("单价") = Trim(Res.Fields("单价"))
SyBaseRes.Fields("数量") = Trim(Res.Fields("数量"))
SyBaseRes.Fields("原币") = Trim(Res.Fields("原币"))
SyBaseRes.Fields("本币") = Trim(Res.Fields("本币"))
SyBaseRes.Fields("单位") = Trim(Res.Fields("单位"))
SyBaseRes.Fields("数据库名") = Trim(Text1.Text)
SyBaseRes.Update
Else
Text2.Text = Text2.Text + vbCrLf + "正在更新: " Trim(Res.Fields("序号"))
SyBaseRes.Update "序号", Trim(Res.Fields("序号"))
SyBaseRes.Update "产品编号", Trim(Res.Fields("物料编号"))
SyBaseRes.Update "产品名称", Trim(Res.Fields("产品名称"))
SyBaseRes.Update "单价", Trim(Res.Fields("单价"))
SyBaseRes.Update "数量", Trim(Res.Fields("数量"))
SyBaseRes.Update "原币", Trim(Res.Fields("原币"))
SyBaseRes.Update "本币", Trim(Res.Fields("本币"))
SyBaseRes.Update "单位", Trim(Res.Fields("单位"))
SyBaseRes.Update "数据库名", Trim(Text1.Text)
End If
PB.value = PB.value + 1
Res.MoveNext
DoEvents
Loop
If SyBaseRes.State = 1 Then SyBaseRes.Close
Set SyBaseRes = Nothing
End Sub
网络希望不要用ACCESS用ASA(就是sybase那个太强了)
在此本人使用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开发数据库管理系统的程序员。
有这个提示,说明错误在你的SQL语句中。你检查一下你代码中的SQL语句。