我正在寫一個簡單的程序來導入Excel表到我的數據庫工作,但我遇到的一個錯誤:導入Excel工作表與C#的SQL Server
找不到可安裝ISAM
我不確定這是什麼意思,經過幾個小時的搜索與這麼多不同的主題,我已經轉向SO。有很多關於Jet和ACE的討論,我不確定它們之間有什麼區別,但這裏是簡要的說明:我有一個叫做test或者test1的excel文件,我只想導入文件中的第一張。這裏是我的源代碼到目前爲止:
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 System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string filePath = null;
public Form1()
{
InitializeComponent();
}
//Method to check database connection
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn;
connetionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=SSPI;";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
//Method to select a file
private void button2_Click(object sender, EventArgs e)
{
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Zach/Documents/test1.xls;Extended Properties=Excel 12.0,HDR=Yes;IMEX=1";
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select * FROM [Sheet1$]", connection);
connection.Open(); //HERE IS WHERE THE ERROR IS
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "Table";
bulkCopy.WriteToServer(dr);
MessageBox.Show("Data Exoprted To Sql Server Succefully");
}
}
}
}
}
}
我在接近這個在正確的莊園嗎?
嘗試分號:'Excel的12.0; HDR =是; IMEX = 1' – shibormot 2013-03-10 21:54:01
同樣的錯誤在這裏,我完全在這裏輸了。 – 2013-03-10 22:04:03
'......「;擴展屬性= \」Excel 12.0; HDR =是; IMEX = 1 \「;」'? – shibormot 2013-03-10 22:10:10