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

发布于 2024-08-13 01:49:58 字数 170 浏览 3 评论 0原文

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

I have an access DB (.mdb) named: Programs, with one table named: Data. By using the DataGridView I present the data from the table Data. I want to delete a row from the DataGridView and from the DB during runtime. Does anyone know how to do that (using C#)?
Another question I have is, who can i run queries on my DB?
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め七分饶幸 2024-08-20 01:49:58

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

之后,只需填充网格,例如 select * 来自 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;
    }

希望这是有道理的。

Very simple.
I suppose in your datagrid you have a check box by choosing which you will be able to choose the rows that you want to delete. And assuming you have a submit button, so after choosing the rows click on the submit button. In the button's click event call the Delete query say delete from tblname where id = @id [@id is the id that will be passed from your grid]

After that just populate the grid e.g. select * from tblname

e.g.

Aspx code

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

Submit button cilck event's code

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

Hope this makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文