如何解决OLE DB提供程序Microsoft ACE OLEDB 12.0链接服务器(null)错误?

时间:2014-06-13 13:52:54

标签: c# asp.net connection-string aceoledb

上传Excel文件以将列插入数据库时​​,我收到以下错误。

  

用于链接服务器的OLE DB提供程序Microsoft ACE OLEDB 12.0(null)

首先,我选择一个Excel文件,然后我将表格的名称放到我的Excel文件的列中,最后,当我点击按钮保存时,我得到了该错误。

我的web.config

<appSettings>
    <add key="FolderPath" value="~/Files/" />
</appSettings>
<connectionStrings>
    <!--Ce qui va nous permettre d'importer des fichiers excel aec le format 2003 et 2007 dans SQL Server-->
    <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
            Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
    <add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
    <!--Ce qui va nous permettre de se connecter à la base de données-->
    <add name="connexionBase" providerName="System.Data.SqlClient" connectionString="server=localhost;database=projetDGCS;uid=sa;pwd=dgcs9876" />
  </connectionStrings>
  <system.web>

我的代码背后:

protected void btnSave_Click(object sender, EventArgs e)
{
    string FileName = lblFileName.Text;
    string Extension = Path.GetExtension(FileName);
    string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);
    string CommandText = "";
    switch (Extension)
    {
        case ".xls": //Excel 97-03
            CommandText = "spx_ImportFromExcel03";
            break;
        case ".xlsx": //Excel 07
            CommandText = "spx_ImportFromExcel07";
            break;
    }
    //Read Excel Sheet using Stored Procedure
    //And import the data into Database Table
    String strConnString = ConfigurationManager.ConnectionStrings["connexionBase"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = CommandText;
    cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value =
                       ddlSheets.SelectedItem.Text;
    cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value =
                       FolderPath + FileName;
    cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value =
                       rbHDR.SelectedItem.Text;
    cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value =
                       txtTable.Text;
    cmd.Connection = con;
    try
    {
        con.Open();
        object count = cmd.ExecuteNonQuery();
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = count.ToString() + " records inserted.";
    }
    catch (Exception ex)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = ex.Message;
    }
    finally
    {
        con.Close();
        con.Dispose();
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
}

1 个答案:

答案 0 :(得分:0)

System.Data.OleDb.OleDbConnection myconnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" 
      + filepathofexcel `enter code here`+ "';Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'");

myconnection.Open();
string st = "select * from [" + SheetName + "]";
System.Data.OleDb.OleDbDataAdapter mycmd = new System.Data.OleDb.OleDbDataAdapter(st, myconnection);

mycmd.TableMappings.Add("Table", "TestTable");
mycmd.Fill(ds);