SqlBulkCopy不从Excel导入数据

我正在做一个简单的项目,从Excel导出数据到SQL Server与Visual Studio 12和C#。 我设法从Excel导入到数据集,但没有插入到我的数据库,虽然代码显示一个积极的消息框,我已经设置告诉我什么时候是好的。

这就是说,我的数据已成功导出到SQL,但是当我select“显示表数据”没有数据显示; 表是空的。 这是我的代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; using System.IO; using System.Data.OleDb; using System.Data.Common; using System.Data.SqlClient; namespace testoledb { public partial class Form1 : Form { DataSet OleDs = new DataSet(); OleDbDataAdapter OleAdapter = new OleDbDataAdapter(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void upload_excl_Click(object sender, EventArgs e) { string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); path = Path.Combine(path, "AGENDA.xlsx"); string path2 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); path2 = Path.Combine(path2, "Database1.mdf"); string OleConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+@";Extended Properties='Excel 12.0 Macro;MDR=Yes;ImportMixedTypes=Text;TypeGuessRows=0'"; //,HDR=Yes;IMEX=1"""; OleDbConnection OleConn = new OleDbConnection(OleConnectionString); string OleStrCmd = "select * from [Feuil1$A1:I330]"; OleDbCommand OleCmd = new OleDbCommand(OleStrCmd, OleConn); try { OleConn.Open(); OleDs.Clear(); OleAdapter.SelectCommand = OleCmd; OleAdapter.Fill(OleDs); dataGridView1.DataSource = OleDs.Tables[0]; //***************************************charger la base******************************************************************************* using (DbDataReader dr = OleCmd.ExecuteReader()) { // SQL Server Connection String string sqlConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + path2 + ";Integrated Security=True"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "[dbo].[Table]"; bulkCopy.WriteToServer(dr); MessageBox.Show("Data Exoprted To Sql Server Succefully"); } } //*********************************************************************************************** } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { OleConn.Close(); } } } } 

要告诉你是否真的导入数据,你可以这样做:

 OleDBCommand commandRowCount = new SqlCommand( "SELECT COUNT(*) FROM " + "dbo.Table;", OleConn); long countStart = System.Convert.ToInt32( commandRowCount.ExecuteScalar()); 

这会给你开始的行号,可能是0.然后在导入数据之后,这个:

 // Perform a final count on the destination // table to see how many rows were added. long countEnd = System.Convert.ToInt32( commandRowCount.ExecuteScalar()); 

这会给你结尾的行数。 然后可以通过计算countEnd - countStartcountEnd - countStart导入了多less行。 检查OleDs.Tables[0].Rows.Count的值也是有帮助的,以确保DataSet有行。

有关SqlBulkCopy.WriteToServer更多信息: http : //msdn.microsoft.com/zh-cn/library/434atets( SqlBulkCopy.WriteToServer .aspx

编辑:

我回头看看你的原始代码,看起来你不需要使用DbDataReader 。 由于您已经将数据加载到您的数据DataSet ,因此有一个以DataTable为参数的SqlBulkCopy.WriteToServer版本,所以您可以试试这个:

  // SQL Server Connection String string sqlConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + path2 + ";Integrated Security=True"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "[dbo].[Table]"; bulkCopy.WriteToServer(OleDs.Tables[0]); MessageBox.Show("Data Exoprted To Sql Server Succefully"); } 

注意,我没有在DbDataReader包含外部的using块。 试一下,看看是否适合你。