DBFactory

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        
If e.KeyCode = Keys.Escape Then
            
Me.Close()
        
ElseIf e.KeyCode = Keys.Enter Then
            
Me.ListView1.Items.Clear()
            
Dim gSearch As GoodsSearch.Class1 = New GoodsSearch.Class1()
            
Dim dt As DataTable = New DataTable()
            dt 
= gSearch.GetGoodsInfo(Me.TextBox1.Text.Trim(), "access")
            
If dt.Rows.Count > 0 Then                
                
Dim i As Integer
                
For i = 0 To dt.Rows.Count - 1

                    
Me.ListView1.Items.Add(i + 1)
                    
Me.ListView1.Items(i).SubItems.Add(dt.Rows(i)(0).ToString)
                    
Me.ListView1.Items(i).SubItems.Add(dt.Rows(i)(2).ToString)
                
Next
                
'MsgBox("success")
            End If
        
End If
    
End Sub

Imports System.Data
Imports System
Imports System.Data.Common
Public Class Class1

    
Dim dbProvider As String
    
Dim connStr As String
    
Dim dbFiled As String

    
Public Function GetGoodsInfo(ByVal searchCondition As StringByVal dbType As StringAs DataTable


        
'Select Case searchCondition.Length      '判断搜索的字段类型
        '    Case 12
        '        dbFiled = "barcode"             '商品条码
        '    Case 6
        '        dbFiled = "code"                '商品码
        '    Case Else
        '        dbFiled = "Name"                '商品名称
        'End Select

        
If Char.IsDigit(searchCondition) Then
            dbFiled 
= "barcode"
        
Else
            dbFiled 
= "Name"

        
End If

        
Select Case dbType.Trim().ToUpper()     '数据库类型
            Case "SQLSERVER"
                dbProvider 
= "System.Data.SqlClient"
                connStr 
= "server=(local);database=sampleData;Integrated Security=true;"
            
Case "ACCESS"
                dbProvider 
= "System.Data.OleDb"
                connStr 
= "provider=microsoft.Jet.OleDb.4.0;Data Source=../../databases/base.mdb"
            
Case Else
                
MsgBox("error")
                
'Return
                'Exit Function
        End Select

        
Dim myProvider As DbProviderFactory = DbProviderFactories.GetFactory(dbProvider)
        
Dim conn As DbConnection = myProvider.CreateConnection()
        conn.ConnectionString 
= connStr
        
Dim ds As DataSet = New DataSet()
        
Try
            conn.Open()
            
Dim cmd As DbCommand = myProvider.CreateCommand()
            
Dim da As DbDataAdapter = myProvider.CreateDataAdapter()
            cmd.Connection 
= conn
            cmd.CommandText 
= "select barcode,code,name from COMMOD where " + dbFiled + " like  '%" + searchCondition + "%'"
            da.SelectCommand 
= cmd
            
'da.SelectCommand.Connection = conn
            'da.SelectCommand.CommandText = "select id,integerValue from sampleData where id=" + searchCondition + ""
            da.Fill(ds, "goodsInfo")
            
Return ds.Tables("goodsInfo")
        
Catch ex As Exception
            
MsgBox("无该商品信息! " + ex.Message)
        
Finally
            conn.Close()


        
End Try
        
Dim dt As DataTable = New DataTable()
        
Return dt



        
'Dim cmd As DbCommand = myProvider.CreateCommand()
        'cmd.Connection = conn
        'cmd.CommandText = "select id,integerValue from sampleData where id=" + searchCondition + ""
        'Dim dr As DbDataReader = cmd.ExecuteReader()

        
'dr.Close()
        'conn.Close()

        
'Return goodsInfo

    
End Function

end class
posted @ 2007-01-14 14:26  yongwnet  阅读(391)  评论(0编辑  收藏  举报