2012-12-29 66 views
-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 

回答

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 
+0

它的工作:d:d:d謝謝(恩塔耍花招fash5) – Losha