上一篇Using LINQ in ASP.NET (1) 中介紹了利用LINQ to SQL實現對數據的增刪改的操作,但是在實 際的項目應用中,我們經常會使用到存儲過程。本篇將介紹如何利用LINQ對存儲過程進行操作。
我們利用的還是Northwind數據庫,首先創建存儲過程:
(1)返回所有EMPLOYEES 的信息
CREATE PROCEDURE [dbo].[Employees_GetAll]
AS
SELECT * FROM EMPLOYEES ORDER BY EMPLOYEEID
(2)根據EMPLOYEEID獲得信息
CREATE PROCEDURE [dbo].[Employees_GetByID]
(
@ID int
)
AS
SELECT * FROM EMPLOYEES WHERE EMPLOYEEID=@ID
(3)添加職員信息
CREATE PROCEDURE [dbo].[Employees_Insert]
(
@FIRSTNAME NVARCHAR(20),
@LASTNAME NVARCHAR(20)
)
AS
INSERT INTO EMPLOYEES(FIRSTNAME,LASTNAME)
VALUES(@FIRSTNAME,@LASTNAME)
(4)更新職員信息
CREATE PROCEDURE [dbo].[Employees_Update]
(
@ID INT,
@FIRSTNAME NVARCHAR(20),
@LASTNAME NVARCHAR(20)
)
AS
UPDATE EMPLOYEES
SET FIRSTNAME=@FIRSTNAME,
LASTNAME=@LASTNAME
WHERE EMPLOYEEID=@ID