• LinqToSql增删改


    1.建立Users表如下:

    image

    其中userid是自动增长的.

    2.生成实体类

    新建一个LinqToSql类.如图:

    image

    3.从"服务器资源管理器"中拖出Users表,放在该dbml上.如下图:

    image

    4.查看dbml代码,结果如下,只需大致看即可:

    [Table(Name="dbo.Users")]
    public partial class Users : INotifyPropertyChanging, INotifyPropertyChanged
    {
    	
    	private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    	
    	private int _userid;
    	
    	private string _username;
    	
    	private string _userpwd;
    	
    	private System.Nullable<int> _age;
    	
        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnuseridChanging(int value);
        partial void OnuseridChanged();
        partial void OnusernameChanging(string value);
        partial void OnusernameChanged();
        partial void OnuserpwdChanging(string value);
        partial void OnuserpwdChanged();
        partial void OnageChanging(System.Nullable<int> value);
        partial void OnageChanged();
        #endregion
    	
    	public Users()
    	{
    		OnCreated();
    	}
    	
    	[Column(Storage="_userid", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    	public int userid
    	{
    		get
    		{
    			return this._userid;
    		}
    		set
    		{
    			if ((this._userid != value))
    			{
    				this.OnuseridChanging(value);
    				this.SendPropertyChanging();
    				this._userid = value;
    				this.SendPropertyChanged("userid");
    				this.OnuseridChanged();
    			}
    		}
    	}
    	
    	[Column(Storage="_username", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]
    	public string username
    	{
    		get
    		{
    			return this._username;
    		}
    		set
    		{
    			if ((this._username != value))
    			{
    				this.OnusernameChanging(value);
    				this.SendPropertyChanging();
    				this._username = value;
    				this.SendPropertyChanged("username");
    				this.OnusernameChanged();
    			}
    		}
    	}
    	
    	[Column(Storage="_userpwd", DbType="NVarChar(20)")]
    	public string userpwd
    	{
    		get
    		{
    			return this._userpwd;
    		}
    		set
    		{
    			if ((this._userpwd != value))
    			{
    				this.OnuserpwdChanging(value);
    				this.SendPropertyChanging();
    				this._userpwd = value;
    				this.SendPropertyChanged("userpwd");
    				this.OnuserpwdChanged();
    			}
    		}
    	}
    	
    	[Column(Storage="_age", DbType="Int")]
    	public System.Nullable<int> age
    	{
    		get
    		{
    			return this._age;
    		}
    		set
    		{
    			if ((this._age != value))
    			{
    				this.OnageChanging(value);
    				this.SendPropertyChanging();
    				this._age = value;
    				this.SendPropertyChanged("age");
    				this.OnageChanged();
    			}
    		}
    	}
    	
    	public event PropertyChangingEventHandler PropertyChanging;
    	
    	public event PropertyChangedEventHandler PropertyChanged;
    	
    	protected virtual void SendPropertyChanging()
    	{
    		if ((this.PropertyChanging != null))
    		{
    			this.PropertyChanging(this, emptyChangingEventArgs);
    		}
    	}
    	
    	protected virtual void SendPropertyChanged(String propertyName)
    	{
    		if ((this.PropertyChanged != null))
    		{
    			this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    		}
    	}
    }
    5.现在我们用Linq To Sql完成简易的用户管理页面.
    添加用户:
    代码如下:
            <table>
                <tr>
                    <td>
                        Name:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtname"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Password:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtPwd" TextMode="Password"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Age:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center;">
                        <asp:Button runat="server" ID="btnSave" Text="保存" BackColor="Silver" 
                            BorderStyle="None" onclick="btnSave_Click" />
                    </td>
                </tr>
            </table>
            <hr />
            <table style="200px; text-align:center">
                <asp:Repeater ID="rpt" runat="server">
                    <HeaderTemplate>
                        <tr>
                            <td>
                                用户名
                            </td>
                            <td>
                                密码
                            </td>
                            <td>
                                年龄
                            </td>
                        </tr>
                    </HeaderTemplate>
                    <ItemTemplate>                    
                        <tr>
                            <td><%# Eval("username") %></td>
                            <td><%# Eval("userpwd") %></td>
                            <td><%# Eval("age") %></td>
                        </tr>                    
                    </ItemTemplate>
                </asp:Repeater>   
              </table>

    后台:

        DataClassesDataContext context = new DataClassesDataContext(ConstInfo.Connection);
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
            }
        }
    
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Users user = new Users();
            
            user.username = txtname.Text;
            user.userpwd = txtPwd.Text;
            user.age = int.Parse(txtAge.Text);
    
            context.Users.InsertOnSubmit(user);
    
            context.SubmitChanges();
            SetBind();
    
        }
    
        void SetBind()
        {
            rpt.DataSource = context.Users;
            rpt.DataBind();
        }
    页面效果:
    image 

    删除用户:

    前台:

            <table style="200px; text-align:center">
                <asp:Repeater ID="rpt" runat="server">
                    <HeaderTemplate>
                        <tr>
                            <td>
                                用户名
                            </td>
                            <td>
                                密码
                            </td>
                            <td>
                                年龄
                            </td>
                        </tr>
                    </HeaderTemplate>
                    <ItemTemplate>                    
                        <tr>
                            <td><%# Eval("username") %></td>
                            <td><%# Eval("userpwd") %></td>
                            <td><%# Eval("age") %></td>
                            <td>
                                <asp:LinkButton runat="server" ID="lbtnDelete" OnClick="lbtnDelete_Click" CommandArgument='<%# Bind("userid") %>' Text="删除">
                                </asp:LinkButton>
                            </td>
                        </tr>                    
                    </ItemTemplate>
                </asp:Repeater> 
    后台:
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            int userid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
    
            var user = from u in context.Users
                       where u.userid == userid
                       select u;
    
            context.Users.DeleteOnSubmit(user.First<Users>());
            context.SubmitChanges();
    
            SetBind();
        }
    效果:
    image 
    修改用户:
    前台代码
            <table style="300px; text-align:center">
                <asp:Repeater ID="rpt" runat="server">
                    <HeaderTemplate>
                        <tr>
                            <td>
                                用户名
                            </td>
                            <td>
                                密码
                            </td>
                            <td>
                                年龄
                            </td>
                        </tr>
                    </HeaderTemplate>
                    <ItemTemplate>                    
                        <tr>
                            <td><%# Eval("username") %></td>
                            <td><%# Eval("userpwd") %></td>
                            <td><%# Eval("age") %> %></td>
                            <td>
                                <asp:LinkButton runat="server" ID="lbtnDelete" OnClick="lbtnDelete_Click" CommandArgument='<%# Bind("userid") %>' Text="删除">
                                </asp:LinkButton>
                                <asp:LinkButton runat="server" ID="lbtnEdit" OnClick="lbtnEdit_Click" CommandArgument='<%# Bind("userid") %>' Text="编辑">
                                </asp:LinkButton>
                            </td>
                        </tr>                    
                    </ItemTemplate>
                </asp:Repeater>
    后台代码:
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int userid = int.Parse(hiddenUserid.Value);
    
            var user = from u in context.Users
                       where u.userid == userid
                       select u;
    
            Users u1 = user.First<Users>();
    
            u1.username = txtname.Text;
            u1.age = int.Parse(txtAge.Text);
    
            context.SubmitChanges();
            SetBind();
        }
     
  • 相关阅读:
    HDU 3572 Task Schedule(拆点+最大流dinic)
    POJ 1236 Network of Schools(Tarjan缩点)
    HDU 3605 Escape(状压+最大流)
    HDU 1166 敌兵布阵(分块)
    Leetcode 223 Rectangle Area
    Leetcode 219 Contains Duplicate II STL
    Leetcode 36 Valid Sudoku
    Leetcode 88 Merge Sorted Array STL
    Leetcode 160 Intersection of Two Linked Lists 单向链表
    Leetcode 111 Minimum Depth of Binary Tree 二叉树
  • 原文地址:https://www.cnblogs.com/oneword/p/1605708.html
Copyright © 2020-2023  润新知