Click here to Skip to main content
14,868,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following code writes the single xml file to an sql table.
Can you please show me how to do a foreach so I can read in multiple files from a single directory

private void button1_Click(object sender, EventArgs e)
    string cs = @"Server=SQLEX\WS19;Database=XML;User Id=sa;Password=;";
    using (SqlConnection con = new SqlConnection(cs))
            DataSet ds = new DataSet();
            DataTable dt1 = ds.Tables["cats"];
            using (SqlBulkCopy bc = new SqlBulkCopy(con))
                bc.DestinationTableName = "Category";
                bc.ColumnMappings.Add("catClass", "types");
                bc.ColumnMappings.Add("catDefinition", "description");
                bc.ColumnMappings.Add("cattValue", "amount");

Thank you for any assistance or guidance
[no name] 25-May-15 12:25pm
Reading the documentation often helps
callasda 25-May-15 12:27pm
Already saw these but of no help
[no name] 25-May-15 12:49pm
Probably because you did not actually read about it and write any code. There is no code at all in your example where you got any list of files or tried using a foreach. Do you expect people to write your code for you?
callasda 25-May-15 12:55pm
any chance you could ignore my posts in future ?
Would be the best thing you could ever do
[no name] 25-May-15 13:49pm
Ignore someone that can't be bothered to at least try it themselves before asking someone to do it for them? Sure, not a problem.

1 solution

Take a look here: Directory.GetFiles Method (String)[^] - on the bottom of page, you'll find sample code (ProcessDirectory).
callasda 25-May-15 15:40pm
Thanks for your link.
I have read that Directory.EnumerateFiles is more efficient than Directory.GetFiles. Is there any truth in this ?
Maciej Los 25-May-15 17:04pm
The difference is here: The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.. Source: Directory.EnumerateFiles Method (String, String, SearchOption) - Remarks section.
Maciej Los 25-May-15 17:05pm
Can you accept my answer - formally - to remove your question from unanswered list?
callasda 25-May-15 17:24pm
Yes, Will do so right now.
Maciej Los 25-May-15 17:24pm
Thank you ;)
callasda 25-May-15 17:25pm
First time I've been on this forum. Clicked accepted but now showing Solution 1 (reject solution). Did I do something wrong ?
Maciej Los 25-May-15 17:31pm
No. Everything is OK. You can reject solution, if it wasn't helpful.
callasda 25-May-15 18:22pm
Are you an expert on xml bulkcopy ?. Am I allowed to ask you a couple of questions in this regards ?
Maciej Los 26-May-15 2:04am
CP community is full of masters ;) Feel free to ask another question...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900