步步为营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 ,如需转载请自行联系原作者

相关文章
|
SQL 监控 关系型数据库
一键开启百倍加速!RDS DuckDB 黑科技让SQL查询速度最高提升200倍
RDS MySQL DuckDB分析实例结合事务处理与实时分析能力,显著提升SQL查询性能,最高可达200倍,兼容MySQL语法,无需额外学习成本。
|
SQL 存储 关系型数据库
MySQL体系结构详解:一条SQL查询的旅程
本文深入解析MySQL内部架构,从SQL查询的执行流程到性能优化技巧,涵盖连接建立、查询处理、执行阶段及存储引擎工作机制,帮助开发者理解MySQL运行原理并提升数据库性能。
|
SQL 监控 关系型数据库
SQL优化技巧:让MySQL查询快人一步
本文深入解析了MySQL查询优化的核心技巧,涵盖索引设计、查询重写、分页优化、批量操作、数据类型优化及性能监控等方面,帮助开发者显著提升数据库性能,解决慢查询问题,适用于高并发与大数据场景。
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
723 18
|
SQL 关系型数据库 MySQL
(SQL)SQL语言中的查询语句整理
查询语句在sql中占了挺大一部分篇幅,因为在数据库中使用查询语句的次数远多于更新与删除命令。而查询语句比起其他语句要更加的复杂,可因为sql是数据库不可或缺的一部分,所以即使不懂,也必须得弄懂,以上。
594 0
|
SQL 人工智能 数据库
【三桥君】如何正确使用SQL查询语句:避免常见错误?
三桥君解析了SQL查询中的常见错误和正确用法。AI产品专家三桥君通过三个典型案例:1)属性重复比较错误,应使用IN而非AND;2)WHERE子句中非法使用聚合函数的错误,应改用HAVING;3)正确的分组查询示例。三桥君还介绍了学生、课程和选课三个关系模式,并分析了SQL查询中的属性比较、聚合函数使用和分组查询等关键概念。最后通过实战练习帮助读者巩固知识,强调掌握这些技巧对提升数据库查询效率的重要性。
497 0
|
Oracle 关系型数据库 网络安全
sqlnet.ora
一、例子 1、SQLNET.AUTHENTICATION_SERVICES= (NTS)    NAMES.DIRECTORY_PATH= (TNSNAMES,HOSTNAME)    客户端就会首先在tnsnames.
1378 0
|
监控 前端开发 API
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
1021 5
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
594 7