Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Delete All

How to delete all data in sql in one click?
This is most likely related with my pending question which is now still searching for the solution. The XAML.cs here are as follows

StudentDomainContext objctx;

private void btndelete_Click(object sender, RoutedEventArgs e)
{
objctx = new StudentDomainContext();
var query = objctx.GetStudentsQuery();
objctx.Load(query, Datadelete, null);
}

private void Datadelete(LoadOperation<student> lo)
{
student st = lo.Entities.First();
objctx.student.Remove(st);
try
{
objctx.SubmitChanges();
MessageBox.Show("Data deleted!")
}
catch (Exception ex)
{
MessageBox.Show("Data deletion failed" +ex.Message);
}

}

The Class query are as follos......
public IQueryable<student> GetStudents()
{
return this.ObjectContext.students;
}

The codes are working but when I execute, only one data will be deleted. What I want here is to delete all datas inside my database concerning student Table. Please Help!
0
Whing Dela Cruz
Asked:
Whing Dela Cruz
  • 4
  • 3
2 Solutions
 
angus_young_acdcCommented:
It is because you are only selecting the first value on this line: student st = lo.Entities.First();

Try changing it to lo.Entities.All();
0
 
angus_young_acdcCommented:
Or change the query to DELETE FROM [YourStudentTableName]

If there is no Where clause then it will delete everything in that table.
0
 
Whing Dela CruzAuthor Commented:
Hi! I tried this lo.Entities.All(); but it doesn't work! Can you provide me how to change the query?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
angus_young_acdcCommented:
var query = objctx.GetStudentsQuery();


The above line should be returning your query (from what I can work out from the amount of code), within the GetStudentsQuery method you should see what is happening and be able to update it accordingly.
0
 
Whing Dela CruzAuthor Commented:
Hi! I'm trying but it does'nt work...
0
 
Whing Dela CruzAuthor Commented:
Hi! Everyone, What I did here is to put something Loop... But I dought if it is good I dea because I feel that there's something delay of the Looping. Any Idea Please....

private void Datadelete(LoadOperation<student> lo)
{
foreach (student st in lo.Entities)
objctx.student.Remove(st);
try
{
objctx.SubmitChanges();
MessageBox.Show("Data deleted!")
}
catch (Exception ex)
{
MessageBox.Show("Data deletion failed" +ex.Message);
}

}

 Please tell me if there is another solution aside from this....
0
 
Whing Dela CruzAuthor Commented:
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now