寻求代码设计建议:与VB.Net和Ms-Access合作 [英] Asking for code design advice: working with VB.Net and Ms-Access

查看:81
本文介绍了寻求代码设计建议:与VB.Net和Ms-Access合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许天真地,我创建了一个类(AdminDatabase)来处理与不同的MS-Access数据库文件的连接(请参见底部的代码).该类的目的是允许从MS-Access数据库检索数据并处理数据.这很好.我可以通过SQL语句为AdminDatabase类的实例提供数据,并在结果中填充数据表(在 code 部分中的 getDataTable 方法.现在,我已向项目中添加了数据集使用Visual Studio的数据设计器.

Perhaps naively, I created a class (AdminDatabase) to handle connection to different MS-Access database files (see code at bottom). The purpose of the class was to allow retrieval of data from a MS-Access database and to manipulate the data. This works well. I can feed an instance of the AdminDatabase class an SQL statement and fill a dataTable with the result (getDataTable method in the code section. Now I have added data sets to the project using Visual Studio's data designer.

现在我有点困惑.一旦用户选择了相关的数据文件,则AdminDatabase类将设置连接字符串.由于新数据集也使用相同的设置My.Settings.AdminConnectionString进行连接,这使我想到了以下问题:

Now I am a bit confused. The AdminDatabase class set's the connection string once the user has chosen the relevant data file. Since the new data sets also use the same setting, My.Settings.AdminConnectionString for the connection, which brings me to my questions:

  1. 实例化AdminDatabase对象之后,我是否可以假定我使用数据设计器创建的数据集将在运行时连接到用户选择的MS-Access数据库文件?

  1. After instantiating a AdminDatabase object, can I assume that the data sets I created using the data designer will connect to the MS-Access database file chosen by the user at run time?

我应该在我的连接类中编写一些方法来访问由数据设计器创建的数据集中的数据,还是可以直接访问这些数据集?我想我可能只是在某处可以在项目中设置连接字符串设置的方法.

Should I write methods in my connection class to access data in the data sets created with the data designer, or would accessing the data sets directly be ok? I guess I could just have a method somewhere to set the connection string setting in the project.

我还能如何处理?

代码



     public class AdminDatabase
       ' stores the connection string which is set in the New() method
       dim strAdminConnection as string

       public sub New()
       ...
       adminName = dlgopen.FileName
       conAdminDB = New OleDbConnection
       conAdminDB.ConnectionString = "Data Source='" + adminName + "';" + _
           "Provider=Microsoft.ACE.OLEDB.12.0"

       ' store the connection string in strAdminConnection
       strAdminConnection = conAdminDB.ConnectionString.ToString()
       My.Settings.SetUserOverride("AdminConnectionString", strAdminConnection)
       ...
       End Sub

       ' retrieves data from the database
       Public Function getDataTable(ByVal sqlStatement As String) As DataTable
            Dim ds As New DataSet
            Dim dt As New DataTable
            Dim da As New OleDbDataAdapter
            Dim localCon As New OleDbConnection


            localCon.ConnectionString = strAdminConnection

            Using localCon
                Dim command As OleDbCommand = localCon.CreateCommand()
                command.CommandText = sqlStatement
                localCon.Open()
                da.SelectCommand = command
                da.Fill(dt)
                getDataTable = dt
            End Using

        End Function
    End Class

推荐答案

如果您使用强类型的数据集而不是通用的DataTable作为返回值,那么我会认为它们是不同的方法,返回的操作正确的数据集是完成.

If you are using strongly typed datasets for the returns rather than the generic DataTable, I would see those as being different methods, returning the proper dataset for the operation being completed.

我个人不再使用DataSets,所以我不确定100%是否有更清洁的方法...

I personally don't use DataSets much anymore, so I'm not 100% sure if there is a cleaner way of doing it...

这篇关于寻求代码设计建议:与VB.Net和Ms-Access合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆