【问题标题】:How to delete a row from DB (*.mdb) in c#如何在 C# 中从 DB (*.mdb) 中删除一行
【发布时间】:2009-11-26 18:04:54
【问题描述】:

我有一个名为:Programs 的访问数据库 (.mdb),其中有一个名为:Data 的表。通过使用 DataGridView,我展示了数据表中的数据。我想在运行时从 DataGridView 和数据库中删除一行。有谁知道怎么做(使用 C#)?
我的另一个问题是,我可以在我的数据库上运行谁的查询?
谢谢!

【问题讨论】:

    标签: c# database ms-jet-ace


    【解决方案1】:

    非常简单。 我想在您的数据网格中,您有一个复选框,您可以通过选择哪个复选框来选择要删除的行。并假设您有一个提交按钮,因此在选择行后单击提交按钮。在按钮的单击事件中调用删除查询说delete from tblname where id = @id [@id 是将从您的网格传递的 id]

    之后只需填充网格,例如select * from tblname

    例如

    Aspx 代码

    <asp:GridView runat="server" ID="gvTest" Width="100%" AutoGenerateColumns="false">
          <Columns>
          <asp:TemplateField HeaderText="Select"  ItemStyle-HorizontalAlign="Center"  ItemStyle-Width="5%">
            <ItemTemplate>
             <asp:CheckBox ID="chkDel" runat="server" />
            </ItemTemplate> 
           </asp:TemplateField>
           <asp:BoundField  HeaderText="RegID" DataField="RegID" ItemStyle-Width="10%">
            <ItemStyle HorizontalAlign="Left"/>
           </asp:BoundField>        
           <asp:TemplateField HeaderText="Name" ItemStyle-Width="22%" ItemStyle-HorizontalAlign="Left">
           <ItemTemplate>
           <asp:Label ID="lblName" runat="server" Width="100%" Text= '<%# DataBinder.Eval(Container.DataItem, "UserName")%>'></asp:Label>
           </ItemTemplate>
           </asp:TemplateField>          
          </Columns>
         </asp:GridView>
    
    <br>
    
    <asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click"></asp:Button>
    

    提交按钮cilck事件代码

    protected void btnSubmit_Click(object sender, EventArgs e)
        {
            for (int count = 0; count < gvTest.Rows.Count; count++ )
            {
                CheckBox chkSelect = (CheckBox)gvTest.Rows[count].FindControl("chkDel");
                if (chkSelect != null)
                {
                    if (chkSelect.Checked == true)
                    {
                        //Receiveing RegID from the GridView
                        int RegID = Int32.Parse((gvTest.Rows[count].Cells[1].Text).ToString());
    
                        object result = DeleteRecord(RegID); //DeleteRecord Function will delete the record                   
                    }
                }
            }
            PopulateGrid(); //PopulateGrid Function will again populate the grid
        }
    
       public void DeleteRecord(int RegId)
        {
            string connectionPath = "Data Source=<your data source>;Initial Catalog=<db name>;Integrated Security=True;userid='test';pwd='test'";
            string command = "";
        SqlConnection connection = new SqlConnection(@connectionPath);
            command = "delete from tblname where id = " +  RegId 
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(command, connection);
            cmd.ExecuteNonQuery();
        }
        catch (SqlException sqlExcep) {} 
        finally
         {
            connection.Close();
         }
        }
    
        public void PopulateGrid()
        {
    
            DataTable dt = new DataTable();
        dt = GetRecord();
        if(dt !=null && dt.rows.count>0)
        {
                 gvTest.DataSource = dt;
                 gvTest.DataBind();
        }
    
        }
    
        public DataTable GetRecord()
        {
           string connectionPath = "Data Source=<your data source>;Initial Catalog=<db name>;Integrated Security=True;userid='test';pwd='test'";
            string command = "";
            SqlDataAdapter adapter = new SqlDataAdapter();          
        SqlConnection connection = new SqlConnection(@connectionPath);
        command = "Select * from tblname" ;
        connection.Open();
        SqlCommand sqlCom = new SqlCommand(command, connection);
        adapter.SelectCommand = sqlCom;
        DataTable table = new DataTable();  
        adapter.Fill(table);
        return table;
        }
    

    希望这是有道理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2017-03-30
      • 2018-07-23
      • 1970-01-01
      • 2013-06-17
      • 2020-09-07
      • 1970-01-01
      相关资源
      最近更新 更多