The format of Datetime is incorrect when excel data is stored in sqlserver.

Source: Internet
Author: User
First, let's talk about the general idea of my implementation: 1. Read excel Data and assign values to datatable. Then, the detailed description is displayed on the page: Some columns are saved to the database in the datetime format, dataColumndtColumnnewDataColumn (); dtColumn. dataTypeSystem. ty

First, let's talk about the general idea of my implementation: 1. Read excel Data and assign values to datatable. Then, the detailed description is displayed on the page: Some columns are saved to the database in the datetime format, dataColumn dtColumn = new DataColumn (); dtColumn. dataType = System. ty

First, let's talk about the general idea of my implementation:

<1>Read excel Data, assign values to able, and then display on the page

Details: Some columns are saved in the datetime format when stored in the database, and I also perform the following processing when assigning values to the able.

DataColumn dtColumn = new DataColumn ();

DtColumn. DataType = System. Type. GetType ("System. DateTime ");

<2>Save the datatable to sqlserver

2.1 using a stored procedure with parameters, I have implemented this method (a column inserted in one row can also be inserted in the datetime column, and can be viewed in sqlserver)

2.2 When SqlBulkCopy is used (to improve efficiency as much as possible), the following error occurs:

Implementation Details:

string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;            using(SqlConnection conn = new SqlConnection(connStr))            {                conn.Open();                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))                {                    bulkCopy.DestinationTableName = "dbo.*****";                    try                    {                        bulkCopy.WriteToServer(dt);                    }                    catch (Exception ex)                    {                        Response.Write(ex.Message);                    }                                    }            }

The following error is reported:

The given value of type String from the data source cannot be converted to type datetime of the specified target column. RunTime

Now we are stuck on sqlbulkcopy. Please advise. Thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.