Three solutions to import Excel file data to SqlServer Database

Source: Internet
Author: User

Solution 1: Use OleDB to obtain data from an Excel file and then use DataSet to transfer data to SQL Server. This method has the advantages of being flexible, you can perform operations on cells in an Excel table as needed.

 

  1. OpenFileDialog = new OpenFileDialog ();
  2. OpenFileDialog. Filter = "Excel files (*. xls) | *. xls ";
  3.  
  4. If (openFileDialog. ShowDialog () = DialogResult. OK)
  5. {
  6. FileInfo fileInfo = new FileInfo (openFileDialog. FileName );
  7. String filePath = fileInfo. FullName;
  8. String connExcel = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" + filePath + "; Extended Properties = Excel 8.0 ";
  9. Try
  10. {
  11. OleDbConnection oleDbConnection = new OleDbConnection (connExcel );
  12. OleDbConnection. Open ();
  13. // Obtain an excel table
  14. DataTable dataTable = oleDbConnection. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, null );
  15.  
  16. // Obtain the sheet name, where [0] [1]... [N]: The form Element arranged by name
  17. String tableName = dataTable. Rows [0] [2]. ToString (). Trim ();
  18. TableName = "[" + tableName. Replace ("", "") + "]";
  19.  
  20. // Obtain data from an Excel file using SQL statements
  21. // String query = "SELECT classDate, classpdate, classTeacher, classTitle, classID FROM" + tableName;
  22. String query = "SELECT date, course class city, Lecturer, course name, duration FROM" + tableName;
  23. DataSet = new DataSet ();
  24.  
  25. // OleDbCommand oleCommand = new OleDbCommand (query, oleDbConnection );
  26. // OleDbDataAdapter oleAdapter = new OleDbDataAdapter (oleCommand );
  27. OleDbDataAdapter oleAdapter = new OleDbDataAdapter (query, connExcel );
  28. OleAdapter. Fill (dataSet, "gch_Class_Info ");
  29. // Insert records into the SQL Server data table after obtaining data from the excel file
  30. DataTable ableable1 = new DataTable ();
  31. SqlDataAdapter sqlDA1 = new SqlDataAdapter (@ "SELECT classID, classDate,
  32. ClassPlace, classTeacher, classTitle, durativeDate FROM gch_Class_Info ", sqlConnection1 );
  33. // SqlCommandBuilder sqlCB1 = new SqlCommandBuilder (sqlDA1 );
  34. SqlDA1.Fill (dataTable1 );
  35.  
  36. Foreach (DataRow dataRow in dataSet. Tables ["gch_Class_Info"]. Rows)
  37. {
  38. DataRow dataRow1 = ableable1.newrow ();
  39. DataRow1 ["classDate"] = dataRow ["date"];
  40. DataRow1 ["classPlace"] = dataRow ["course class city"];
  41. DataRow1 ["classTeacher"] = dataRow [""];
  42. & Nb

Related Article

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.