回 帖 发 新 帖 刷新版面

主题:在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]

回复列表 (共2个回复)

沙发

把所有“OleDb”开头的,都改成“SQL”,比如:OleDbCommand改成SqlCommand

con.ConnectionString = “server=你的服务器名;database=你的数据库名;uid=你的用户名;pwd=你的密码”

板凳

添加内容到数据库时我是单击“确定”按扭,运行。“确定”的代码为
search.insert("" & TextBox1.Text & "','" & TextBox2.Text & "','" &  TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text  & "")
MsgBox("添加成功!", MsgBoxStyle.OKOnly, "提示")


我是依照你的方法去做了,但出现了一个问题:
“未将对象引入设置到对象的实例”


代码以黄色线条出现的是下面代码中的   ada.InsertCommand.CommandText = SQL
不知如何入手,请大师指点。谢谢

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
[em10][em10][em10][em10]
为了能够与sql server连接,我把整个模块代码进行了修改,就有了上面说的黄色线条的错误,修改为的模块代码为
Imports System.Data.SqlClient
Imports System.Data


Public Class search

    Public Shared con As SqlConnection
    Public Shared ada As SqlDataAdapter
    Public Shared ds As DataSet


    Public Shared Sub init()
        con = New SqlConnection
    
        '定义如何联接数据库
        Dim connStr As String
        con.ConnectionString = "Server=(local);Database=BookSystem;uid=sa;pwd=1234"
        connStr = con.ConnectionString
        '定义一个连接对象,连接到数据库
        Dim jgBookConn As New SqlConnection(connStr)
        jgBookConn.Open()
        '定义一个执行对象,用于Sql语句执行
        Dim jgBookCommand As New SqlCommand("select * from photolist")
        jgBookCommand.Connection = jgBookConn   '执行Sql语句
        '获取数据
        Dim jgReader As SqlDataReader
        jgReader = jgBookCommand.ExecuteReader(CommandBehavior.CloseConnection)

        ada = New SqlDataAdapter
        ada.SelectCommand = New SqlCommand
        ada.InsertCommand = New SqlCommand
        ada.UpdateCommand = New SqlCommand
        ada.DeleteCommand = New SqlCommand

        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

我来回复

您尚未登录,请登录后再回复。点此登录或注册