如何插入一个数据表到SQL Server数据库中的表? [英] How to insert a data table into SQL Server database table?

查看:172
本文介绍了如何插入一个数据表到SQL Server数据库中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一些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:


  1. 有人说按行插入线另一建议批量更新...等:什么?它更好

  2. 我应该使用 OLE SQL服务器对象(如的DataAdapter 连接)?

  1. Someone said insert line by line another suggested bulk update... etc: what it better?
  2. Should I use OLE or SQL Server objects (like dataAdapter or connection)?

我需要的是从他的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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆