【发布时间】:2012-03-16 18:18:46
【问题描述】:
这可能已经得到解答,但我的搜索没有找到我要找的东西。
基本上,我正在开发一个允许用户在设计时构建查询的应用程序,即对于没有 SQL 必备知识的用户
到目前为止,该应用程序允许用户从数据库中选择他们希望开始查询的表(我现在不会详细介绍其余部分)
我的困惑是这样的;我已经在一个子例程中与数据库建立了连接,该子例程获取架构信息并对其进行过滤以仅显示数据库中的可用表,然后将数据编译到一个列表框中,这是那个子:
Public Sub getSchemaInfo()
Dim ds As New DataSet
Dim dt As New DataTable
Dim con As New OleDbConnection
Dim strDatabaseLocation As String = Application.StartupPath
Dim da As New OleDbDataAdapter
Dim i As Integer
'ds.Tables.Add(dt)
con.ConnectionString = "Provider=microsoft.jet.oledb.4.0; data source = " & strDatabaseLocation & _
"\EmployeeDepartment.mdb"
'clear listbox of any data first
frmAddTable.lbTables.Items.Clear()
'Try catch block used to handle connection errors gracefully
Try
con.Open()
'Accessing methods to obtain schema information
dt = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() _
{Nothing, Nothing, Nothing, "TABLE"})
'loop datatable to store schema information within it
For i = 0 To dt.Rows.Count - 1
'compile lbtables with a list of available tables from the database
frmAddTable.lbTables.Items.Add(dt.Rows(i)!TABLE_NAME.ToString())
Next
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Data Load Error", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
End Try
con.Close()
如您所见,最顶部是有关与数据库的连接以及将信息加载到数据集中的所有信息。
我的问题是这样的;每当我需要访问数据库和其中的任何信息时,我是否必须执行所有连接过程(oledbconnection 等) 还是有什么方法可以为连接函数创建一个类,并在需要连接时简单地引用它们?
例如,我现在正在创建另一个子程序,它根据列表框中选择的表收集列,并将其显示回相关复选框中的主表单,再次连接到数据库,因此我需要执行所有的连接过程吗?
任何信息都会非常有用,谢谢!
【问题讨论】:
-
阅读更多内容后,这是否适合称为“连接池”的东西?如果是这样,有人可以详细说明吗?
标签: sql vb.net visual-studio-2010 oledb