peter piper

peter piper

  • NA
  • 22
  • 3.3k

reading in xml files from folder

May 25 2015 12:43 PM
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();
            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

Answers (1)