我的应用程序当前正在将 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();
哪个正确地将所有数据放入库存的数据网格中。但是,我想确保在将数据放入库存表中时,名称和基金将其不同的名称传递到各自的表中......有什么想法吗?
为起跑线道歉,由于某种原因一直被淘汰