dataAdapter.update & datatable

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);
musclejackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vo1dCommented:
maybe this works:
adapter.Update(dt.Select(null, null, DataViewRowState.Added));
0
musclejackAuthor Commented:
still not working.  
0
Bob LearnedCommented:
That usually means that you haven't added any rows to the DataTable, so there is nothing to insert.

Bob
0
musclejackAuthor Commented:
can u give me a simple full example. thx
0
vo1dCommented:
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));


vo1d
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.