ERLEDIGT
JA
JA
ANTWORTEN
2
2
ZUGRIFFE
718
718
EMPFEHLEN
-
12.09.08 09:50 #1
- Registriert seit
- Feb 2005
- Ort
- Bad Dürkheim
- Beiträge
- 5
Hallo zusammen,
ich versuche, mir Visual Basic mit Visual Studio 2005 anzueignen. Ich habe mir eine Testtabelle im SqlServer 2005 angelegt. Nun will ich Daten mit sqlReader auslesen und anzeigen. Einzelne Werte mit Scalar funktionieren.
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
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parametrisierte Abfrage der Testtabelle[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button2.Click[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] con [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlConnection = GetConnection()[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sql [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"select NID,Name,Passwort from dbo.tbl_01 where Gruppe = @Gruppe"[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand(sql, con)[/SIZE] [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parameter deffinieren[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@NID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.Int, 7)[/SIZE] [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 20)[/SIZE] [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Passwort"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 7)[/SIZE] [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Gruppe"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 10)[/SIZE] [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parameter füllen[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Gruppe"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FA"[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@NID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NID"[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Name"[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Passwort"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Passwort"[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Komando ausführen[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sdr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlDataReader = cmd.ExecuteReader[/SIZE] [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Datensätze anzeigen[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Do While (sdr.Read())[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Loop[/COLOR][/SIZE] [/COLOR][/SIZE][SIZE=2]sdr.Close()[/SIZE] [SIZE=2]con.Close()[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE] [/COLOR][/SIZE]
Was ist an meinem Code falsch, da ich folgende Fehlermeldung bekomme:
Programmzeile
Dim sdr As SqlDataReader = cmd.ExecuteReader wird gelbhinterlegt
Meldung: Konvertierung des Parameterwertes von String in Int32 fehlgeschlagen.
Außerdem, wie zeige ich die ausgelesenen Werte in 3 Textboxen an, nicht mit den vorgefertigten MS-Teilen sonder per Code.
Für Eure Hilfe sage ich jetzt schon mal Danke.
Gruß Peter
-
Hi
Du legst beim Erstellen der Parameter fest, dass @NID vom Typ Integer ist.
hier übergibst du jedoch einen String.Code vbnet:1
cmd.Parameters("@NID").Value = "NID"
Edit:
Was wahrscheinlich noch zu einem Fehler führen wird:
Dein Command hat nur einen Parameter, du fügst jedoch 4 hinzu.Geändert von Nico Graichen (12.09.08 um 20:59 Uhr) Grund: Nachtrag
Grüße Nico
----------------------
Xing
----------------------
Zitat von Mark Twain (1835-1910)
Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
-
13.09.08 01:41 #3
- Registriert seit
- Feb 2005
- Ort
- Bad Dürkheim
- Beiträge
- 5
Danke Nico!
Tip war okay und ich konnte weitermachen und jetzt gehts
.
Die Anzeige klappt auch fast.
Gruß Peter
Ähnliche Themen
-
SQLDataReader Cursor? Zeilenanzahl?
Von Steve222 im Forum .NET DatenverwaltungAntworten: 1Letzter Beitrag: 23.11.07, 10:59 -
[C#]SQLDataReader / Zeilenumbruch unter ASP.Net
Von Noxxxon im Forum .NET Web und KommunikationAntworten: 1Letzter Beitrag: 09.10.07, 11:13 -
Anfängerproblem
Von VirusX im Forum VisualStudio & MFCAntworten: 1Letzter Beitrag: 10.01.07, 11:02 -
Anfängerproblem
Von pgd im Forum Cinema 4DAntworten: 12Letzter Beitrag: 30.01.06, 15:22 -
SqlDatareader und Transaktionen mit einer connection
Von harlequinn im Forum .NET ArchivAntworten: 1Letzter Beitrag: 04.05.05, 15:45





Zitieren

Login





