步步为营VS 2008 + .NET 3.5(10) - DLINQ(LINQ to SQL)之调用存储过程的添加、查询、更新和删除

简介:
[索引页]
[源码下载]


步步为营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
 
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
 
ALTER PROCEDURE [dbo].[spDeleteCategory] 
        @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
 
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
 
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>
SP.aspx.cs
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
 
using DAL; 
 
public partial  class LINQ_DLINQ_SP : System.Web.UI.Page 

         // 实例化一个NorthwindDataContext(DataContext) 
         // 在对象关系设计器(Object Relational Designer)中拖进来存储过程,同时NorthwindDataContext类中就会自动生成调用相应存储过程的相应方法 
        NorthwindDataContext _ctx =  new NorthwindDataContext(); 
 
         protected  void Page_Load( object sender, EventArgs e) 
        { 
                 if (!Page.IsPostBack) 
                { 
                        BindCategory(); 
                } 
        } 
 
         private  void BindCategory() 
        { 
                var categories = _ctx.GetCategory( null); 
 
                gvCategory.DataSource = categories; 
                gvCategory.DataBind(); 
        } 
 
         protected  void btnAdd_Click( object sender, EventArgs e) 
        { 
                 // categoryId - 用于获取存储过程的输出值(output) 
                 int? categoryId =  null
                 // rtn - 用于获取存储过程的返回值(return) 
                 int rtn = _ctx.AddCategory(txtCategoryName.Text, txtDescription.Text,  ref categoryId); 
 
                Page.ClientScript.RegisterStartupScript( 
                         this.GetType(), 
                         "js"
                         string.Format( "alert('output:{0},return:{1}')", categoryId.ToString(), rtn.ToString()), 
                         true); 
 
                gvCategory.EditIndex = -1; 
                BindCategory(); 
        } 
 
         protected  void gvCategory_SelectedIndexChanged( object sender, EventArgs e) 
        { 
                var products = _ctx.GetProduct(( int)gvCategory.SelectedValue); 
 
                dvProduct.DataSource = products; 
                dvProduct.DataBind(); 
        } 
 
         protected  void gvCategory_RowDeleting( object sender, GridViewDeleteEventArgs e) 
        { 
                 // rtn - 用于获取存储过程的返回值(return) 
                 int rtn = _ctx.DeleteCategory(( int)gvCategory.DataKeys[e.RowIndex].Value); 
 
                Page.ClientScript.RegisterStartupScript( 
                         this.GetType(), 
                         "js"
                         string.Format( "alert('return:{0}')", rtn.ToString()), 
                         true); 
 
                gvCategory.EditIndex = -1; 
                BindCategory(); 
        } 
 
         protected  void gvCategory_RowUpdating( object sender, GridViewUpdateEventArgs e) 
        { 
                 // rtn - 用于获取存储过程的返回值(return) 
                 int rtn = _ctx.UpdateCategory( 
                        ( int)gvCategory.DataKeys[e.RowIndex].Value, 
                        ((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].Controls[0]).Text, 
                        ((TextBox)gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text); 
 
                Page.ClientScript.RegisterStartupScript( 
                         this.GetType(), 
                         "js"
                         string.Format( "alert('return:{0}')", rtn.ToString()), 
                         true); 
 
                gvCategory.EditIndex = -1; 
                BindCategory(); 
        } 
 
         protected  void gvCategory_RowEditing( object sender, GridViewEditEventArgs e) 
        { 
                gvCategory.EditIndex = e.NewEditIndex; 
                BindCategory(); 
        } 
 
         protected  void gvCategory_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e) 
        { 
                gvCategory.EditIndex = -1; 
                BindCategory(); 
        } 
}
 




     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345010 ,如需转载请自行联系原作者

相关文章
|
XML 开发框架 .NET
.NET 9 中 LINQ 新增功能实操
.NET 9 中 LINQ 新增功能实操
170 0
|
开发框架 .NET 开发工具
.NET 9 中 LINQ 新增的功能
.NET 9 中 LINQ 新增的功能
165 0
|
开发框架 前端开发 算法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
251 0
|
SQL Oracle 关系型数据库
.NET 开源快捷的数据库文档查询和生成工具
【8月更文挑战第1天】推荐几款.NET开源数据库文档工具:1. DBDocumentor,支持多类型数据库,快速生成详尽文档;2. SqlDoc,界面简洁,自定义内容与格式;3. DBInfo,强大查询功能,支持多种导出格式。这些工具有效提升文档管理效率与质量。
261 0
效率提升利器:一个在线的.NET源码查询网站
效率提升利器:一个在线的.NET源码查询网站
213 0
|
存储 关系型数据库 MySQL
Mysql存储过程查询结果赋值到变量
Mysql存储过程查询结果赋值到变量
370 0
|
存储 SQL
sql_存储过程、函数、分支、循环
sql_存储过程、函数、分支、循环
241 0
|
存储 SQL 安全
SQL 存储过程和函数的对比、变量、条件和处理程序、游标、流程控制详解+代码示例
SQL 存储过程和函数的对比、变量、条件和处理程序、游标、流程控制详解+代码示例
|
SQL 存储 数据安全/隐私保护