Visual Studio 2005 初体验之三:使用DBFactory class连接数据库
在以前开发的项目中,连接到不同的数据源需要使用不同的代码,VS2005中的DBFactory class则可以完成对不同数据源的连接使用相同的代码。因此,如果在一个项目中需要使用多个数据源(如Access、SQLServer、Foxpro等),那么VS2005为开发者提供了很好的途径。
实例:
1、新建一个工程,按照图示的要求布置界面。
2、GetData button click事件的代码:
2.1、获得使用的数据库名称:
Dim myName As String = providerComboBox.SelectedItem.ToString()
2.2、定义连接数据库对象:
Dim myConnectionSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(myName)
Dim myProvider As DbProviderFactory = DbProviderFactories.GetFactory(myConnectionSettings.ProviderName)
Dim myConnection As DbConnection = myProvider.CreateConnection()
myConnection.ConnectionString = myConnectionSettings.ConnectionString
2.3、打开数据库连接对象:
myConnection.Open()
2.4、为datagrid定义一个数据集:
Dim myAdapter As DbDataAdapter = myProvider.CreateDataAdapter()
Dim myCommand As DbCommand = myProvider.CreateCommand()
Dim myCommand As DbCommand = myProvider.CreateCommand()
Dim myQuery As String = "SELECT * FROM SampleData"
Dim myDataSet As DataSet = New DataSet()
Dim myDataSet As DataSet = New DataSet()
myCommand.Connection = myConnection
myCommand.CommandText = myQuery
myCommand.CommandText = myQuery
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myDataSet)
2.5、绑定datagrid:
displayDataGridView.DataSource = myDataSet.Tables(0)
3、获得服务器和数据库连接字符串信息:
providerNameTextLabel.Text = "Provider: " & myConnectionSettings.ProviderName.ToString()
connectionStringLabel.Text = "ConnectString:" & myConnectionSettings.ConnectionString.ToString()
connectionStringLabel.Text = "ConnectString:" & myConnectionSettings.ConnectionString.ToString()
4、完整的代码如下:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.Common
Imports System.Configuration
Imports System.Diagnostics
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.Common
Imports System.Configuration
Imports System.Diagnostics
Public Class factoryClassesForm
Private Sub getDataButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getDataButton.Click
Try
Try
'' Get the name of the database we are going to use
Dim myName As String = providerComboBox.SelectedItem.ToString()
Dim myName As String = providerComboBox.SelectedItem.ToString()
'' Get the connection settings from the configuration file based on the name selected
'' in the ComboBox
Dim myConnectionSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(myName)
'' in the ComboBox
Dim myConnectionSettings As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(myName)
'' Create an instance of the DbProviderFactory by using the Provider name in the config file
Dim myProvider As DbProviderFactory = DbProviderFactories.GetFactory(myConnectionSettings.ProviderName)
Dim myProvider As DbProviderFactory = DbProviderFactories.GetFactory(myConnectionSettings.ProviderName)
'' Create the connection from the DbProvider factory, this code does not need to change for
'' for different providers
Dim myConnection As DbConnection = myProvider.CreateConnection()
'' for different providers
Dim myConnection As DbConnection = myProvider.CreateConnection()
'' Get the connection string from the connectionsettings
'' This gets us the specific provider by which we will connect
myConnection.ConnectionString = myConnectionSettings.ConnectionString
'' This gets us the specific provider by which we will connect
myConnection.ConnectionString = myConnectionSettings.ConnectionString
'' Open the connection
myConnection.Open()
myConnection.Open()
'' now we can create the DataAdapter and then create a command
Dim myAdapter As DbDataAdapter = myProvider.CreateDataAdapter()
Dim myCommand As DbCommand = myProvider.CreateCommand()
Dim myAdapter As DbDataAdapter = myProvider.CreateDataAdapter()
Dim myCommand As DbCommand = myProvider.CreateCommand()
'' Create the DataSet so that we can populate the datagrid
Dim myQuery As String = "SELECT * FROM SampleData"
Dim myDataSet As DataSet = New DataSet()
myCommand.Connection = myConnection
myCommand.CommandText = myQuery
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myDataSet)
Dim myQuery As String = "SELECT * FROM SampleData"
Dim myDataSet As DataSet = New DataSet()
myCommand.Connection = myConnection
myCommand.CommandText = myQuery
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myDataSet)
displayDataGridView.DataSource = myDataSet.Tables(0)
'' with other provider and connection string information
providerNameTextLabel.Text = "Provider: " & myConnectionSettings.ProviderName.ToString()
connectionStringLabel.Text = "ConnectString:" & myConnectionSettings.ConnectionString.ToString()
Catch ex As Exception
MessageBox.Show("There was an error retrieving data. Please try again.", "Alert")
End Try
End Sub
providerNameTextLabel.Text = "Provider: " & myConnectionSettings.ProviderName.ToString()
connectionStringLabel.Text = "ConnectString:" & myConnectionSettings.ConnectionString.ToString()
Catch ex As Exception
MessageBox.Show("There was an error retrieving data. Please try again.", "Alert")
End Try
End Sub
Private Sub factoryClassesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
providerComboBox.SelectedIndex = 0
End Sub
providerComboBox.SelectedIndex = 0
End Sub
End Class
5、运行后的效果如图: