再议数据存取
某些程序运行中需要大量存取数据,以前问过得到2种解决方案,一是用INI文件做中介,另一种是用EXCEL文件做中介,前者不便于后端维护,后者存取大量数据发现速度较慢,且2种方案都要写大量代码,有没有既方便又快捷的方法呢?我想用数据库文件应该能解决问题!但不知怎样"引用"?
下面建个简单模型来探讨一下!
目标:从某外部文件取出3组数据,将其和显示在Text1上。
用EXCEL做外部文件(目录D:\Data.Xls,3组数据分别在B3、B4和B5上),代码如下:
Private Sub Command1_Click()
Text1.Text = Val(GetData1) + Val(GetData2) + Val(GetData3)
End Sub
Private Function GetData1() As String
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.open("D:\Data.Xls")
Set xlSheet = xlBook.Worksheets(1) '设置活动工作表
GetData1 = xlSheet.Range("$B$3")
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Function
Private Function GetData2() As String
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.open("D:\Data.Xls")
Set xlSheet = xlBook.Worksheets(1)
GetData2 = xlSheet.Range("$B$4")
xlBook.Close (True)
xlApp.Quit
Set xlApp = Nothing
End Function
Private Function GetData3() As String
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.open("D:\Data.Xls")
Set xlSheet = xlBook.Worksheets(1)
GetData3 = xlSheet.Range("$B$5")
xlBook.Close (True)
xlApp.Quit
Set xlApp = Nothing
End Function
那么用Access数据库做外部文件应该怎样实现上述目标呢?