开发要求,前台一次性将多笔记录存入数据库中。也就是说,将一个DataTable作为一个参数传入。而数据库的存储过程,也得有一个表数据类型(sql2008) http://www.cnblogs.com/insus/articles/1916591.html
为了实现这个功能,Insus.NET先从数据库,然后至程序前台,一步一步演示给大家。在数据库中创建一个表[Orders]:
View Code 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
创建一个表类型[OrdersTableType]:
View Code 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
创建存储过程[usp_Orders_Insert],这个存储过程的变量,就是上面定义好的表类型。
View Code 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
在程序,写一个类别Orders,其中一个属性,private DataTable _OrdersDataTable; 与一个方法Insert().
Ordersusing 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 );
}
}
}
在xxx.aspx网页中拉一个按钮,希望把DataTable在铵钮的事件中,插入数据库中:
<asp:Button ID="ButtonAdd" runat="server" Text="Add" OnClick="ButtonAdd_Click" />
在cs页面中,写一个DataTable,Insus.NET并手动产生三笔记录。真正的环境是你得用户有前端产生多笔记录之后,最后再一次性传入数据库中。
GetTable() 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;
}
最后是铵钮Click事件:
引用命名空间 using Insus.NET;
实例化Orders类别 Orders objOrders = new Orders();
View Code 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);
}
}