-3

I have a table in my Database to store Holidays. The Columns:

Holiday, Day, Date

I want to copy all the rows from this table into a DataTable in my C# application. Iv created the DataTable with the same Columns.

DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] {
            new DataColumn("Holiday", typeof(string)),
            new DataColumn("Day", typeof(string)),
            new DataColumn("Date", typeof(DateTime))
        }); 

How can I populate this DataTable with all of the rows from my SQL Server database?

Reeggiie
  • 782
  • 7
  • 16
  • 36

1 Answers1

0

You can just use a Data Adapter to fill the datatable.

 DataTable dt = new DataTable();
 using (SqlConnection sqlConn = new SqlConnection(connectionString))
 {
     using (SqlCommand cmd = new SqlCommand("SELECT * FROM Holidays", sqlConn))
     {
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
     }
 }
Jonathan Carroll
  • 880
  • 1
  • 5
  • 20