mercoledì 16 maggio 2012

MS: 70-516 Samples of chapter 1 Lesson 1 part 1

Perchè No ? Mi pare oppurtuno condividere chi come me sta affrontando questi studi. Nel mio caso sono partito proprio dall'inizio, un po' per comprendere fino a che punto sono arrivato un po' appunto per condividere quest'esperienza. Spero come sempre di essere d'aiuto.
static void Main(string[] args)
        {
            /* le classi disconnesse sono elemeniti cardine del framework..
             * La loro presenza risale alla versione 1.0
             * Non sono mai state dichiarate come obsolete e sono di uso diffuso.
             * 
             * la particolarità e che non sono connesse direttamente ai dati
             */

            Sample_01();

            Sample_02();

            Sample_03();

            Sample_04();

            Sample_05();

            Sample_06();

            Sample_07();

            Sample_08();

            Sample_09();
        }
Ho preparato i primi nove esempi, sono decisamente semplici ma permettono di comprendere quanto è proposto nel libro d'esame.
static void Sample_01()
        { 
            // Creazione di una DataTable 
            // esempio decisamente semplice

            // questa è creata senza esplicitare 
            // il nome
            DataTable dt0 = new DataTable();

            // questa è creata esplicitando il nome
            DataTable dt1 = new DataTable("NewDataTable");


            Console.WriteLine("dt0 Name:" + dt0.TableName);
            Console.WriteLine("dt1 Name:" + dt1.TableName);
        }
static void Sample_02()
        { 
            // aggiungere una o più colonne ad una 
            // datatable
            
            DataTable dt1 = new DataTable("NewDataTable");

            //
            // per ogni colonna è necessario 
            // esplicitare la definizione
            // al fine di creare uno schema
            // 

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int); // indica il tipo della colonna
            dc0.AllowDBNull = false;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string); 
            dc1.MaxLength = 50;  // indica la lunghezza massima del campo
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true; // indica che potrà contenere un null value
            dt1.Columns.Add(dc2);



            Console.WriteLine("dt1 Name:" + dt1.TableName +" has columns:");
            for (int i = 0; i < dt1.Columns.Count; i++)
            {
                Console.WriteLine("Column Name:" + dt1.Columns[i].ColumnName);
            }
        }
static void Sample_03()
        {
            // aggiungere una chiave primaria alla
            // datatable

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int); 
            dc0.AllowDBNull = false;
            //
            // indichiamo che la colonna ID 
            // sarà un auto increment
            // -specificando il "quanto" per l'incremento dc0.AutoIncrementStep
            // -specificando il "partendo da :"  dc0.AutoIncrementSeed
            //
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;

            dt1.Columns.Add(dc0);


            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;  
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true; 
            dt1.Columns.Add(dc2);

            //
            // aggiungiamo la collonna all' array dell colonne chiave.
            // Questo campo è dato da una o più colonne
            // in modo da poter considerare anche chiavi complesse.
            // 
            dt1.PrimaryKey = new DataColumn[] { dc0 };
        }
static void Sample_04()
        {
            // aggiungere una o più righe 
            // ossia aggiungere una o più DataRow

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };


            dt1.Rows.Add(new object[] { 1, "Fabio", "Team Leader"});
            dt1.Rows.Add(new object[] { 2, "Fabio", "Developer" });
            dt1.Rows.Add(new object[] { 3, "Fabio", "Tester" });

        }
static void Sample_05()
        {
            // Osservare gli stati di una dataRow
            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = true;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };

            //
            // esempi di cambiamento di stato di una
            // DataRow
            //
            DataRow dr = dt1.NewRow();
            Console.WriteLine("Row State " + dr.RowState);

            dt1.Rows.Add(dr);            
            Console.WriteLine("Row State " + dr.RowState);

            dr["Name"] = "Fabio";
            Console.WriteLine("Row State " + dr.RowState);

            dt1.AcceptChanges();
            Console.WriteLine("Row State " + dr.RowState);

            dr["Name"] = "Claudio";
            Console.WriteLine("Row State " + dr.RowState);

            dt1.RejectChanges();
            Console.WriteLine("Row State " + dr.RowState);

            dr["Name"] = "Claudio";
            Console.WriteLine("Row State " + dr.RowState);
            dt1.AcceptChanges();

            dr.Delete();
            Console.WriteLine("Row State " + dr.RowState);
            dt1.AcceptChanges();
        }
static void Sample_06()
        {
            // verificare la versione di ogni riga.

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };


            dt1.Rows.Add(new object[] { 1, "Fabio", "Team Leader" });
            dt1.Rows.Add(new object[] { 2, "Fabio", "Developer" });
            dt1.Rows.Add(new object[] { 3, "Fabio", "Tester" });


            DataRow dr = dt1.Rows[1];

            dr["Name"] = "Claudio";

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                foreach (string versionString in Enum.GetNames(typeof(DataRowVersion)))
                {
                    DataRowVersion version = ( DataRowVersion)Enum.Parse( typeof(DataRowVersion), versionString);

                    if (dt1.Rows[i].HasVersion(version))
                    {
                        Console.WriteLine(string.Format( "Version: {0} Value: {1} \r\n", version, dt1.Rows[i]["Name", version]));
                    }
                }
            }
        }
static void Sample_07()
        {
            // Due metodi importanti di DataRow

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };


            DataRow dr1 = dt1.Rows.Add(new object[] { 1, "Fabio", "Team Leader" });
            DataRow dr2 = dt1.Rows.Add(new object[] { 2, "Fabio", "Developer" });
            DataRow dr3 = dt1.Rows.Add(new object[] { 3, "Fabio", "Tester" });

            dt1.AcceptChanges();

            // imposta la riga come AGGIUNTA
            if (dr1.RowState == DataRowState.Unchanged)
            {
                dr1.SetAdded();
                Console.WriteLine("Row State " + dr1.RowState);
            }

            if (dr2.RowState == DataRowState.Unchanged)
            {
                // imposta la riga come MODIFICATA                
                dr2.SetModified();
                Console.WriteLine("Row State " + dr2.RowState);
            }
        }
static void Sample_08()
        {
            // Clone / Copy di una DataTable

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };

            DataRow dr1 = dt1.Rows.Add(new object[] { 1, "Fabio", "Team Leader" });
            DataRow dr2 = dt1.Rows.Add(new object[] { 2, "Fabio", "Developer" });
            DataRow dr3 = dt1.Rows.Add(new object[] { 3, "Fabio", "Tester" });

            // esegue la copia della tabella
            DataTable dt2 = dt1.Copy();

            // esegue il colone della tabella
            DataTable dt3 = dt1.Clone();

            dt1.Rows[0][1] = "Demo";

            Console.WriteLine("DT1 :" + dt1.Rows[0][1]);
            Console.WriteLine("DT2 :" + dt2.Rows[0][1]);

            dt2.Rows[0][1] = "Test";

            Console.WriteLine("DT1 :" + dt1.Rows[0][1]);
            Console.WriteLine("DT2 :" + dt2.Rows[0][1]);


            if (dt3.Rows.Count > 0)
            {
                Console.WriteLine("DT3 :" + dt3.Rows[0][1]);
            }
        }
static void Sample_09()
        {
            // Clone / Copy di una DataTable e delle sue righe

            DataTable dt1 = new DataTable("NewDataTable");

            DataColumn dc0 = new DataColumn("ID");
            dc0.DataType = typeof(int);
            dc0.AllowDBNull = false;
            dc0.AutoIncrement = true;
            dc0.AutoIncrementStep = 1;
            dc0.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc0);

            DataColumn dc1 = new DataColumn("Name");
            dc1.DataType = typeof(string);
            dc1.MaxLength = 50;
            dc1.AllowDBNull = false;
            dt1.Columns.Add(dc1);

            DataColumn dc2 = new DataColumn("Description");
            dc2.DataType = typeof(string);
            dc2.MaxLength = 150;
            dc2.AllowDBNull = true;
            dt1.Columns.Add(dc2);

            dt1.PrimaryKey = new DataColumn[] { dc0 };

            DataRow dr1 = dt1.Rows.Add(new object[] { 1, "Fabio", "Team Leader" });
            DataRow dr2 = dt1.Rows.Add(new object[] { 2, "Fabio", "Developer" });
            DataRow dr3 = dt1.Rows.Add(new object[] { 3, "Fabio", "Tester" });

          
            // esegue il colone della tabella
            DataTable dt2 = dt1.Clone();

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dt2.ImportRow(dt1.Rows[i]);
            }

        }

Nessun commento: