步步为营VS 2008 + .NET 3.5(10) - DLINQ(LINQ to SQL)之调用存储过程的添加、查询、更新和删除
作者:webabcd
介绍
以Northwind为示例数据库,DLINQ(LINQ to SQL)之调用指定存储过程的添加操作、查询操作、更新操作和删除操作
示例
相关的存储过程
ALTER PROCEDURE [dbo].[spInsertCategory]
@CategoryName nvarchar(15),
@Description ntext,
@CategoryID int OUTPUT
AS
SET NOCOUNT ON
INSERT INTO [dbo].[Categories] (
[CategoryName],
[Description]
) VALUES (
@CategoryName,
@Description
)
SET @CategoryID = SCOPE_IDENTITY()
RETURN @@ERROR
@CategoryName nvarchar(15),
@Description ntext,
@CategoryID int OUTPUT
AS
SET NOCOUNT ON
INSERT INTO [dbo].[Categories] (
[CategoryName],
[Description]
) VALUES (
@CategoryName,
@Description
)
SET @CategoryID = SCOPE_IDENTITY()
RETURN @@ERROR
ALTER PROCEDURE [dbo].[spUpdateCategory]
@CategoryID int,
@CategoryName nvarchar(15),
@Description ntext
AS
SET NOCOUNT ON
UPDATE [dbo].[Categories] SET
[CategoryName] = @CategoryName,
[Description] = @Description
WHERE
[CategoryID] = @CategoryID
RETURN @@ERROR
@CategoryID int,
@CategoryName nvarchar(15),
@Description ntext
AS
SET NOCOUNT ON
UPDATE [dbo].[Categories] SET
[CategoryName] = @CategoryName,
[Description] = @Description
WHERE
[CategoryID] = @CategoryID
RETURN @@ERROR
ALTER PROCEDURE [dbo].[spDeleteCategory]
@CategoryID int
AS
SET NOCOUNT ON
DELETE FROM [dbo].[Categories]
WHERE
[CategoryID] = @CategoryID
RETURN @@ERROR
@CategoryID int
AS
SET NOCOUNT ON
DELETE FROM [dbo].[Categories]
WHERE
[CategoryID] = @CategoryID
RETURN @@ERROR
ALTER PROCEDURE [dbo].[spSelectCategory]
@CategoryID int = null
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT
[CategoryID],
[CategoryName],
[Description],
[Picture]
FROM
[dbo].[Categories]
WHERE
@CategoryID IS NULL OR [CategoryID] = @CategoryID
@CategoryID int = null
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT
[CategoryID],
[CategoryName],
[Description],
[Picture]
FROM
[dbo].[Categories]
WHERE
@CategoryID IS NULL OR [CategoryID] = @CategoryID
ALTER PROCEDURE [dbo].[spSelectProduct]
@ProductID int = null
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT
[ProductID],
[ProductName],
[SupplierID],
[CategoryID],
[QuantityPerUnit],
[UnitPrice],
[UnitsInStock],
[UnitsOnOrder],
[ReorderLevel],
[Discontinued]
FROM
[dbo].[Products]
WHERE
@ProductID IS NULL OR [ProductID] = @ProductID
@ProductID int = null
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SELECT
[ProductID],
[ProductName],
[SupplierID],
[CategoryID],
[QuantityPerUnit],
[UnitPrice],
[UnitsInStock],
[UnitsOnOrder],
[ReorderLevel],
[Discontinued]
FROM
[dbo].[Products]
WHERE
@ProductID IS NULL OR [ProductID] = @ProductID
SP.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SP.aspx.cs"
Inherits="LINQ_DLINQ_SP" Title="调用存储过程的添加、查询、更新和删除" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
分类名称:<asp:TextBox ID="txtCategoryName" runat="server"></asp:TextBox>
分类描述:<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
</p>
<asp:GridView ID="gvCategory" runat="server" DataKeyNames="CategoryID" OnSelectedIndexChanged="gvCategory_SelectedIndexChanged"
OnRowDeleting="gvCategory_RowDeleting" OnRowCancelingEdit="gvCategory_RowCancelingEdit"
OnRowEditing="gvCategory_RowEditing" OnRowUpdating="gvCategory_RowUpdating">
<Columns>
<asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True">
</asp:CommandField>
</Columns>
</asp:GridView>
<br />
<asp:DetailsView ID="dvProduct" runat="server" DataKeyNames="ProductID">
</asp:DetailsView>
</asp:Content>
Inherits="LINQ_DLINQ_SP" Title="调用存储过程的添加、查询、更新和删除" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<p>
分类名称:<asp:TextBox ID="txtCategoryName" runat="server"></asp:TextBox>
分类描述:<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
</p>
<asp:GridView ID="gvCategory" runat="server" DataKeyNames="CategoryID" OnSelectedIndexChanged="gvCategory_SelectedIndexChanged"
OnRowDeleting="gvCategory_RowDeleting" OnRowCancelingEdit="gvCategory_RowCancelingEdit"
OnRowEditing="gvCategory_RowEditing" OnRowUpdating="gvCategory_RowUpdating">
<Columns>
<asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True">
</asp:CommandField>
</Columns>
</asp:GridView>
<br />
<asp:DetailsView ID="dvProduct" runat="server" DataKeyNames="ProductID">
</asp:DetailsView>
</asp:Content>
SP.aspx.cs











































































































OK
[源码下载]
本文出自 “webabcd” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/345010