ASP. NET data import implementation Excel to MSSQL

Source: Internet
Author: User

What is the process of importing ASP. NET data to Excel to MSSQL? What do you need to pay attention?

When creating a website project, you may encounter ASP. NET data is required to be imported to the SQL Server database, and Excel is also considered a database. It is technically difficult to use OleDb to connect and then read data and write it into the SQL Server database connected with SqlClient.

However, a problem to be considered is that the system installation environment is generally dedicated SQL server + web server + client, while Excel files are often imported on the client. If you directly use SQL statements to read the files, because SQL runs on a web server and cannot read the Excel address of the client, an error occurs.

Now that you know the cause of the error, the solution will be:

1. Upload the client Excel file to a folder on the web server. Be sure to set the "write" permission for the network user for the folder;

2. Use OleDb + SQL statements to read Excel files on the web server

3. Import the read ASP. NET data to the SQL Server database.

4. Delete temporary Excel files on the web server

Some code is as follows:

 
 
  1. StringFilename = FileUpload1.PostedFile. FileName. Substring (FileUpload1.PostedFile. FileName. LastIndexOf ("\\"));
  2. FileUpload1.PostedFile. SaveAs (Server. MapPath ("Fileupload \\") + Filename );// Upload a file 
  3.  
  4. StringConn ="Provider = Microsoft. Jet. OLEDB.4.0; Data Source ="+
  5. Server. MapPath ("Fileupload \") + filename +"; Extended Properties = Excel 8.0 ";
  6. StringSqlin ="SELECT * FROM [Sheet2 $]";
  7. OleDbCommand olecommand =NewOleDbCommand (sqlin,NewOleDbConnection (conn ));
  8. OleDbDataAdapter adapterin =NewOleDbDataAdapter (olecommand );
  9. DataSet dsin =NewDataSet ();
  10. Adapterin. Fill (dsin );
  11. DataTable dtin = dsin. Tables [0];// Connect and read Excel Data 
  12.  
  13. For(I = 3; I <= totalrow; I ++)// Import the data of the first worksheet in the Excel file to the SQL database scjd_youliaoxiaohaojihua table 
  14. {
  15. StringSQL ="Insert into scjd_youliaoxiaohaojihua (yuexuhao, danwei, youpin,
  16. Cheliangmingchengxinghao, zichanxingzhi, chew.hao, qiyou, chaiyou, beizhu) values ('";
  17. IntJ;
  18. SQL + = DropDownList1.SelectedValue. ToString () + DropDownList3.SelectedValue. ToString ();
  19. If(DropDownList1.SelectedValue. ToString ()! = DateTime. Now. Year. ToString () |Int. Parse
  20. (DateTime. Now. Month. ToString ())! =Int. Parse (DropDownList3.SelectedValue. ToString ()))
  21. {
  22. SQL + ="20";
  23. }
  24. Else 
  25. {
  26. If(Int. Parse (DateTime. Now. Day. ToString () <10)
  27. SQL + ="0";
  28. SQL + = DateTime. Now. Day. ToString ();
  29. }
  30. IfI-3 + count <9)
  31. SQL + ="00";
  32. Else IfI-3 + count <99)
  33. SQL + ="0";
  34. SQL + = (I-3 + 1 + count). ToString ();
  35. SQL + ="','";
  36. For(J = 1; j <8; j ++)
  37. {// Traverse all columns in a row in the Excel table, except the last column 
  38. SQL + = dtin. Rows [I] [j]. ToString (). Trim ();
  39. SQL + ="','";
  40. }
  41. SQL + = dtin. Rows [I] [8]. ToString (). Trim ();
  42. SQL + ="')";
  43. Try 
  44. {
  45. DoSql (SQL );
  46. }
  47. Catch(Exception eeeeeee)
  48. {
  49. Response. Write ("<Script> alert ('data import error, please check the Excel file') </script>");
  50. Return;
  51. }
  52. }
  53.  
  54. FileInfo file =NewFileInfo (Server. MapPath ("Fileupload /") + Filename );
  55. If(File. Exists)
  56. {// Delete an object 
  57. File. Delete ();
  58. }
  59.  
  60. Protected VoidDoSql (StringSQL)// Functions used to execute SQL statements 
  61. {
  62. SqlConnection conn =NewSqlConnection ();// Create a connection object 
  63. Conn. ConnectionString = ConfigurationManager. etettings ["Conn"]. ToString ();// Assign a value to the connection string 
  64. Conn. Open ();// Open the database 
  65. SqlCommand cmd =NewSqlCommand (SQL, conn );
  66. Cmd. ExecuteNonQuery ();// 
  67. Conn. Close ();// Close the database 
  68. }

Note:

1. The code in this article is based on a small project that I just created recently. Some data is highly targeted and not universal, and only has a general idea.

2. Due to the capability and level problems, some code seems clumsy. If you can use the Stored Procedure flexibly, you should be able to greatly simplify the code.

This section describes how to import ASP. NET data to Excel to MSSQL. We hope you can understand how to import ASP. NET data to Excel to MSSQL.

  1. Introduction to data caching in ASP. NET data cache
  2. Implementation of ASP. NET data collection
  3. Use of SqlHelperSqlHelper in ASP. NET data access layer
  4. Hash Algorithm for ASP. NET Data Encryption
  5. Implement ASP. NET data encryption using symmetric encryption algorithms

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.