存储过程参数为DataTable
来源:互联网 发布:cad网络信息点图标 编辑:程序博客网 时间:2024/06/05 22:43
开发要求,前台一次性将多笔记录存入数据库中。也就是说,将一个DataTable作为一个参数传入。而数据库的存储过程,也得有一个表数据类型(sql2008) http://www.cnblogs.com/insus/articles/1916591.html
为了实现这个功能,Insus.NET先从数据库,然后至程序前台,一步一步演示给大家。在数据库中创建一个表[Orders]:
CREATE TABLE [dbo].[Orders]
(
[Orders_nbr] INT IDENTITY(1,1) PRIMARY KEY,
[ItemCode] NVARCHAR(50) NOT NULL,
[UM] NVARCHAR(20) NOT NULL,
[Quantity] DECIMAL(18,6) NOT NULL,
[UnitPrice] DECIMAL(18,6) NOT NULL
)
GO
(
[Orders_nbr] INT IDENTITY(1,1) PRIMARY KEY,
[ItemCode] NVARCHAR(50) NOT NULL,
[UM] NVARCHAR(20) NOT NULL,
[Quantity] DECIMAL(18,6) NOT NULL,
[UnitPrice] DECIMAL(18,6) NOT NULL
)
GO
创建一个表类型[OrdersTableType]:
CREATE TYPE [dbo].[OrdersTableType] AS TABLE
(
ItemCode NVARCHAR(50) NOT NULL,
UM NVARCHAR(20) NOT NULL,
Quantity DECIMAL(18,6) NOT NULL,
UnitPrice DECIMAL(18,6) NOT NULL
)
GO
(
ItemCode NVARCHAR(50) NOT NULL,
UM NVARCHAR(20) NOT NULL,
Quantity DECIMAL(18,6) NOT NULL,
UnitPrice DECIMAL(18,6) NOT NULL
)
GO
创建存储过程[usp_Orders_Insert],这个存储过程的变量,就是上面定义好的表类型。
CREATE PROCEDURE [dbo].[usp_Orders_Insert]
(
@OrdersCollection [OrdersTableType] READONLY
)
AS
INSERT INTO [dbo].[Orders] ([ItemCode],[UM],[Quantity],[UnitPrice])
SELECT oc.[ItemCode],oc.[UM],[Quantity],oc.[UnitPrice] FROM @OrdersCollection AS oc;
GO
(
@OrdersCollection [OrdersTableType] READONLY
)
AS
INSERT INTO [dbo].[Orders] ([ItemCode],[UM],[Quantity],[UnitPrice])
SELECT oc.[ItemCode],oc.[UM],[Quantity],oc.[UnitPrice] FROM @OrdersCollection AS oc;
GO
在程序,写一个类别Orders,其中一个属性,private DataTable _OrdersDataTable; 与一个方法Insert().
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Orders
/// </summary>
namespace Insus.NET
{
public class Orders
{
private string _ItemCode;
private string _UM;
private decimal _Quantity;
private decimal _UnitPrice;
private DataTable _OrdersDataTable;
public string ItemCode
{
get { return _ItemCode; }
set { _ItemCode = value; }
}
public string UM
{
get { return _UM; }
set { _UM = value; }
}
public decimal Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
public decimal UnitPrice
{
get { return _UnitPrice; }
set { _UnitPrice = value; }
}
public DataTable OrdersDataTable
{
get { return _OrdersDataTable; }
set { _OrdersDataTable = value; }
}
BusinessBase objBusinessBase = new BusinessBase ();
public Orders()
{
//
// TODO: Add constructor logic here
//
}
public void Insert()
{
Parameter[] parameter = {
new Parameter ("@OrdersCollection",SqlDbType.Structured,-1,_OrdersDataTable),
};
objBusinessBase.ExecuteProcedure ("usp_Orders_Insert",parameter );
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Orders
/// </summary>
namespace Insus.NET
{
public class Orders
{
private string _ItemCode;
private string _UM;
private decimal _Quantity;
private decimal _UnitPrice;
private DataTable _OrdersDataTable;
public string ItemCode
{
get { return _ItemCode; }
set { _ItemCode = value; }
}
public string UM
{
get { return _UM; }
set { _UM = value; }
}
public decimal Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
public decimal UnitPrice
{
get { return _UnitPrice; }
set { _UnitPrice = value; }
}
public DataTable OrdersDataTable
{
get { return _OrdersDataTable; }
set { _OrdersDataTable = value; }
}
BusinessBase objBusinessBase = new BusinessBase ();
public Orders()
{
//
// TODO: Add constructor logic here
//
}
public void Insert()
{
Parameter[] parameter = {
new Parameter ("@OrdersCollection",SqlDbType.Structured,-1,_OrdersDataTable),
};
objBusinessBase.ExecuteProcedure ("usp_Orders_Insert",parameter );
}
}
}
在xxx.aspx网页中拉一个按钮,希望把DataTable在铵钮的事件中,插入数据库中:
<asp:Button ID="ButtonAdd" runat="server" Text="Add" OnClick="ButtonAdd_Click" />
在cs页面中,写一个DataTable,Insus.NET并手动产生三笔记录。真正的环境是你得用户有前端产生多笔记录之后,最后再一次性传入数据库中。
private DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("ItemCode", typeof(string));
table.Columns.Add("UM", typeof(string));
table.Columns.Add("Quantity", typeof(decimal));
table.Columns.Add("UnitPrice", typeof(decimal));
table.Rows.Add("A003-06", "pcs", "10", "3.24");
table.Rows.Add("A133-26", "pcs", "10", "9.06");
table.Rows.Add("A605-06", "pcs", "3", "5.67");
return table;
}
{
DataTable table = new DataTable();
table.Columns.Add("ItemCode", typeof(string));
table.Columns.Add("UM", typeof(string));
table.Columns.Add("Quantity", typeof(decimal));
table.Columns.Add("UnitPrice", typeof(decimal));
table.Rows.Add("A003-06", "pcs", "10", "3.24");
table.Rows.Add("A133-26", "pcs", "10", "9.06");
table.Rows.Add("A605-06", "pcs", "3", "5.67");
return table;
}
最后是铵钮Click事件:
引用命名空间 using Insus.NET;
实例化Orders类别 Orders objOrders = new Orders();
protected void ButtonAdd_Click(object sender, EventArgs e)
{
Orders objOrders = new Orders();
try
{
objOrders.OrdersDataTable = GetTable();
objOrders.Insert();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
{
Orders objOrders = new Orders();
try
{
objOrders.OrdersDataTable = GetTable();
objOrders.Insert();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
执行结果:
0 0
- 存储过程参数为DataTable
- 关于sql存储过程接收参数为datatable
- 存储过程传入DataTable参数
- Sql2008中使用DataTable作为存储过程的参数
- 将DataTable作为存储过程参数的用法
- [C#新手入门 三] 使用DataTable作为存储过程的参数
- 得到存储过程参数为output的参数值
- Oracle-存储过程-参数为数组-返回值为集合,java-调用存储过程
- .net 如何将datatable作为参数传入sql的存储过程
- 用存储过程保存多条信息(DataTable作为参数)
- 如何设置SQl存储过程调用参数为空
- java调用参数为数组的存储过程
- tibco 调用存储过程(参数为date)备忘
- JAVA调用存储过程输出参数为游标
- 示例12 存储过程和参数为输出类型游标
- 存储过程数据集输出参数类型为非游标
- 判断存储过程查询条件参数为空
- mysql动态sql,存储过程动态tablename,存储过程参数为表名
- IE9的文档模式和JavaScript
- 统计字符串中单词的个数(指针)
- mysql命令行常用命令
- 《实用OpenCV》<一> 计算机视觉及OpenCV介绍
- Linux init程序分析
- 存储过程参数为DataTable
- 浅谈矩阵分解以及应用
- HDOJ 1165 Eddy's research II(数学)
- POJ 动态规划题目列表
- oracle 安装过程中的问题
- Linux init程序分析(续)
- ios_oc @property和@synthesize以及属性介绍
- Oracle中的wmsys.wm_concat
- Android 创建快捷方式图标