ADO.NET DataTable Class | ADO.NET DataTable C# | DataTable C# Tutorial | ADO.NET

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data;


namespace ADO_Net_DataTable

{

    internal class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DataTable employees = new DataTable("employees");


                DataColumn id = new DataColumn("id");

                id.Caption = "Emp_Id";

                id.DataType = typeof(int);  //First way

                //id.DataType = System.Type.GetType("System.Int32");//Second way

                id.AllowDBNull = false; //By default, value of AllowDBNull is true

                id.AutoIncrement = true;

                id.AutoIncrementSeed = 1;

                id.AutoIncrementStep = 1;

                employees.Columns.Add(id);


                DataColumn name = new DataColumn("name");

                name.Caption = "Emp_Name";

                name.DataType = typeof(string);

                name.AllowDBNull = false;

                name.MaxLength = 50;

                name.DefaultValue = "Anonymous";

                name.Unique = true;

                employees.Columns.Add(name);


                DataColumn gender = new DataColumn("gender");

                gender.Caption = "Emp_Gender";

                gender.DataType = typeof(string);

                gender.AllowDBNull = false;

                gender.MaxLength = 20;

                employees.Columns.Add(gender);


                DataRow row1 = employees.NewRow();

                //row1["id"] = 1;

                row1["name"] = "Tarun";

                row1["gender"] = "Male";

                employees.Rows.Add(row1);


                employees.Rows.Add(null,"Yagnik","Male");

                employees.Rows.Add(null,"Pravin","Male");

                employees.Rows.Add(null,"Rekha","Female");


                employees.PrimaryKey = new DataColumn[] { id };


                foreach(DataRow row in employees.Rows)

                {

                    Console.WriteLine(row["id"] + " " + row["name"] + " " + row["gender"]);

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

            Console.ReadLine();

        }

    }

}

Above File is ADO_Net_DataTable\Program.cs











<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
<connectionStrings>
<add name="dbcs" connectionString="Data Source=.\SQLEXPRESS;initial Catalog=ado_db;Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Above File is ADO_Net_DataTable\App.config




Comments

Popular posts from this blog

Create a User Registration Form in ASP.NET using SQL Server, Visual Studio 2022 & Bootstrap

Create a Simple Login Form in ASP.NET using Visual Studio 2022

SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar