Search code examples
c#ado.net

Removing a row for a DataRowCollection doesn't work


I am practicing ADO.net by C# and I am writing a class to insert , delete , update , query my table Students.The problem is delete . I implement it as a DbManager class. If I invoke Delete method to delete specified row from the table Students, it execute successfully , but after invoking Exit method the row still exists in data source. How to solve this problem ?

The following is a passage from DbManager class:

  class DbManager
  {
    private DataSet dataset;
    private SqlDataAdapter adapter;

    public DbManager()
    {

        dataset = new DataSet("School");

        string queryString = @"
                               SELECT * 
                               FROM [Students]
                              ";

        var conn = Connect();            
        adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(queryString, conn);
        conn.Open();
        adapter.Fill(dataset, "Students");
        conn.Close();
    }
    
     private SqlConnection Connect()
     { 

        string connectionString = @"
                                    Data Source = .\SQLEXPRESS;
                                    Initial catalog = lgd;
                                    Integrated Security = true
                                   ";
        return new SqlConnection(connectionString);
     }

    public void Menu()
    {
       // invoke Delete Method ,then invoke Exit method to update database and exit. 
    }
    
     private void Delete()
     {
        Console.WriteLine("Enter the id you want to delete .");
        string id = Console.ReadLine();

        DataTable students = dataset.Tables["Students"];
        if (students.Columns.Contains(id))
        {
            DataRow row = students.Rows.Find(id);
            students.Rows.Remove(row);
        }

        Console.WriteLine("Delete successfully .");
     }

     private void Exit()
     {
        var builder = new SqlCommandBuilder(adapter);
        builder.GetUpdateCommand();
        adapter.Update(dataset, "Students");

        Console.WriteLine("Exit successfully !");
        Environment.Exit(0);
     }
}



    
    

Solution

  •         if (students.Columns.Contains(id)) //this is pointless btw
            {
                DataRow row = students.Rows.Find(id);
                row.Delete();
            }
    

    Marks the row as deleted but leaves it in the datatable so that the later Update operation can see it, see it is Deleted RowState and issue a DELETE SQL

    Removing it from the collection prevents the adapter ever knowing it was there

    Note that in future when you work with EF, to delete a row from the DB you DO use the pattern in your question: db.Students.Remove(someStudent); because db.Students does more closely represent the Students table in the db

    You don't need to Open/Close your connection: dataadapter knows how to do it itself

    I can't see the point of your if - if the students table in the db has an ID column, your dataset's datatable will definitely have an ID column