程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 簡單實現linq存儲過程基本操作

簡單實現linq存儲過程基本操作

編輯:關於.NET

linq存儲過程的運用還是比較廣泛的,如果想運用好linq語言,那linq存儲過程是必不可少的。

本篇,關注的是linq存儲過程來完成數據庫的四步操作(查詢,刪除,更新,新增)。

數據庫仍然是Northwind,數據源是表Customers

1). 首先創建一個網站,添加Linq to Sql類,名稱為Northwind.dbml;

2). 打開Visual Studio的服務器資源管理器,選擇數據連接-添加連接,引入Northwind數據庫,展開 ,將Customers表拖到Northwind.dbml,這樣就完成了創建Customers對象;

3). 創建查詢,刪除,更新,新增 Customers 的linq存儲過程,代碼如下:

linq存儲過程之查詢Customers

Procedure Name: [dbo].[selCustomers]
Note               : get Customers
Created By       : Tom
Created Date    : 2008-5-28
CREATE PROCEDURE [dbo].[selCustomers]
(
@ncCoustomerID NVARCHAR(5) = NULL,
@nvcCountry NVARCHAR(15) = NULL
AS
IF @ncCoustomerID IS NULL OR @ncCoustomerID = ''
SET @ncCoustomerID = NULL
IF @nvcCountry IS NULL OR @nvcCountry = ''
SET @nvcCountry = NULL
SELECT *
FROM CUSTOMERS
WHERE CUSTOMERID LIKE ISNULL(@ncCoustomerID, '%')
AND    COUNTRY LIKE ISNULL(@nvcCountry, '%')
RETURN

linq存儲過程之刪除Customers

Procedure Name : [dbo].[delCustomers]
Note                : delete Customer
Created By        : Tom
Created Date     : 2008-5-28
CREATE PROCEDURE [dbo].[delCustomers]
(
@ncCustomerID NCHAR(5)
)
AS
DELETE CUSTOMERS
WHERE CUSTOMERID = @ncCUSTOMERID
RETURN

linq存儲過程之更新Customer

Procedure Name : [dbo].[updCustomers]
Note                : modify Customer
Created By        : Tom
Created Date     : 2008-5-28
CREATE PROCEDURE [dbo].[updCustomers]
(
  @ncCustomerID NCHAR(5),
  @nvcCompanyName NVARCHAR(40),
  @nvcAddress NVARCHAR(60),
  @intReturnValue INT OUTPUT
)
AS
--不返回受影響的行
  SET NOCOUNT ON
  --更新指定的Customers
  UPDATE CUSTOMERS
SET COMPANYNAME = @nvcCompanyName,
   ADDRESS = @nvcAddress
  WHERE CUSTOMERID = @ncCustomerID
IF @@ERROR <> 0
   BEGIN
   --如果更新發生異常,返回-1
  SET @intReturnValue = -1
  RETURN
   END
RETURN

linq存儲過程之新增Customer

Procedure Name : [dbo].[updCustomers]
Note                : modify Customer
Created By        : Tom
Created Date     : 2008-5-28
CREATE PROCEDURE [dbo].[updCustomers]
(
@ncCustomerID NCHAR(5),
@nvcCompanyName NVARCHAR(40),
@nvcAddress NVARCHAR(60),
@intReturnValue INT OUTPUT
)
AS
--不返回受影響的行
  SET NOCOUNT ON
  --更新指定的Customers
  UPDATE CUSTOMERS
  SET COMPANYNAME = @nvcCompanyName,
ADDRESS = @nvcAddress
  WHERE CUSTOMERID = @ncCustomerID
IF @@ERROR <> 0
  BEGIN
   --如果更新發生異常,返回-1
  SET @intReturnValue = -1
   RETURN
  END
RETURN

4). 打開Visual Studio2008的服務器資源管理器,展開Northwind數據庫的linq存儲過程目錄,將上 敘四個存儲過程依次拖入到打開的 Northwind.dbml右側方法區,查看引入的四個linq存儲過程的屬性, 修改其Name名為"GetCustomers","DeleteCustomers","UpdateCustomers","InsertCustomers",如下圖所 示:

四個linq存儲過程的屬性

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved