如何在我的表中添加删除链接以删除数据库中的记录?

时间:2016-02-11 07:10:27

标签: c# asp.net ms-access-2010

我本质上是尝试添加一些可点击的方式来删除或编辑我的表中的条目。这些条目都保存在填充表的访问数据库中。我最大的问题是我不确定如何编程可点击方法,以便它保存我尝试编辑/删除的用户名。任何建议将不胜感激。

相关代码:

main.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="main" %>
<%@ Reference Control="~/UserInfoBoxControl.ascx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder runat="server" ID="phUserInfoBox" />

    </div>

        <asp:Button id="login" runat="server"  Text="edit profile" onclick="btnRegister_click" />
        <asp:Button id="create" runat="server"  Text="logout" onclick="btnLogout_click" />
    </form>
</body>
</html>

main.aspx.cs

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class main : System.Web.UI.Page
{
    private OleDbConnection bookConn;
    private OleDbCommand oleDbCmd = null;
    private String connParam = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='F:\test\Database21.accdb'; Persist Security Info=False;";

    protected void Page_Load(object sender, EventArgs e)
    {
        {
            OleDbDataReader reader;
            bookConn = new OleDbConnection(connParam);
            bookConn.Open();
            oleDbCmd = new OleDbCommand("SELECT user_name, fname, lname  FROM profiles",bookConn);
            reader = oleDbCmd.ExecuteReader();
            while (reader.Read())
            {
                UserInfoBoxControl MyUserInfoBoxControl =(UserInfoBoxControl)LoadControl("UserInfoBoxControl.ascx");
                phUserInfoBox.Controls.Add(MyUserInfoBoxControl);
                MyUserInfoBoxControl.UserName = reader.GetString(0);
                MyUserInfoBoxControl.FirstName = reader.GetString(1);
                MyUserInfoBoxControl.LastName = reader.GetString(2);
            }
            bookConn.Close();
        }
    }

    protected void btnRegister_click(object sender, EventArgs e)
    {
        Response.Redirect("myprofile.aspx");
    }

    protected void btnLogout_click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
}

UserInfoBoxControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserInfoBoxControl.ascx.cs" Inherits="UserInfoBoxControl" %>



<table>
  <tr>
    <th>UserName</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr>
    <td><%= this.UserName %>  </td>
    <td><%= this.FirstName %></td>
    <td><%= this.LastName %></td>

  </tr>

</table>

UserInfoBoxControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

    private string userName;
    private string fname;
    private string lname;


    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }
    public string FirstName
    {
        get { return fname; }
        set { fname = value; }
    }
    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

2 个答案:

答案 0 :(得分:0)

您需要使用模型绑定来定义要在网格中显示的类类型。

http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/updating-deleting-and-creating-data

然后,您将在gridview中指定更新/删除/ etc等记录的命令。在这些方法中,您将获得适用的信息并删除。 防爆网格:


<asp:GridView runat="server" ID="studentsGrid"
    ItemType="ContosoUniversityModelBinding.Models.Student" DataKeyNames="StudentID"
    SelectMethod="studentsGrid_GetData"
    UpdateMethod="studentsGrid_UpdateItem" DeleteMethod="studentsGrid_DeleteItem"
    AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"  
    AutoGenerateColumns="false">

然后注意如何传入id:

public void studentsGrid_DeleteItem(int studentID)
{
    using (SchoolContext db = new SchoolContext())
    {
        var item = new Student { StudentID = studentID };
        db.Entry(item).State = EntityState.Deleted;
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            ModelState.AddModelError("", 
              String.Format("Item with id {0} no longer exists in the database.", studentID));
        }
    }
}

如果这是您第一次使用网络表单,我会诚实地看看MVC路由并使用MVC Scaffolding。您不能使用Access数据库,但实体框架需要像上面那样直接使用OLEDB代码(如果是选项则不使用访问权限)

答案 1 :(得分:0)

我最终搞清楚了。我所做的就是在Web用户控件框中添加一个按钮,在后面的代码中创建了一个新的会话变量。

UserInfoBoxControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserInfoBoxControl.ascx.cs" Inherits="UserInfoBoxControl" %>



<table>
  <tr>
    <th>UserName</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <tr>
    <td><%= this.UserName %>   </td>
    <td><%= this.FirstName %></td>
    <td><%= this.LastName %>   <asp:Button id="login" runat="server"  Text="edit user" onclick="btnEdit_click" /></td>


  </tr>


</table>

UserInfoBoxControl.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

    private string userName;
    private string fname;
    private string lname;


    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }
    public string FirstName
    {
        get { return fname; }
        set { fname = value; }
    }
    public string LastName
    {
        get { return lname; }
        set { lname = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnEdit_click(object sender, EventArgs e)
    {
        Session["UserNameMod"] = userName;

        Response.Redirect("userinfo.aspx");
    }
}