主题:在vb.net中怎样把连接access模块代码转化为连接SQL代码
在vb.net中连接access有个模块的代码是这样的
Imports System.Data.OleDb
Imports System.IO
Public Class search
Public Shared con As OleDbConnection
Public Shared ada As OleDbDataAdapter
Public Shared ds As DataSet
Public Shared Sub init()
con = New OleDbConnection
con.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""" & Directory.GetCurrentDirectory & "/data/data.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
ada = New OleDbDataAdapter
ada.SelectCommand = New OleDbCommand
ada.InsertCommand = New OleDbCommand
ada.UpdateCommand = New OleDbCommand
ada.DeleteCommand = New OleDbCommand
ada.SelectCommand.Connection = con
ada.InsertCommand.Connection = con
ada.UpdateCommand.Connection = con
ada.DeleteCommand.Connection = con
End Sub
Public Shared Function query(ByVal SQL As String) As DataSet
Try
ds = New DataSet
ada.SelectCommand.CommandText = SQL
ada.Fill(ds)
Return (ds)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Shared Function insert(ByVal SQL As String) As Integer
Dim i As Integer
ds = New DataSet
ada.InsertCommand.CommandText = SQL
con.Open()
i = ada.InsertCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
End Function
Public Shared Function update(ByVal SQL As String) As Integer
Try
Dim i As Integer
ds = New DataSet
ada.UpdateCommand.CommandText = SQL
con.Open()
i = ada.UpdateCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Shared Function delete(ByVal SQL As String) As Integer
Dim i As Integer
ds = New DataSet
ada.DeleteCommand.CommandText = SQL
con.Open()
i = ada.DeleteCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
End Function
End Class
想把它转为用SQL SERVER连接,也一样单独成模块,不知如何修改,请各位大师指点
[em10][em10]
Imports System.Data.OleDb
Imports System.IO
Public Class search
Public Shared con As OleDbConnection
Public Shared ada As OleDbDataAdapter
Public Shared ds As DataSet
Public Shared Sub init()
con = New OleDbConnection
con.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""" & Directory.GetCurrentDirectory & "/data/data.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
ada = New OleDbDataAdapter
ada.SelectCommand = New OleDbCommand
ada.InsertCommand = New OleDbCommand
ada.UpdateCommand = New OleDbCommand
ada.DeleteCommand = New OleDbCommand
ada.SelectCommand.Connection = con
ada.InsertCommand.Connection = con
ada.UpdateCommand.Connection = con
ada.DeleteCommand.Connection = con
End Sub
Public Shared Function query(ByVal SQL As String) As DataSet
Try
ds = New DataSet
ada.SelectCommand.CommandText = SQL
ada.Fill(ds)
Return (ds)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Shared Function insert(ByVal SQL As String) As Integer
Dim i As Integer
ds = New DataSet
ada.InsertCommand.CommandText = SQL
con.Open()
i = ada.InsertCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
End Function
Public Shared Function update(ByVal SQL As String) As Integer
Try
Dim i As Integer
ds = New DataSet
ada.UpdateCommand.CommandText = SQL
con.Open()
i = ada.UpdateCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Shared Function delete(ByVal SQL As String) As Integer
Dim i As Integer
ds = New DataSet
ada.DeleteCommand.CommandText = SQL
con.Open()
i = ada.DeleteCommand.ExecuteNonQuery()
con.Close()
ada.Fill(ds)
Return (i)
End Function
End Class
想把它转为用SQL SERVER连接,也一样单独成模块,不知如何修改,请各位大师指点
[em10][em10]