開發要求,前台一次性將多筆記錄存入數據庫中。也就是說,將一個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
(
[ 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
(
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
(
@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().
Orders
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並手動產生三筆記錄。真正的環境是你得用戶有前端產生多筆記錄之后,最后再一次性傳入數據庫中。
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;
}
{
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);
}
}
{
Orders objOrders = new Orders();
try
{
objOrders.OrdersDataTable = GetTable();
objOrders.Insert();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
執行結果:

