简体   繁体   English

从Excel导入数据到Sql Server 2008#2

[英]Import Data from Excel to Sql Server 2008 #2

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";
        }
    }
}

I am trying to import data from excel to SQL Server, it works fine till I am not passing date but now I want to pass the date to SQL Server it provides error as datatype not matches. 我正在尝试将数据从excel导入到SQL Server,在我不传递日期之前,它可以正常工作,但是现在我想将日期传递到SQL Server,因为数据类型不匹配,它提供了错误。

Anyone has logic or please suggest me what can I do to .. 任何人都有逻辑,或者请建议我该怎么做..

The DateTime you read from excel is OLE Automation date and you have to convert it to c# DateTime before you insert in to sql server. 您从excel中读取的DateTime是OLE自动化日期,您必须将其转换为c# DateTime才能插入到SQL Server中。 It would be double value for date when you read from excel. 当您从excel中读取时,它将是日期的双倍值。 You can use DateTime.FromOADate to convert the double value to DateTime . 您可以使用DateTime.FromOADate将double值转换为DateTime You can use SqlBulkCopy.WriteToServer(DataTable table) , this method allows you to pass the datatable. 您可以使用SqlBulkCopy.WriteToServer(DataTable table) ,此方法允许您传递数据表 You can change the date in datatable in require format and use it to save bulk data in sql server. 您可以按要求格式更改数据表中的日期,并使用它在SQL Server中保存批量数据。 You can import excel data to datatable, this article will help you. 您可以将excel数据导入数据表, 本文将为您提供帮助。

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

Might be the column in the Excel Sheet is not in a valid date format. 可能是Excel工作表中的列的日期格式无效。

Change it to Date Type. 将其更改为日期类型。

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

Then you try to Import... 然后,您尝试导入...

It works i tried to convert it in datatble den change datatype and then insert 我尝试将其转换为datatble den change datatype然后插入

 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";
                }

in this i had created a function txtdob which converts my string to datetime format Thank you i tried it workes if u feel so mark it as answer 在此我创建了一个函数txtdob,将我的字符串转换为日期时间格式,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM