Autor Beitrag
Bluesonic_666
Hält's aus hier
Beiträge: 5


c# VS 2003
BeitragVerfasst: Do 20.10.05 11:02 
Hallo ich habe n paar kleine Probleme beim nutzen von int werten aus einer Access db.

Ich habe folgendes gemacht:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
      test= livdatum.ToString("MM")+"/"+livdatum.ToString("dd")+"/"+livdatum.ToString("yyyy");
      const string CONNSTR = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=blp.mdb;";
      string SQL = "SELECT DISTINCT tage,name FROM blpdaten WHERE von=#"+test+"# AND zimmer=100";

      OleDbConnection conn = new OleDbConnection(CONNSTR);
      OleDbCommand cmd = new OleDbCommand(SQL, conn);
      OleDbDataReader dr;             // DataReader-Objekt wird deklariert
      conn.Open();                    // Verbindung zur Datenbank wird geöffnet
      dr = cmd.ExecuteReader();       // DataReader-Objekt wird aus Command-Objekt erzeugt 
      while (dr.Read())               // so lange, bis False zurückgegeben wird
      {
        
        textBox1.Text += dr["name"];
        textBox2.Text += dr["tage"].ToString();
        
       // komplette Zeile zur Listbox hinzufügen
      }            
      dr.Close();
      conn.Close();

      xxxx = Convert.ToInt32(textBox2.Text);


Wie kann ich den wert aus tage als int und nicht als string auslesen bzw konvertieren.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt.
ani
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 77

Windows Vista Home Premium
C#(VS 2008)
BeitragVerfasst: Do 20.10.05 11:22 
versuchs mal mit

int.Parse();
Bluesonic_666 Threadstarter
Hält's aus hier
Beiträge: 5


c# VS 2003
BeitragVerfasst: Do 20.10.05 12:52 
wie wird das dann eingebunden?
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Do 20.10.05 14:18 
Du bastelst dort einen Datumswert in einen SQL string. Sowas ist generell die falsche Herangehensweise, bei Datumswerten forderst du den nächsten Bug aufgrund von unterschiedlichen Regionaleinstellungen förmlich heraus. ;)
Außerdem sind Connection, Command und Reader nicht umsonst IDisposable. Rufst du kein Dispose auf[meta]direkt per try-finally, oder praktischerweise mit einem using-Block[/meta], hast du ein MemLeak. :shock:

Hier mal ein Bleistift, wie du es lösen könntest:
ausblenden volle Höhe C#-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:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
static BlpDings[] GetBlpDaten(DateTime livDatum)
{

  const string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=blp.mdb;";
  const string selectBlpDatenSql =
    "SELECT DISTINCT Tage\n" +
    "               ,Name\n" +
    "FROM   BlpDaten\n" +
    "WHERE  Von = :LivDatum and\n" +
    "       Zimmer = 100";

  using (IDbConnection connection = new OleDbConnection(connectionString))
  using (IDbCommand command = connection.CreateCommand())
  {
    connection.Open();
    command.CommandText = selectBlpDatenSql;

    IDataParameter livDatumParameter = command.CreateParameter();

    livDatumParameter.DbType = DbType.Date;
    livDatumParameter.Value = livDatum;

    command.Parameters.Add(livDatum);

    using (IDataReader reader = command.ExecuteReader())
    {
      if (!reader.IsClosed)
      {
        List<BlpDings> blpDaten = new List<BlpDings>();
        while (reader.Read())
        {
          int tage = reader.GetInt32(0);
          string name = reader.GetString(1);

          blpDaten.Add(new BlpDings(tage, name));

        }

        BlpDings[] result = new BlpDings[blpDaten.Count];

        blpDaten.CopyTo(result);
        return result;
      }
    }
  }
  return new BlpDings[0];
}


An die listBox bekommst du es so: listBox.DataSource = GetBlpDaten(iregndeinDatumsWert);
Hier noch die Klasse, die einem Datesatz entspricht:
ausblenden C#-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:
class BlpDings
{
  int tage;
  string name;

  public int Tage
  {
    get { return tage; }
    set { tage = value; }
  }

  public string Name
  {
    get { return name; }
    set { name = value; }
  }

  public BlpDings(int tage, string name)
  {
    this.name = name;
    this.tage = tage;
  }

  public override string ToString()
  {
    return string.Format("{0} ({1} Tage)",
                         name,
                         tage);
  }
}


Sorry, dass ich dir dass nur so hinschludere...
Schaue es dir an[meta]unbekannte dinge bitte auch in der SDK Dku nachschlagen[/meta] und frage wo der der Schuh drückt , wenn er überhaupt drückt. Ich schaue nacher/heute abend nochmal vorbei und werde drauf eingehen. ;)