0

我已经多次查看我的代码,希望有人可以帮助我看到我没有看到的东西。我正在尝试将数据从 Excel 提取到 VS2010 中具有多列的 SQL 表中,但是当我尝试上传数据时,出现错误“无法找到第 8 列”。我认为第 8 列和其他任何列都没有问题。还有谁?谢谢!

protected void Button1_Click(object sender, EventArgs e)
{
    //make local copy
    string sheetname = "Sheet1";
    string path = Server.MapPath("~/Import.xls");
    //connect to local Excel
    try
    {

        FileUpload.SaveAs(path);
        System.Data.OleDb.OleDbConnection MyConnection;
        System.Data.DataSet DtSet;
        System.Data.OleDb.OleDbDataAdapter MyCommand;  //check sheet name
        MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetname + "$]", MyConnection);
        MyCommand.TableMappings.Add("Table", "TestTable");
        DtSet = new System.Data.DataSet();
        MyCommand.Fill(DtSet);
        SqlConnection curConnection = new SqlConnection(@"Data Source=1tc-Techcomm2;Initial Catalog=EventManagement;Integrated Security=True");
        curConnection.Open();
        SqlCommand curCommand;
        SqlParameter param;
        string str;

        for (int i = 0; i < DtSet.Tables[0].Rows.Count; i++)
        {
            if (DtSet.Tables[0].Rows[i][1] == DBNull.Value)
                continue;
            curCommand = curConnection.CreateCommand();
            curCommand.CommandText = @"Insert into TestSP (SessionHead_Title, SessionHead_Type, SessionHead_Description, SessionHead_Confirmed, SessionHead_Presenter, SessionHead_Champion, SessionHead_Objective, SessionHead_Notes, SessionHead_Schedule, SessionHead_Equipment, SessionHead_Hardware, SessionHead_CDA) Values (@a,@b,@c,@d,@e,@f,@g,@h,@i,@j,@k,@l,@m)";
            for (int j = 0; j < 13; j++)
            {
                param = new SqlParameter();
                str = "@";
                str += (char)('a' + j);
                param.ParameterName = str;
                param.SqlDbType = SqlDbType.VarChar;
                param.Value = DtSet.Tables[0].Rows[i][j];//This is where it errors out at after 8 times through
                curCommand.Parameters.Add(param);

            }
            Label1.Text = "THE EXCEL DATE HAS SUCCESSFULLY BEEN SENT TO THE DATABASE"; 

            int Event_Id = curCommand.ExecuteNonQuery();

        }
        MyConnection.Close();
        curConnection.Close();
    }
    catch (Exception ex)
    {
        //Response.Write(ex.ToString());
        Label1.Text = ex.Message; 

    }
}
4

3 回答 3

1

我认为问题在于电子表格的列标题与表标题不匹配。我在某处读到 SQL Server 表中的列名在 Excel 文件中必须相同。我想这解决了它...感谢您的帮助!

于 2012-04-11T18:48:58.880 回答
0

如果您知道 SQL Server 上表的结构,请尝试使用 SqlBulkCopy。映射您的列并将其写入服务器。您的数据集中已经有了数据。

SqlBulkCopy _bc = new SqlBulkCopy(curConnection);

_bc.DestinationTableName = "TestSP";

_bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping("DataTableColumnName1", "SQLServerColumnName1");
_bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping("DataTableColumnName2", "SQLServerColumnName2");
_bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping("DataTableColumnName3", "SQLServerColumnName3");
_bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping("DataTableColumnName4", "SQLServerColumnName4");

_bc.WriteToServer(DtSet.Tables[0]);

如果您不了解结构,您仍然可以使用此过程,但您必须发挥创造力。我必须这样做,这很有趣。

于 2012-04-05T22:46:53.320 回答
0

user3611272解决方案帮助了我:

...我尝试附加一些随机表并转到附加弹出窗口底部的“高级选项”你会看到一些“存储列映射”和其中的一些映射,现在删除所有 相关映射然后点击“接受”现在你可以附加表格

于 2020-06-25T18:37:55.993 回答