//获取全部数据
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
运行到这里的时候出现 .Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC)。请安装 Microsoft Data Access Components(MDAC) 2.6 或更高版本。我电脑的
HKEY_LOCAL_MACHINE\Software\Microsoft\DataAccess\FullInstallVer 
值是 2.81.1132.0
Excel 2003   +  sqlserver2005出现这种情况怎么解决?我重新下了MDAC2.7 和MDAC2.8都没用,还是出现这样的错误

解决方案 »

  1.   

    From your code, I can't see where you are trying to import data into SQL Server. You are simply opening an OLEDB connection to Excel from your application rather than via SQL Server.What is the value of excelFile variable?
      

  2.   

    如果实在不行,就先从access导成文本,然后再把文本的数据导入sql server 2005.
      

  3.   


    有链接SQL的,我这里只是贴出得到EXCEL数据就发生错误的部分, DataSet ds = new DataSet();
                try
                {
                    //获取全部数据
                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = string.Format("select * from [{0}$]", dbName);
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds, dbName);                //如果目标表不存在则创建
                    string strSql = string.Format("if object_id('{0}') is null create table {0}(", dbName);
                    foreach (System.Data.DataColumn c in ds.Tables[0].Columns)
                    {
                        strSql += string.Format("[{0}] varchar(255),", c.ColumnName);
                    }
                    strSql = strSql + ")";                using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
                    {
                        sqlconn.Open();
                        System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
                        command.CommandText = strSql;
                        command.ExecuteNonQuery();
                        sqlconn.Close();
                    }
                    //用bcp导入数据
                    using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
                    {
                        bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
                        bcp.BatchSize = 100;//每次传输的行数
                        bcp.NotifyAfter = 100;//进度提示的行数
                        bcp.DestinationTableName = dbName;//目标表
                        bcp.WriteToServer(ds.Tables[0]);
                    }
      

  4.   

    Ok, then it's not a SQL Server problem. It is definitely a problem with connection from .Net to Excel via ADO.Net OLEDB connection.Change the code that creates the connection string the following and let me know if it works:
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
      

  5.   


    还是不可以。依然异常
    .Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC)。请安装 Microsoft Data Access Components(MDAC) 2.6 或更高版本。
      

  6.   

    Ok, back to my original question: what is the value of excelFile you are using to create the connection string?
      

  7.   

    我真是笨。数据库里有个导入导出,可以直接从EXCEL导入数据的,,不过要2003版 的