简体   繁体   English

批量复制到多个表

[英]Bulk copy to multiple tables

my application is currently reading data from a csv file into my GUI and then displaying , at the moment the code looks like this : 我的应用程序当前正在将csv文件中的数据读取到GUI中,然后显示,此时代码如下所示:

try
        {
            //Browse for file
            OpenFileDialog ofd = new OpenFileDialog();
            //Only show .csv files
            ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv";
            DialogResult result = ofd.ShowDialog();

            if (result == DialogResult.OK)
            {
                SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=ionCalc;Integrated Security=True");
                string filepath = ofd.FileName;
                StreamReader sr = new StreamReader(filepath);
                string line = sr.ReadLine();
                string[] value = line.Split(',');
                DataTable dt = new DataTable();
                DataRow row;

                foreach (string dc in value)
                {
                    dt.Columns.Add(new DataColumn(dc));
                }

                while (!sr.EndOfStream)
                {
                    value = sr.ReadLine().Split(',');
                    if (value.Length == dt.Columns.Count)
                    {
                        row = dt.NewRow();
                        row.ItemArray = value;
                        dt.Rows.Add(row);
                    }
                }
                SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
                bc.DestinationTableName = "Inventory";
                bc.BatchSize = dt.Rows.Count;
                bc.ColumnMappings.Add("Name", "Name");
                bc.ColumnMappings.Add("IN", "IN");
                bc.ColumnMappings.Add("Fund", "Fund");
                bc.ColumnMappings.Add("Status", "Status");
                bc.ColumnMappings.Add("ShareCurrency", "ShareCurrency");
                bc.ColumnMappings.Add("PriceFrequency", "PriceFrequency");
                bc.ColumnMappings.Add("ClassCode", "ClassCode");
                bc.ColumnMappings.Add("Simulation", "Simulation");
                bc.ColumnMappings.Add("Hedged", "Hedged");
                bc.ColumnMappings.Add("FundCurrency", "FundCurrency");
                bc.ColumnMappings.Add("Type", "Type");
                con.Open();
                bc.WriteToServer(dt);
                bc.Close();
                connection.Close();

Which correctly throws all the data into the datagrid for the inventory. 正确地将所有数据扔到清单的数据网格中。 However I want to make sure that while the data is getting placed in the inventory table that the name and fund have their distinct names passed to their respective tables ...any ideas ? 但是,我想确保在将数据放入库存表时,名称和基金将其不同的名称传递给各自的表...有什么想法吗?

Apologies for the starting line , for some reason keeps being cut out 对于起跑线表示歉意,由于某些原因不断被淘汰

var names = dt.AsEnumerable().Select(o=>o.Field<string>("Name").Distinct());
var funds = dt.AsEnumerable().Select(o=>o.Field<string>("Fund").Distinct());

Once you have these 2 collections you can convert them to datatables as you see fit (Lots of suggestions about the site), then use more SqlBulkCopy to process them 一旦拥有了这两个集合,就可以将它们转换为合适的数据表(有关该站点的许多建议),然后使用更多的SqlBulkCopy来处理它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM