如何插入一个数据表到SQL Server数据库中的表? [英] How to insert a data table into SQL Server database table?
问题描述
我从一些Excel文件导入数据,我有它保存到一个数据表
。现在,我想救我的 SQL服务器
数据库信息。
I have imported data from some Excel file and I have saved it into a datatable
. Now I'd like to save this information in my SQL Server
database.
我看到了很多的信息在网络上,但我无法理解这一点:
I saw a lot of information on the web but I cannot understand it:
- 有人说按行插入线另一建议批量更新...等:什么?它更好
- 我应该使用
OLE
或SQL服务器
对象(如的DataAdapter
或连接
)?
- Someone said insert line by line another suggested bulk update... etc: what it better?
- Should I use
OLE
orSQL Server
objects (likedataAdapter
orconnection
)?
我需要的是从他的Excel文件中读取员工每周小时的报告,并将其保存到,所有的报告都保存在一个数据库表(请使用每周新记录的DB)。
My need is to read the employee weekly hours report, from his Excel file and save it to a database table where all the reports are saved (updating the db with new records every week).
Excel文件只包含当前周报告。
The Excel file contains reports only for the current week.
推荐答案
创建一个自定义TABLETYPE
数据库中的:
CREATE TYPE [dbo].[MyTableType] AS TABLE(
[Id] int NOT NULL,
[Name] [nvarchar](128) NULL
)
和在存储Ptocedure定义参数
:
CREATE PROCEDURE [dbo].[InsertTable]
@myTableType MyTableType readonly
AS
BEGIN
insert into [dbo].Records select * from @myTableType
END
和发送数据表
直接到SQL Server:
and send your DataTable
directly to sql server:
using (var command = new SqlCommand("InsertTable") {CommandType = CommandType.StoredProcedure})
{
var dt = new DataTable(); //create your own data table
command.Parameters.Add(new SqlParameter("@myTableType", dt));
SqlHelper.Exec(command);
}
这篇关于如何插入一个数据表到SQL Server数据库中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!