sql 通过存储过程和自定义类型批量新增数据

1,建立存储过程

create PROCEDURE [dbo].[p_Company_Insert]
@CompanyCollection [CompanyTableType] READONLY
AS
INSERT INTO tb_Company (
            [cpID]
           ,[cpHiID]
           ,[cpBuySellTypeID]
           ,[cpName]
           ,[cpShortName]
           ,[cpIndustry]
           ,[cpSell]
           ,[cpBuy]
           ,[cpAddressID]
           ,[cpAddress]
           ,[cpPost]
           ,[cpTel]
		)

    SELECT 
			oc.[cpID]
           ,oc.[cpHiID]
           ,oc.[cpBuySellTypeID]
           ,oc.[cpName]
           ,oc.[cpShortName]
           ,oc.[cpIndustry]
           ,oc.[cpSell]
           ,oc.[cpBuy]
           ,oc.[cpAddressID]
           ,oc.[cpAddress]
           ,oc.[cpPost]
           ,oc.[cpTel]
    FROM @CompanyCollection AS oc;

GO

2,建立相对应的数据类型

/****** Object:  UserDefinedTableType [dbo].[CompanyTableType]    Script Date: 07/04/2014 10:20:51 ******/
CREATE TYPE [dbo].[CompanyTableType] AS TABLE(
	[cpID] [int] NOT NULL,
	[cpHiID] [int] NULL,
	[cpBuySellTypeID] [nvarchar](200) NULL,
	[cpName] [nvarchar](200) NOT NULL,
	[cpShortName] [nvarchar](200) NULL,
	[cpIndustry] [nvarchar](300) NULL,
	[cpSell] [nvarchar](200) NULL,
	[cpBuy] [nvarchar](200) NULL,
	[cpAddressID] [int] NOT NULL,
	[cpAddress] [nvarchar](300) NULL,
	[cpPost] [nvarchar](100) NULL,
	[cpTel] [nvarchar](100) NULL,
)
GO

3,执行代码

        /// <summary>
        /// 单条添加,将datatable作为参数传进去,返回datatable的自增长编号(调用存储过程)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        [WebMethod]
        public DataTable BuySell_Insert(DataTable dt, string token)
        {
            CheckLoginedS(token);
            if (dt.Rows.Count > 0 && dt.Rows != null)
            {
                tb_BuySell bs = new tb_BuySell();
                DataTable bt = bs.BuySell_Insert(dt);
                return bt;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 把datatable当参数,批量添加数据库中,返回datatable的新增行
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public DataTable BuySell_Insert(DataTable tb)
        {
            DataTable dt = null;
            CMD.CommandText = "p_BuySell_Insert";
            CMD.CommandType = CommandType.StoredProcedure;
            CMD.Parameters.Clear();
            CMD.Parameters.AddWithValue("@BuySellCollection", tb);
            dt = DB.DataTable(CMD);
            return dt;
        }

  

转载于:https://www.cnblogs.com/Aamir-Ye/p/4560961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值