Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


dataAdapter.update  & datatable

Posted on 2006-04-20
Medium Priority
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
  • 2
  • 2
LVL 11

Expert Comment

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

Author Comment

ID: 16501505
still not working.  
LVL 96

Expert Comment

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


Author Comment

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

Accepted Solution

vo1d earned 2000 total points
ID: 16511749
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

577 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