Autor |
Beitrag |
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 13:17
Hallo Miteinander;
ich hab hier ein kleines Problem.Bei mir wird in der sich öffnenden Konsole nichts angezeigt.Hier mal der Quelltext:
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
| using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb;
namespace Testverbindung { class Program { static void Main(string[] args) { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand() cmd.CommandText = "SELECT * FROM tblAdressen"; Console.ReadLine(); } } } |
Hab ich da irgendwas falsch gemacht.Ich bekomme nämlich keine Fehlermeldung.Und die Konsole bleibt leer
Moderiert von Tino: CS-Tags hinzugefügt.
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 13:24
Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
| using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb;
namespace Testverbindung { class Program { static void Main(string[] args) { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb"); conn.Open(); OleDbCommand cmd = new OleDbCommand() cmd.CommandText = "SELECT * FROM tblAdressen"; OleDbDataReader dr = cmd.ExecuteReader(); while(dr.Read) Console.WriteLine(dr["Spaltenname Spalte1"] dr["Spaltenname Spalte"] usw.); dr.Close(); con.Close(); } } } |
Du musst die Ausgabe mit einen DataReader durchführen. Oben im Code sieht du, wies geht. Natürlich musst die die Spaltennamen richtig eintragen
MFG
Orothred
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 13:40
Wenn ich es so eingebe wie du beschrieben hast kommen 3 Fehlermeldungen:
1.-> ") erwartet."
2.-> "; erwartet."
3.-> "Ungültiger Ausdruck ")"
Alle Fehler verweisen auf die Zeile : Console.WriteLine(dr["Adr_Vorname"] dr["Adr_Nachname"]);
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 13:44
selbst bisschen denken kann auch helfen ^^
du musst den string schon richtig eingeben, sonst sieht er den string nach dem ersten befehl als beendet an und fragt sich was der rest da soll.
probiers mal so:
Quelltext 1:
| Console.WriteLine(dr["Adr_Vorname"]+" "+dr["Adr_Nachname"]); |
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 13:50
Ok.Ich werd mich das nächste mal anstrengen *FG*.Jetzt hab ich aber noch ein Problem mit dem Datareader.Beim ausführen wird folgende Fehlermeldung ausgegeben
"ExecuteReader: Connection-Eigenschaft wurde nicht initialisiert."
Der Fehler verweist auf die Zeile OleDbDataReader dr = cmd.ExecuteReader();
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 13:54
oh, mein fehler...du musst dem OleDbCommand nach das Connection-Objekt übergeben, also:
Quelltext 1:
| OleDbCommand cmd = new OleDbCommand(conn); |
dann sollte es funktionieren (hoffe ich ^^)
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 14:01
Tja dem ist wohl leider nicht so.Aus eins mach zwei (Fehlermeldungen.Hier noch mal der gesammte Text:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace Testverbindung
{
class Program
{
static void Main(string[] args)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand(conn);
cmd.CommandText = "SELECT * FROM tblAdressen";
OleDbDataReader dr = cmd.ExecuteReader();
while(dr.Read())
Console.WriteLine(dr["Adr_Vorname"] + " " + dr["Adr_Nachname"]);
dr.Close();
conn.Close();
}
}
}
Und hier die Meldungen:
Fehler 1 Die beste Übereinstimmung für die überladene System.Data.OleDb.OleDbCommand.OleDbCommand(string)-Methode hat einige ungültige Argumente.
Fehler 2 1-Argument: kann nicht von "System.Data.OleDb.OleDbConnection" in "string" konvertiert werden.
beziehen sich beide auf diese Zeile OleDbCommand cmd = new OleDbCommand(conn);
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 14:09
Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
| using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb;
namespace Testverbindung { class Program { static void Main(string[] args) { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb"); conn.Open(); string strSQL = "SELECT * FROM tblAdressen"; OleDbCommand cmd = new OleDbCommand(strSQL, conn); OleDbDataReader dr = cmd.ExecuteReader(); while(dr.Read()) Console.WriteLine(dr["Adr_Vorname"] + " " + dr["Adr_Nachname"]); dr.Close(); conn.Close(); } } } |
dann speicher den SQL-Befehl in einem String ab und übergib den string und die Connection an den DataReader (wie oben im Code gezeigt). Wenns jetzt net geht, bin ich mit meinem Latein am Ende
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 14:12
Ja so hab ich des jetzt auch gemacht *FG*
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb");
conn.Open();
string strSQL = "SELECT * FROM tblAdressen";
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
OleDbDataReader dr = cmd.ExecuteReader();
while(dr.Read())
Console.WriteLine(dr["Adr_Nachname"]+" "+dr["Adr_Vorname"]);
dr.Close();
conn.Close();
Console.ReadLine();
Aber trotzdem vielen Dank für die kompetente und schnelle Hilfe
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 14:15
funktionierts jetz?
achja, noch als tipp: wenn du code postest, dann formatier ihn auch so
einfach [Code]"Hier Code einfügen"[/Code]
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 14:18
Ja sicher das.Funzt 1A.Ok das mit dem Code werd ich in Zukunft so machen.
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 14:57
So ich hab eine neues Problem.Ich hab irgendwie Probleme mit der SQL Syntax INSERT INTO
Quelltext 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:
| using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb;
namespace Beispiele { class _002_ExecuteNonQuery { static void Main(string[] args) { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Adressen.mdb");
string strSQL = "INSERT INTO tblAdressen(Adr_Vorname, Adr_Nachname, Adr_Strasse,) VALUES('Guenther','Meier','Hauptstr.1')";
try { conn.Open(); OleDbCommand cmd = new OleDbCommand(strSQL, conn); cmd.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine("Fehlermeldung: {0}", e.Message); } conn.Close(); Console.ReadLine(); } } } |
Wo is des Problem? Das Konsolenfenster zeigt mir die Meldung :Fehler in der INSERT INTO-Anweisung.Eigentlich sollte die Syntax stimmen
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 14:59
das erste was mir ins auge sticht is der punkt bei Hauptstr.1
lass den mal testweise weg, weiß jetz net genau, obs daran liegen kann
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 15:02
Nein.Daran liegt es leider nicht.Ich hatte auch schon bei Guenther das ü drin.Wars aber auch net
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 15:05
gut, dann könnte es noch das komma hinter adr_strasse sein. mach das mal weg und probier nochmal
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 15:09
Okeee soweit ganz gut.Das Komma wars.Sowie die gesamte SQL-Syntax verstehe sollte doch eigentlich ein Eintrag in die Datenbank gemacht werden oder irre ich ????
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 15:11
|
|
Orothred
Beiträge: 86
C# (VS 2005)
|
Verfasst: Fr 28.09.07 15:12
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 15:14
Ja hat es auch gemacht.Man sollte doch vielleicht die richtige Datenbank nehmen und net ne andere mit gleichem Namen*FG*
|
|
Scooby2007
Beiträge: 16
|
Verfasst: Fr 28.09.07 15:16
Ich hätte noch eine Frage dann geb ich Ruhe.Was genau passiert in diesem Codeabschnitt bzw. wofür is der da :
OleDbCommand cmd = new OleDbCommand(strSQL, conn);
cmd.ExecuteNonQuery();
|
|