Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点,是 Microsoft Office 的系统程序之一。用vba代码将access数据库连接求代码,代码如下:
Sub FYMXDL()
Dim XQID As Integer
Dim JZID As Integer
Dim FYID As Integer
Dim FBXZ As String '分包性质
Dim DW As String
Dim SARR(1 To 31) As Double
Dim rst As New ADODB.Recordset
mYpath = ThisWorkbook.Path &"\jzfydata.accdb"
Set cONn = CreateObject("ADODB.Connection")
cONn.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0Data Source=" &mYpath
cONn.ConnectionString = cONn.ConnectionString &"Jet OLEDB:Database "
cONn.Open
XQID = Cells(3, 2).Value
JZID = Cells(3, 5).Value
'清空改小区-建筑的费用明细
Sql = "delete from fymxb where 小区ID=" &XQID &" AND 建筑ID = " &JZID
cONn.Execute Sql
Const kshh = 7
hh = kshh
Do While Cells(hh, 3).Value >0
FYID = Cells(hh, 3).Value
FBXZ = Cells(hh, 11).Text
For i = 1 To 31
SARR(i) = Round(Cells(hh, 13 + i - 1).Value, 2)
Next i
Sql = Sql &"," &SARR(i)
Next i
Sql = Sql &" )"
cONn.Execute Sql
hh = hh + 1
Loop
End Sub
远程连接access数据库的几个方法:1.建立VPN(Virtual Private Network),然后把服务器中mdb文件所在的 Folder 共享即可。连接方法如下:
“Provider=Microsoft.Jet.OLEDB.4.0Data Source=\\服务器名\\文件夹 \\mydata.mdbJet
OLEDB:Database Password=datapassPersist Security Info=False”
2.把数据库放在Web 服务器上,使ADO或RDO通过RDS(Remote Data Service) 及IIS来实现:
如果服务器设置了ODBC DSN的话,连接方法如下:
"Provider=MS RemoteRemote Server=http://服务器名Remote Provider=MSDASQLDSN=AdvWorksUid=dataUserNamePwd=dataPassWord"
如果设置的是OLE DB Provider 的话,连接方法如下:
"Provider=MS RemoteRemote Server=http://服务器名Remote
Provider=Microsoft.Jet.OLEDB.4.0Data Source=数据库文件路径,"+ "admin,"+"" (数据库用户名为:admin;密码为空)
3.编写服务器程序,通过TCP/IP,传递Recordset。
4.使用第三方控件,如:ADO Anywhere或UDAParts RDB等。
5.使用XMLHTTP
应用举例
OledbConnection myconn = null
myconn = new OledbConnection("192.168.1.155", "DSN=mydataDRIVER={Microsoft Access Driver (*.mdb)}UID=testPWD=test")//其中服务器IP是 192.168.1.155 数据库名是 mydata 数据库用户名这密码都是test myconn.Open()
下面这段程序是连接数据库,并将excel表格内容写入数据库。代码如下:Sub FYMXDL()
Dim XQID As Integer
Dim JZID As Integer
Dim FYID As Integer
Dim FBXZ As String '分包性质
Dim DW As String
Dim SARR(1 To 31) As Double
Dim rst As New ADODB.Recordset
mYpath = ThisWorkbook.Path &"\jzfydata.accdb"
Set cONn = CreateObject("ADODB.Connection")
cONn.ConnectionString = "Provider=Microsoft.Ace.OleDB.12.0Data Source=" &mYpath
cONn.ConnectionString = cONn.ConnectionString &"Jet OLEDB:Database "
cONn.Open
XQID = Cells(3, 2).Value
JZID = Cells(3, 5).Value
'清空改小区-建筑的费用明细
Sql = "delete from fymxb where 小区ID=" &XQID &" AND 建筑ID = " &JZID
cONn.Execute Sql
Const kshh = 7
hh = kshh
Do While Cells(hh, 3).Value >0
FYID = Cells(hh, 3).Value
FBXZ = Cells(hh, 11).Text
For i = 1 To 31
SARR(i) = Round(Cells(hh, 13 + i - 1).Value, 2)
Next i
Sql = "INSERT INTO fymxb(小区ID,建筑ID,费用ID,分包性质,工作量,单价合计_中标,人工费_中标, 主材费_中标, 辅材费_中标, 机械费_中标, 管理费_中标, 利润_中标,规费_中标,税金_中标,合价_中标,单价合计_标准成本,人工费_标准成本,主材费_标准成本,辅材费_标准成本,机械费_标准成本,管理费_标准成本,利润_标准成本,规费_标准成本,税金_标准成本,合价_标准成本,单价合计_实际成本,人工费_实际成本,主材费_实际成本,辅材费_实际成本,机械费_实际成本,管理费_实际成本,利润_实际成本,规费_实际成本,税金_实际成本,合价_实际成本) VALUES (" &XQID &", " &JZID &", " &FYID &", '" &FBXZ &"'"
For i = 1 To 31
Sql = Sql &"," &SARR(i)
Next i
Sql = Sql &" )"
cONn.Execute Sql
hh = hh + 1
Loop
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)