Simple implementation method for uploading EXCEL data to SQL SERVER
- 2020-05-30 21:11:26
- OfStack
Three things to note about the method of uploading EXCEL data to SQL SERVER!
Note 1: to upload EXCEL data to SQL SERVER, you must upload EXCEL to the server in advance.
How to do it: in the ASP.NET environment, add an FileUpload upload control
E. X:
Note 2: the structure (fields) of the table in the SQL SERVER server should be in the same order as the EXCEL format sent from the SQL SERVER server.
The key code to save to the server is E.X (SQL statement):
Teenie is the name of the previous page of my EXCEL (note that this 1 must be written correctly, otherwise it will be wrong, and $should be added after the name).
Unblock SQL:
SQL Server blocks access to STATEMENT'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component has been shut down as part of the security configuration for this server. System administrators can enable 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see the 'peripheral application configurator' in the SQL Server online series.
Because SQL2005 does not turn on the 'Ad Hoc Distributed Queries' component by default, the method is as follows
Ha ha! Said to the use of this relatively simple can deal with 1, because of EXCEL data for 1, are the company's internal data processing, so completely enough to deal with, but if it is a commercial or professional website to let any ordinary users upload EXCEL words is not suitable, after all, you can't urge all users to the name of the table data pages EXCEL call Teenie ha ha, one kind of very troublesome SQL online statements (is the form of parameters) can solve this problem, ha ha just as well go to under the reference 1!
Note 1: to upload EXCEL data to SQL SERVER, you must upload EXCEL to the server in advance.
How to do it: in the ASP.NET environment, add an FileUpload upload control
E. X:
if (FileUpload1.HasFile) // If the user does select the browse button for the upload control , Browse file successfully .
{
this.FileUpload1.SaveAs("E:\\Temp\\" + FileUpload1.FileName); // Save to the server's directory , It needs to be modified according to the specific situation of the server FileUpload1.FileName The file name of the upload is automatically retrieved .
}
//OK Successfully uploaded to the server E:\Temp directory ( Pay attention to error handling when writing your own , And exception handling , It is very important ).
Note 2: the structure (fields) of the table in the SQL SERVER server should be in the same order as the EXCEL format sent from the SQL SERVER server.
The key code to save to the server is E.X (SQL statement):
insert into EmployeesCheck select * from OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 8.0;hdr=yes;database=E:\\Temp\\" + FileUpload1.FileName + "',Teenie$)
// EmployeesCheck Is my in SQL SERVER The data acceptance table in ,hdr=yes said EXCEL Content of the first 1 Rows are used as content fields rather than as field columns
Teenie is the name of the previous page of my EXCEL (note that this 1 must be written correctly, otherwise it will be wrong, and $should be added after the name).
Unblock SQL:
SQL Server blocks access to STATEMENT'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component has been shut down as part of the security configuration for this server. System administrators can enable 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see the 'peripheral application configurator' in the SQL Server online series.
Because SQL2005 does not turn on the 'Ad Hoc Distributed Queries' component by default, the method is as follows
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
Ha ha! Said to the use of this relatively simple can deal with 1, because of EXCEL data for 1, are the company's internal data processing, so completely enough to deal with, but if it is a commercial or professional website to let any ordinary users upload EXCEL words is not suitable, after all, you can't urge all users to the name of the table data pages EXCEL call Teenie ha ha, one kind of very troublesome SQL online statements (is the form of parameters) can solve this problem, ha ha just as well go to under the reference 1!