dataAdapter.update  & datatable

Posted on 2006-04-20
Last Modified: 2008-02-26
I have a dt that fill with data from a csv file.  I want to use dataAdapter.update to insert the rows into a database table.  The DataTable columns match the columns in my
database table.  Could someone show me how to do it.  

StreamReader re = File.OpenText("Experts.csv");
DataTable dt = new DataTable();
dt = CsvParser.Parse(re);
Question by:musclejack
    LVL 11

    Expert Comment

    maybe this works:
    adapter.Update(dt.Select(null, null, DataViewRowState.Added));

    Author Comment

    still not working.  
    LVL 96

    Expert Comment

    by:Bob Learned
    That usually means that you haven't added any rows to the DataTable, so there is nothing to insert.


    Author Comment

    can u give me a simple full example. thx
    LVL 11

    Accepted Solution

    this is an example i just created. hope you will get the idea, how it works for your problem.

    DataSet dataSet = new DataSet();
    DataTable dataTable = new DataTable("Names");
    dataTable.Columns.Add(new DataColumn("Idx", typeof(int)));
    dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
    dataTable.Columns.Add(new DataColumn("Fullname", typeof(string)));
    dataTable.Rows.Add(new object[] {0, "Name1", "Name11"});

    SqlConnection sqlConnection = new SqlConnection(@"Data Source=mymachine\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;Pooling=False");
    SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * from Names", sqlConnection);            
    SqlCommand sqlCommand = new SqlCommand("INSERT INTO Names (Idx, Name, Fullname) " +
                                                                          "VALUES (@Idx, @Name, @Fullname)", sqlConnection);

    // Add the parameters for the InsertCommand.
    sqlCommand.Parameters.Add("@Idx", SqlDbType.Int, 4, "Idx");
    sqlCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
    sqlCommand.Parameters.Add("@Fullname", SqlDbType.NVarChar, 50, "Fullname");
    dataAdapter.InsertCommand = sqlCommand;
    dataAdapter.Update(dataTable.Select(null, null, DataViewRowState.Added));


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now