首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从DB (*.mdb)中删除c#中的行

如何从DB (*.mdb)中删除c#中的行
EN

Stack Overflow用户
提问于 2009-11-26 18:04:54
回答 1查看 6.3K关注 0票数 0

我有一个名为:.mdb的access DB ( an )程序,其中有一个名为: Data的表。通过使用DataGridView,我将显示表数据中的数据。我想在运行时从DataGridView和DB中删除一行。有人知道如何做到这一点(使用C#)吗?

我的另一个问题是,我可以在我的DB上运行查询吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2009-11-27 05:25:22

非常简单。我想,在数据集中,通过选择要删除的行,可以选择哪个行,从而有一个复选框。假设您有一个submit按钮,那么在选择行之后,单击submit按钮。在按钮的单击事件调用中,删除查询假设delete from tblname where id = @id @id是将从网格传递的id

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

例如:

Aspx代码

代码语言:javascript
复制
<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事件的代码

代码语言:javascript
复制
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;
    }

希望这是有意义的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1805008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档