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)来处理与不同MS-Access数据库文件的连接(参见底部的代码)。该类的目的是允许从MS-Access数据库中检索数据并操纵数据。这很好用。我可以为AdminDatabase类的一个实例提供一个SQL语句,并用结果填充dataTable(代码部分中的getDataTable方法。现在我已经使用Visual Studio的数据设计器将数据集添加到项目中。
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:
现在我有点困惑。一旦用户选择了相关数据文件,AdminDatabase类就会设置连接字符串。由于新数据集也使用相同的设置,My.Settings.AdminConnectionString用于连接,这让我想到了以下问题:
-
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?
在实例化AdminDatabase对象后,我可以假设我使用数据设计器创建的数据集将连接到用户在运行时选择的MS-Access数据库文件吗?
-
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.
我应该在连接类中编写方法来访问使用数据设计器创建的数据集中的数据,还是直接访问数据集?我想我可以在某处设置一个方法来设置项目中的连接字符串设置。
-
How else can I approach this?
我怎么能接近这个?
Code
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
1 个解决方案
#1
2
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.
如果您使用强类型数据集作为返回而不是通用DataTable,我会将它们视为不同的方法,为正在完成的操作返回正确的数据集。
I personally don't use DataSets much anymore, so I'm not 100% sure if there is a cleaner way of doing it...
我个人不再使用DataSet了,所以我不能100%确定是否有更简洁的方法...
#1
2
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.
如果您使用强类型数据集作为返回而不是通用DataTable,我会将它们视为不同的方法,为正在完成的操作返回正确的数据集。
I personally don't use DataSets much anymore, so I'm not 100% sure if there is a cleaner way of doing it...
我个人不再使用DataSet了,所以我不能100%确定是否有更简洁的方法...