.NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除

步步为营VS 2008 + .NET 3.5(9) - DLINQ(LINQ to SQL)之执行SQL语句的添加、查询、更新和删除



作者:webabcd


介绍
以Northwind为示例数据库,DLINQ(LINQ to SQL)之执行指定SQL语句的添加操作、查询操作、更新操作和删除操作


示例
SQL.aspx

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SQL.aspx.cs"
        Inherits="LINQ_DLINQ_SQL" Title="执行SQL语句的添加、查询、更新和删除" %>

<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>

 

SQL.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_SQL : System.Web.UI.Page
InBlock.gif{
InBlock.gif         // 实例化一个NorthwindDataContext(DataContext)
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                 // 使用NorthwindDataContext对象的ExecuteQuery<T>()方法,通过指定的SQL语句返回指定对象的集合
InBlock.gif                var categories = _ctx.ExecuteQuery<Categories>( @"select * from categories");
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                 // 使用NorthwindDataContext对象的ExecuteCommand()方法,执行指定的SQL语句
InBlock.gif                _ctx.ExecuteCommand( @"insert into categories (categoryname, description) values ({0}, {1})",
InBlock.gif                                                        txtCategoryName.Text,
InBlock.gif                                                        txtDescription.Text);
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                 // 使用NorthwindDataContext对象的ExecuteQuery<T>()方法,通过指定的SQL语句返回指定对象的集合
InBlock.gif                var products = _ctx.ExecuteQuery<Products>( @"select * from products where productid={0}", 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                 // 使用NorthwindDataContext对象的ExecuteCommand()方法,执行指定的SQL语句(调用存储过程)
InBlock.gif                _ctx.ExecuteCommand( @"exec sp_DeleteCategory {0}", gvCategory.DataKeys[e.RowIndex].Value);
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                 // 使用NorthwindDataContext对象的ExecuteCommand()方法,执行指定的SQL语句
InBlock.gif                _ctx.ExecuteCommand( @"update categories set categoryname={0}, description={1} where categoryid={2}",
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                                                        gvCategory.DataKeys[e.RowIndex].Value);
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” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/345008

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值