从数据库中选择特定值

时间:2012-12-29 23:01:45

标签: asp.net vb.net

我尝试使用以下代码从数据库中选择特定值:

    Dim rs, data, lo

    data = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
    Server.MapPath("games.mdb")

    rs = Server.CreateObject("ADODB.Recordset")

    rs.open("select * FROM [index] where ID1=1 ", data)

    Image1.ImageUrl = "images/" + rs("url").ToString

    lo =rs("url")

    rs.MoveNext()
    rs.Close()

当我运行它时,我得到:

show 

System.__ComObject 

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试使用COM对象,而实际上您应该使用本机System.Data.OleDb类。

基于Microsoft提供的示例,这里是使用本机类重写代码:

    Dim connectionString As String

    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("games.mdb")

    Using connection As New System.Data.OleDb.OleDbConnection(connectionString)
        Using command As New System.Data.OleDb.OleDbCommand("select * FROM [index] where ID1=1")
            ' Set the Connection to the new OleDbConnection.
            command.Connection = connection

            ' Open the connection and execute the select command. 
            Try
                connection.Open()
                Dim oReader As System.Data.OleDb.OleDbDataReader

                oReader = command.ExecuteReader()
                If oReader.Read() Then
                    Image1.ImageUrl = "images/" + oReader("url").ToString
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Using
        ' The connection is automatically closed when the code exits the Using block. 
    End Using
相关问题