-1

このコードを使用して、データベースから特定の値を選択しようとしました:

    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 
4

1 に答える 1

1

問題は、実際にはネイティブのSystem.Data.OleDbクラスを使用する必要があるのに、COMオブジェクトを使用しようとしていることです。

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
于 2012-12-30T00:17:49.750 に答える