Visual Studio 2005 初体验之三:使用DBFactory class连接数据库

Visual Studio 2005 初体验之三:使用DBFactory class连接数据库
        在以前开发的项目中,连接到不同的数据源需要使用不同的代码,VS2005中的DBFactory class则可以完成对不同数据源的连接使用相同的代码。因此,如果在一个项目中需要使用多个数据源(如Access、SQLServer、Foxpro等),那么VS2005为开发者提供了很好的途径。
       
实例:
 
1、新建一个工程,按照图示的要求布置界面。
factory1.JPG

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 myQuery As String = "SELECT * FROM SampleData"
         Dim myDataSet As DataSet = New DataSet()
         myCommand.Connection = myConnection
         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()
 
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
 
Public Class factoryClassesForm
    Private Sub getDataButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getDataButton.Click
        Try
            '' Get the name of the database we are going to use
            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)
            '' Create an instance of the DbProviderFactory by using the Provider name in the config file
            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()
            '' Get the connection string from the connectionsettings
            '' This gets us the specific provider by which we will connect
            myConnection.ConnectionString = myConnectionSettings.ConnectionString
            '' Open the connection
            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()
            '' 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)
            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
 
    Private Sub factoryClassesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        providerComboBox.SelectedIndex = 0
    End Sub

End Class
 
5、运行后的效果如图:
factory2.JPG

转载于:https://www.cnblogs.com/xiongeee/archive/2006/12/11/589206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Himall3.0 操作手册.......................................................................................................................... 1 一、名词解释..................................................................................................................................... 5 1、角色....................................................................................................................................... 5 1.1 平台............................................................................................................................... 5 1.2 商家............................................................................................................................... 6 1.3 自营店........................................................................................................................... 6 1.4 门店............................................................................................................................... 7 1.5 会员............................................................................................................................... 9 1.6 推广员........................................................................................................................... 9 1.7 销售员........................................................................................................................... 9 2. 商品.....................................................................................................................................10 2.1 商品分类.....................................................................................................................10 2.2 商品类型.....................................................................................................................12 2.3 商品品牌.....................................................................................................................12 2.4 商品属性.....................................................................................................

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值