Autor Beitrag
bis11
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1247
Erhaltene Danke: 2

Apple Mac OSX 10.11

BeitragVerfasst: So 22.01.06 14:26 
Hallo,

ich arbeite mich gerade in ADO.NET und Datenbanken ein. Dazu habe ich ein Beispiel leicht verändert. Es funktioniert auch sehr gut.

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:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
using System;
using System.Data;
using System.Data.OleDb;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
  /// <summary>
  /// Zusammendfassende Beschreibung für Class1.
  /// </summary>
  class Class1
  {
    /// <summary>
    /// Der Haupteinstiegspunkt für die Anwendung.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      // Set Access connection and select strings.
      // The path to BugTypes.MDB must be changed if you build 
      // the sample from the command line:
      string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\Lagerverwaltung_2002.mdb";
      string strAccessSelect = "SELECT * FROM HardwareEingang";
      // Create the dataset and add the Categories table to it:
      DataSet myDataSet = new DataSet();
      OleDbConnection myAccessConn = null;
      try
      {
        myAccessConn = new OleDbConnection(strAccessConn);
      }
      catch(Exception ex)
      {
        Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
        return;
      }
      try
      {
        OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
        OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
        myAccessConn.Open();
        myDataAdapter.Fill(myDataSet,"HardwareEingang");
      }
      catch (Exception ex)
      {
        Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
        return;
      }
      finally
      {
        myAccessConn.Close();
      }
      // A dataset can contain multiple tables, so let's get them
      // all into an array:
      DataTableCollection dta = myDataSet.Tables;
      foreach (DataTable dt in dta)
      {
        Console.WriteLine("Found data table {0}", dt.TableName);
      }
      // The next two lines show two different ways you can get the
      // count of tables in a dataset:
      Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
      Console.WriteLine("{0} tables in data set", dta.Count);
      // The next several lines show how to get information on
      // a specific table by name from the dataset:
      Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["HardwareEingang"].Rows.Count);
      // The column info is automatically fetched from the database,
      // so we can read it here:
      Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["HardwareEingang"].Columns.Count);
      DataColumnCollection drc = myDataSet.Tables["HardwareEingang"].Columns;
      int i = 0;
      foreach (DataColumn dc in drc)
      {
        // Print the column subscript, then the column's name
        // and its data type:
        Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType);
      }
      DataRowCollection dra = myDataSet.Tables["HardwareEingang"].Rows;
      Console.WriteLine("Anzahl der Datensätze : {0}", dra.Count);
      for (int k = 0; k < dra.Count; k++)
      {
        for (int j = 0; j < 7; j++)
          foreach (DataRow dr in dra)
          {
            Console.WriteLine("{0} --> {1}", j, dr[j]);
          }
        Console.WriteLine(" ");
      }
    }
  }
}


Wenn ich die FOR-Schleife mit k weglasse, zeigt er mir immer nur den letzten Datensatz an. Wenn die Schleife drin ist, gibt es die Ausgabe im Anhang.

Wie kann ich jetzt durch die FOR-Schleife meine Datensätze einzeln untereinander anzeigen ?
Einloggen, um Attachments anzusehen!
Noodles
Hält's aus hier
Beiträge: 6

VS 2005

BeitragVerfasst: Do 26.01.06 19:29 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
foreach (DataRow dr in dataSet.Tables["..."].Rows)
{
    for (int i = 0; i < dr.ItemArray.Length; i++ )
        Console.Write(dr[i].ToString() + ",");

    Console.WriteLine();
}
bis11 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1247
Erhaltene Danke: 2

Apple Mac OSX 10.11

BeitragVerfasst: Sa 04.02.06 14:13 
Danke, so funktioniert es wunderbar.