如何在SQL中导出巨大的Excel数据时解决“来自外部数据库驱动程序的意外错误”(后跟中文字符)

时间:2015-10-26 05:34:48

标签: sql excel import odbc oledb

当我尝试将数据从excel文件导入SQL数据库时,我收到的错误如下:

  

外部数据库驱动程序出现意外错误(霰呫潈ూŐీL)。

我的代码:

private void Import(string fileFullPath, string fileName, StreamWriter writer)
{
     string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath +
    ";Extended Properties=" + (char)34 + "Excel 12.0;HDR=YES;" + (char)34;

    using (OleDbConnection cn = new OleDbConnection(ConnectionString))
    {
        cn.Open();
        DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (dbSchema == null || dbSchema.Rows.Count < 1)
        {
            writer.WriteLine("Error: Could not determine the name.");
            throw new Exception("Error: Could not determine the name.");
        }

        if (dbSchema.Rows.Count > 0)
        {

            Hashtable objHashTableOptionPool = new Hashtable();

            foreach (DataRow objDataRow in dbSchema.Rows)
            {
                if (objDataRow["TABLE_NAME"].ToString() != "Participants$")
                {
                    string name = objDataRow["TABLE_NAME"].ToString();

                    string checkname = string.Empty;
                    if (name.Substring((name.Length - 1), 1) == "'")
                    {
                        checkname = name.Substring(0, (name.Length - 1));
                    }
                    else
                    {
                        checkname = name;
                    }
                    string Rname = name.Substring((checkname.Length - 1), 1);
                    if (Rname != "$")
                    {
                        continue;
                    }

                    OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + name + "]", cn);
                    DataTable dt = new DataTable(name);
                    da.Fill(dt); //getting error here

                }
            }

        }
    }
}

使用数据适配器填充数据表时出错。

0 个答案:

没有答案