Showing posts with label datatable. Show all posts
Showing posts with label datatable. Show all posts

Oct 18, 2013

Difference between dataset and data table

DataSet and DataTable are the key components in ADO.NET programming. This mean that DataTable represents an in memory representation of the database. We can load a single Table from the database into a DataTable and manipulate the data in memory. DataTable can be used as a DataSource and used it if we are going to fetch data in a single database table only. While DataSet on the other hand can define DataRelations - which define the relationship between DataTables, much like a foreign key relationship can be set up between tables in a database. 

Please find the few core differences


Jul 19, 2012

You can make a new DataTable, then import rows of two tables into new table.


DataTable t = new DataTable();
t.Columns.Add("Product", typeof(string));
t.Columns.Add("Value", typeof(int));

foreach(DataTable table in SampleDS.Tables)
{
if(table != null && table.Rows.Count > 0)
{
for(int i = 0; i < table.Rows.Count; i ++)
t.ImportRow(table.Rows[i]);
}
}

Jul 16, 2012

Check for the existence of a specific table in a dataset in c#.net

if ( dataSet.Tables["MyTable"] == null )
throw new exception("Table no present");

Jun 24, 2011

C# DataTable Examples


You need to store data that was read from a database such as SQL Server or generated in memory from user input. DataTable is ideal for this purpose, as you can take objects from memory and display the results in controls such as DataGridView in Windows Forms. Through the descriptions here, we examine the DataTable type in the C# language.
DataTable is found in the System.Data namespace.
It stores data in memory from databases or user input. It helps with using DataGridView and SQL Server databases.

Example 1

First, the DataTable type is probably the most convenient and powerful way to store data in memory. You may have fetched this data from a database, or you may have generated it dynamically. In this example, we get a DataTable with four columns of type int, string and DateTime. This DataTable could then be persisted or displayed.
Program that uses DataTable [C#]

using System;
using System.Data;

class Program
{
    static void Main()
    {
 //
 // Get the DataTable.
 //
 DataTable table = GetTable();
 //
 // Use DataTable here with SQL, etc.
 //
    }

    /// <summary>
    /// This example method generates a DataTable.
    /// </summary>
    static DataTable GetTable()
    {
 //
 // Here we create a DataTable with four columns.
 //
 DataTable table = new DataTable();
 table.Columns.Add("Dosage", typeof(int));
 table.Columns.Add("Drug", typeof(string));
 table.Columns.Add("Patient", typeof(string));
 table.Columns.Add("Date", typeof(DateTime));

 //
 // Here we add five DataRows.
 //
 table.Rows.Add(25, "Indocin", "David", DateTime.Now);
 table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
 table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
 table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
 table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
 return table;
    }
}
Note on GetTable method. This method simply instantiates a new DataTable reference, adds four column collections to it, and then adds five drug and patient records. The next step to using this code could be to assign the DataSource to a Windows Forms control.

Exporting DataTable to CSV File Format in C#.net


protected void Button1_Click(object sender, EventArgs e)
    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlConnection conn = new SqlConnection(strConn);

        SqlDataAdapter da = new SqlDataAdapter("select * from employees", conn);

        DataSet ds = new DataSet();

        da.Fill(ds, "Emp");

        GridView1.DataSource = ds.Tables["Emp"].DefaultView;

        GridView1.DataBind();

        DataTable dt = ds.Tables["Emp"];

        CreateCSVFile(dt, "c:\\csvData.csv");

    }





    public void CreateCSVFile(DataTable dt, string strFilePath)

    {

        #region Export Grid to CSV



        // Create the CSV file to which grid data will be exported.

        StreamWriter sw = new StreamWriter(strFilePath, false);

        // First we will write the headers.

        //DataTable dt = m_dsProducts.Tables[0];

        int iColCount = dt.Columns.Count;

        for (int i = 0; i < iColCount; i++)

        {

            sw.Write(dt.Columns[i]);

            if (i < iColCount - 1)

            {

                sw.Write(",");

            }

        }

        sw.Write(sw.NewLine);

        // Now write all the rows.

        foreach (DataRow dr in dt.Rows)

        {

            for (int i = 0; i < iColCount; i++)

            {

                if (!Convert.IsDBNull(dr[i]))

                {

                    sw.Write(dr[i].ToString());

                }

                if (i < iColCount - 1)

                {

                    sw.Write(",");

                }

            }

            sw.Write(sw.NewLine);

        }

        sw.Close();



        #endregion

    }

Apr 9, 2010

Exporting DataTable to CSV File Format

    protected void Button1_Click(object sender, EventArgs e)
    {

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

        SqlConnection conn = new SqlConnection(strConn);

        SqlDataAdapter da = new SqlDataAdapter("select * from employees", conn);

        DataSet ds = new DataSet();

        da.Fill(ds, "Emp");

        GridView1.DataSource = ds.Tables["Emp"].DefaultView;

        GridView1.DataBind();

        DataTable dt = ds.Tables["Emp"];

        CreateCSVFile(dt, "c:\\csvData.csv");

    }

 

 

    public void CreateCSVFile(DataTable dt, string strFilePath)

    {

        #region Export Grid to CSV

 

        // Create the CSV file to which grid data will be exported.

        StreamWriter sw = new StreamWriter(strFilePath, false);

        // First we will write the headers.

        //DataTable dt = m_dsProducts.Tables[0];

        int iColCount = dt.Columns.Count;

        for (int i = 0; i < iColCount; i++)

        {

            sw.Write(dt.Columns[i]);

            if (i < iColCount - 1)

            {

                sw.Write(",");

            }

        }

        sw.Write(sw.NewLine);

        // Now write all the rows.

        foreach (DataRow dr in dt.Rows)

        {

            for (int i = 0; i < iColCount; i++)

            {

                if (!Convert.IsDBNull(dr[i]))

                {

                    sw.Write(dr[i].ToString());

                }

                if (i < iColCount - 1)

                {

                    sw.Write(",");

                }

            }

            sw.Write(sw.NewLine);

        }

        sw.Close();

 

        #endregion

    }