sql >> Database teknologi >  >> RDS >> Mysql

FEJL:Yderligere oplysninger:Ekstern tabel er ikke i det forventede format

Udover undtagelsen skal du sørge for altid at lukke forbindelser. Under alle omstændigheder kan følgende løse dine problemer:

String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + TextBox1.Text + ";" +
    "Extended Properties=Excel 8.0;";
using (OleDbConnection excel_con = new OleDbConnection(connectionString ))
{
    excel_con.Open();

    DataTable dtExcelData = new DataTable();

    //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
    dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
            new DataColumn("Name", typeof(string)),
            new DataColumn("Password",typeof(string)) });

    using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excel_con))
    {
        oda.Fill(dtExcelData);
    }
    excel_con.Close();

    string consString = MyConString;
    using (SqlConnection con = new SqlConnection(consString))
    {
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
        {
            //Set the database table name
            sqlBulkCopy.DestinationTableName = "dbo.TableName";
            sqlBulkCopy.ColumnMappings.Add("Id", "Id");
            sqlBulkCopy.ColumnMappings.Add("Password", "Password");
            sqlBulkCopy.ColumnMappings.Add("Name", "Name");
            con.Open();
            sqlBulkCopy.WriteToServer(dtExcelData);
            con.Close();
            MessageBox.Show("Upload Successfull!");
        }
    }
}

Du skal sikre dig, at kolonnenavnene matcher, og at dit tabelnavn også er korrekt.

Dette er baseret på et eksempel fundet her .




  1. Vælg, at alle poster ikke opfylder visse betingelser i en samlet tabel

  2. Kombiner flere unikke MySQL-tabeller og bestil efter én kolonne

  3. Hvordan forbinder jeg Java til Mysql?

  4. Base58 Encoder funktion i PostgreSQL