如何在c#中从DB(* .mdb)中删除一行

时间:2009-11-26 18:04:54

标签: c# database ms-jet-ace

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

1 个答案:

答案 0 :(得分:2)

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

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

e.g。

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

希望这是有道理的。