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




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

相关文章
|
27天前
|
SQL
sql语句加正则 简化查询
sql语句加正则 简化查询
16 0
sql语句加正则 简化查询
|
1月前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
15 0
|
6天前
|
SQL 关系型数据库 数据库
SQL 42501: Postgresql查询中的权限不足错误
SQL 42501: Postgresql查询中的权限不足错误
|
7天前
|
SQL 分布式计算 大数据
MaxCompute操作报错合集之在sql 里嵌套查询时,查询条件带有instr时报错,如何解决
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
|
12天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
23天前
|
SQL 运维 监控
面经:Presto/Trino高性能SQL查询引擎解析
【4月更文挑战第10天】本文深入探讨了大数据查询引擎Trino(现称Trino)的核心特性与应用场景,适合面试准备。重点包括:Trino的分布式架构(Coordinator与Worker节点)、连接器与数据源交互、查询优化(CBO、动态过滤)及性能调优、容错与运维实践。通过实例代码展示如何解释查询计划、创建自定义连接器以及查看查询的I/O预期。理解这些知识点将有助于在面试中脱颖而出,并在实际工作中高效处理数据分析任务。
50 12
|
28天前
|
SQL 关系型数据库 MySQL
sql查询指定日期前n天数据
sql查询指定日期前n天数据
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
45 0
|
11天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
18 0
|
2月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
32 0