Click here to Skip to main content
15,881,882 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

C#
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();
            ds.ReadXml(@"E:\XML\product.xml");
            DataTable dt1 = ds.Tables["cats"];
            con.Open();
            using (SqlBulkCopy bc = new SqlBulkCopy(con))
            {
                bc.DestinationTableName = "Category";
                bc.ColumnMappings.Add("catClass", "types");
                bc.ColumnMappings.Add("catDefinition", "description");
                bc.ColumnMappings.Add("cattValue", "amount");
                bc.WriteToServer(dt1);
            }               
    }
}


Thank you for any assistance or guidance
Posted
Comments
[no name] 25-May-15 12:25pm    
Reading the documentation often helps
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/vstudio/ttw7t8t6(v=vs.110).aspx
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).
 
Share this answer
 
Comments
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 ;)

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