码迷,mamicode.com
首页 > 数据库 > 详细

sql server 表值类型的使用

时间:2016-01-25 11:24:05      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

如果你需要向存储过程中传递一个table 那么就用到了表值类型

1.创建表值类型

CREATE TYPE [dbo].[StorePreSeparationAmount] AS TABLE(
[preSeparationId] [int] NOT NULL,
[preSeparationAmount] [decimal](18, 2) NOT NULL,
[userCode] [varchar](20) NOT NULL
)
GO

  

 2.创建存储过程

CREATE PROC sp_preSeparationCommit
(
    @spsa StorePreSeparationAmount READONLY
)
AS
UPDATE ps
SET ps.preSeparationAmount=spsa.preSeparationAmount,
ps.updater=spsa.userCode,
ps.updateTime=GETDATE()
FROM dbo.wms_preSeparation ps
INNER JOIN @spsa spsa ON ps.preSeparationId=spsa.preSeparationId

 

3.Ado.net 使用表值类型

 public bool updatePreSeparationAmount(DataTable dtStorePreSeparationAmount)
        {
            string strSpName = @"sp_preSeparationCommit";
            SqlParameter[] pars =
                    {
                        new SqlParameter("@spsa",SqlDbType.Structured),
                    };

            pars[0].Value = dtStorePreSeparationAmount;
            pars[0].TypeName = "StorePreSeparationAmount";

            int num = DalBase.ExecuteNonQuery(CommandType.StoredProcedure, strSpName, pars);
            if (num > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

  备注:table创建

 DataTable dtStorePreSeparationAmount = new DataTable("dtStorePreSeparationAmount");
                        dtStorePreSeparationAmount.Columns.Add("preSeparationId", Type.GetType("System.Int32"));
                        dtStorePreSeparationAmount.Columns.Add("preSeparationAmount", Type.GetType("System.Decimal"));
                        dtStorePreSeparationAmount.Columns.Add("userCode", Type.GetType("System.String"));

  DataRow drNew = dtStorePreSeparationAmount.NewRow();
                                drNew["preSeparationId"] = nPreSeparationId;
                                drNew["preSeparationAmount"] = dPreSeparationAmount;
                                drNew["userCode"] = login.UserName;
                                dtStorePreSeparationAmount.Rows.Add(drNew);

  

 

sql server 表值类型的使用

标签:

原文地址:http://www.cnblogs.com/caohuimingfa/p/5156627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!