【问题标题】:Import Data from Excel to Sql Server 2008 #2将数据从 Excel 导入 Sql Server 2008 #2
【发布时间】:2014-01-24 05:05:37
【问题描述】:
string path = string.Concat(Server.MapPath("~/TempFiles/"), FileUpload1.FileName);
//Save File as Temp then you can delete it if you want 
FileUpload1.SaveAs(path);


string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);

// Create Connection to Excel Workbook 
using (OleDbConnection connection =
             new OleDbConnection(excelConnectionString))
{
    OleDbCommand command = new OleDbCommand
            ("Select * FROM [Sheet1$]", connection);

    connection.Open();

    // Create DbDataReader to Data Worksheet 
    using (DbDataReader dr = command.ExecuteReader())
    {

        // SQL Server Connection String 
        string sqlConnectionString = @conn;

        // Bulk Copy to SQL Server 
        using (SqlBulkCopy bulkCopy =
                   new SqlBulkCopy(sqlConnectionString))
        {
            bulkCopy.DestinationTableName ="Table1";
            bulkCopy.WriteToServer(dr);
            Label1.Text = "The Client data has been exported successfully from Excel to SQL";
        }
    }
}

我正在尝试将数据从 excel 导入 SQL Server,它工作正常,直到我没有传递日期,但现在我想将日期传递给 SQL Server,它提供错误,因为数据类型不匹配。

任何人都有逻辑或请建议我该怎么做..

【问题讨论】:

    标签: c# sql sql-server excel oledb


    【解决方案1】:

    可能是 Excel 工作表中的列不是有效的日期格式。

    将其更改为日期类型。

    Select the Column in the Excel Sheet -> Right Click -> Format Cells ->
    Number Tab -> Select Date -> Choose your desired Type -> Ok 
    

    然后你尝试导入...

    【讨论】:

    • 我试过这个但没有用...你有没有试过这个??
    【解决方案2】:

    您从 excel 中读取的 DateTime 是 OLE 自动化日期,您必须在插入到 sql server 之前将其转换为 c# DateTime。当您从 excel 中读取时,日期将是双倍值。您可以使用DateTime.FromOADate 将双精度值转换为DateTime。你可以使用SqlBulkCopy.WriteToServer(DataTable table),这个方法可以让你传递数据表。您可以将 datatable 中的日期更改为 require 格式,并将其用于在 sql server 中保存批量数据。您可以将excel数据导入数据表,这个article会帮助您。

    DateTime dt = DateTime.FromOADate(double.Parse(stringVariableContainingDateTime));
    

    【讨论】:

    • 如何在批量导入数据时转换它。有什么可以让我将其导入数据表,然后导入 sql 服务器,以便我们可以检查数据表中的每个数据和转换它
    • 您好,因为我是 asp 新手,但我不知道,但我检查了它.. 我使用了 datareader,我正在执行 datareader 来插入我的值,如果我正在写入数据表,那么我该如何执行它数据表值插入我的数据??感谢您的回复
    • yeh thanx vl try to implementation if i gt vl let u know thanx
    【解决方案3】:

    它有效,我尝试将其转换为 datatble den 更改数据类型,然后插入

     string sqlConnectionString = @conn;
    command.CommandType = CommandType.Text;
                    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    DataSet objDataset1 = new DataSet();
    
                    objAdapter1.Fill(dt);
    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[0][5].ToString() != "")
                        {
                            DateTime dt1 = cf.texttodb(dt.Rows[0][5].ToString());
                            dt.Rows[i][5] = dt1;
                        }}
     using (SqlBulkCopy bulkCopy =
                                   new SqlBulkCopy(sqlConnectionString))
                    {
                        bulkCopy.DestinationTableName = "Tablename";
                        bulkCopy.WriteToServer(dt);
                        Label1.Text = "The Client data has been exported successfully from Excel to SQL";
                    }
    

    在此我创建了一个函数 txtdob,它将我的字符串转换为日期时间格式 谢谢 如果你觉得把它标记为答案,我试过了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      相关资源
      最近更新 更多