求VB.NET2010连接MDB数据库后,添加、修改和删除等写法示例

2024-11-15 01:43:30
推荐回答(1个)
回答1:

引用 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那个太强了)