DataSet Kullanımı

Console üzerinden C# ile basit Dataset kullanımı:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Dataset
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet("MyDataSet");

            DataTable dt1 = new DataTable("Table1");
            DataTable dt2 = new DataTable("Table2");

            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);

            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "Id";
            dc1.DataType = typeof(int);
            dc1.AllowDBNull = false;

            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "FirstName";
            dc2.DataType = typeof(string);
            dc2.AllowDBNull = false;

            DataColumn dc3 = new DataColumn();
            dc3.ColumnName = "LastName";
            dc3.DataType = typeof(string);
            dc3.AllowDBNull = false;

            DataColumn dc4 = new DataColumn();
            dc4.ColumnName = "Occupation";
            dc4.DataType = typeof(string);
            dc4.AllowDBNull = false;

            DataColumn dc5 = new DataColumn();
            dc5.ColumnName = "ReportEndDate";
            dc5.DataType = typeof(DateTime);
            dc5.AllowDBNull = true;

            dt1.Columns.Add(dc1);
            dt1.Columns.Add(dc2);
            dt1.Columns.Add(dc3);
            dt1.Columns.Add(dc4);
            dt1.Columns.Add(dc5);

            DataRow dr = dt1.NewRow();

            dr["Id"] = 1;
            dr["FirstName"] = "Mert";
            dr["LastName"] = "Bayramoglu";
            dr["Occupation"] = "Yazılımcı";
            dr["ReportEndDate"] = DateTime.Today;

            dt1.Rows.Add(dr);

            DataRow dr2 = dt1.NewRow();
            dr2["Id"] = 2;
            dr2["FirstName"] = "Ali";
            dr2["LastName"] = "Veli";
            dr2["Occupation"] = "Gazozcu";
            dr2["ReportEndDate"] = DBNull.Value;

            dt1.Rows.Add(dr2);

            Console.WriteLine("DataTable'da Satır Sayısı:{0}", dt1.Rows.Count);

            foreach (DataRow row in dt1.Rows)
            {
                foreach (DataColumn col in dt1.Columns)
                {
                    Console.Write("\t{0}", row[col]);
                }
                Console.WriteLine();
            }
           
           
            Console.ReadKey();

        }
    }
}

Bir yanıt yazın