【发布时间】:2017-03-29 10:31:46
【问题描述】:
我收到一个错误
给定的 ColumnMapping 与源或目标中的任何列都不匹配
使用以下代码
private void button1_Click_1(object sender, EventArgs e)
{
of1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
if ((of1.ShowDialog()) == System.Windows.Forms.DialogResult.OK)
{
imagepath = of1.FileName; //file path
textBox1.Text = imagepath.ToString();
}
}
private void loadbtn_Click(object sender, EventArgs e)
{
string ssqltable = comboBox1.GetItemText(comboBox1.SelectedItem);
string myexceldataquery = "select * from ["+ ssqltable + "$]";
try
{
OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+imagepath+";Extended Properties='Excel 12.0 Xml; HDR=YES;IMEX=1;';");
string ssqlconnectionstring = "Data Source=.;Initial Catalog=Bioxcell;Integrated Security=true";
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oconn);
oconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
bulkcopy.DestinationTableName = ssqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
oconn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
/* DisplayingData DD = new DisplayingData();
DD.Show();*/
}
我认为 SQL Server 区分大小写,我复制了相同的列名但同样的错误..
有没有办法解决这个问题?
【问题讨论】:
标签: c# sql-server excel