Excel import operation MS SQL SERVER

On issues related to import Excel to sql operation summary:

First, high-volume data import

1, Excel import large quantities of data that we can use sql from inside a batch copy function

Method 2, to build a table type sql structure, when the front end of the datatable excel read, the entire procedure parameters passed datatable stored as background database.

For example:

c#:

public void Import(DataTable dt)
        {
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@DataTable",SqlDbType.Structured)
            };

            parms[0].Value = dt;

            SQLHelper.ExecuteNonQueryStoredProcedure(ConnString, "ImportExcel", parms);
        }

 

SQL:

--创建table type

CREATE TYPE [dbo].[DataTable_TYPE] AS TABLE( [NO] [varchar](20) NULL, [Dept] [varchar](20) NULL, [EmployeeNO] [decimal](18,2) NULL, [EmployeeName] [nvarchar](50) NULL, [JoinDate] [datetime] NULL, [TotalScore] [decimal](18, 2) NULL, [JobQty] [int] NULL, [JobI] [decimal](18, 2) NULL, [JobJ] [decimal](18, 2) NULL, [JobK] [decimal](18, 2) NULL, [JobL] [decimal](18, 2) NULL, [JobM] [decimal](18, 2) NULL, [JobN] [decimal](18, 2) NULL, [JobO] [decimal](18, 2) NULL )

--导入数据存储过程
create proc ImportExcel
@DataTable DataTable_TYPE readonly
as
begin
  insert into XXtable
  select ......
  from @DataTable
end

 Note: When importing data excel, excel many times the read time datatable, excel in the numerical sequence becomes scientific notation, then set the corresponding type of column table type or table can be set to decimal.

Also said in a database when a select cast (cast (abc as float) as decimal (xxx, xx)) directly, but this seems to be a problem is the problem of rounding, such as 10,056,390 after scientific notation to convert 1.00564e + 007 It will become 10,056,400

 

Reproduced in: https: //www.cnblogs.com/Alenliu/p/4226115.html

Guess you like

Origin blog.csdn.net/weixin_33754913/article/details/93470050