Lire une valeur dans ma bdd, et une erreur inutile !
Bonjour, en suivant l'exemple suivant :
Exemple d'extraction de données avec l'objet DataReader
Imports System
Imports System.Data.SqlClient
Imports System.IO
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Namespace ExempleAdoNetVBNET
Public Class CommandeSQL
Public Shared Sub Main()
Dim strConnexion As String = "Data Source=localhost; Integrated Security=SSPI;" + "Initial Catalog=Northwind"
Dim strRequete As String = "SELECT CategoryID, CategoryName FROM Categories;" + "SELECT EmployeeID, LastName FROM Employees"
Try
Dim oConnection As New SqlConnection(strConnexion)
Dim oCommand As New SqlCommand(strRequete, oConnection)
oConnection.Open()
Dim oReader As SqlDataReader = oCommand.ExecuteReader()
Do
Console.WriteLine(ControlChars.Tab + "{0}" + ControlChars.Tab + "{1}", oReader.GetName(0), oReader.GetName(1))
While oReader.Read()
Console.WriteLine(ControlChars.Tab + "{0}" + ControlChars.Tab + "{1}", oReader.GetInt32(0), oReader.GetString(1))
End While
Loop While oReader.NextResult()
oReader.Close()
oConnection.Close()
Catch e As Exception
Console.WriteLine(("L'erreur suivante a été rencontrée :" + e.Message))
End Try
End Sub 'Main
End Class 'CommandeSQL
End Namespace 'ExempleAdoNetVBNET |
J’ai fais sa :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| Imports System
Imports System.Data.SqlClient
Imports System.IO
Public Class CommandeSQL
Private Connect AS new sqlconnection
Private cmd as new sqlcommand
Private MyRead as sqlDataRead
Sub new()
connect.connectionString = "serveur=toto;database=test;Persist Security info=false; integrated security=sspI;"
Public sub ouverture()
connect.open()
cmd.connection = connect
end sub
Public sub Fermetur()
connect.close()
end sub
Public sub Requette(Byval Requette As String)
cmd.commandtext=marequette
Myreader = cmd.executeReader()
end sub
end sub
End Class |
Et dans mas page, je veux simplement récupéré l’id le plus grand de ma table. Je fais:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Private maconnexion as new connexion
blabla....
maconnexion.ouverture()
maconnexion.requette("SELECT MAX(id) AS idmax FROM Gamme")
Try
tbid.text = maconnexion.myReader.getstring(1)
catch ex as exeption
toto.text = ex.message
end try
maconnexion.myreader.close()
blabla... |
J’établi une connexion a la base, mais le catch renvoi :"tentative non valide de lecture lorsque aucune donnée n'est pressante." Pour temps, il y a des entré dans ma base, il y en a 5. Avez vous une idée pour résoudre mon problème, car je ne c’est pas quoi chercher dans Google (tout ce que je trouve, indique que sa devrai marcher.
Merci d’avance.
Dr_shaman