C # University Course (fifth edition) After class exercise 22.7 modifying the baseball database program

Source: Internet
Author: User

/*22.7
(Baseball database program) establishes a program that executes queries against players tables in the baseball database, which are included in the Databases folder in the example directory in this chapter. Display the contents of this table in DataGridView and add a text box and a button to allow the user to search for a specific player based on their last name. You need to include a volume label to mark the text box. When you click this button, the appropriate query should be executed. In addition, you need to provide a button that allows the user to fall back to browsing all of the golfers ' information.
(Modify the Baseball database program) to modify the title so that the user can find the average batting score in a specified range of golfers. Add a Minimumtextbox text box for the minimum average batting score (default is 0.00), add a Mumtextbox text box for the maximum average batting score (default is 1.000), and include a volume label to mark each text box. Adds a button that executes a query that selects rows from the players table, where the Bttingaverage column should be greater than or equal to the specified minimum value and should be less than or equal to the specified maximum value.
*/
Using System;
Using System.Data;
Using System.Data.Entity;
Using System.Data.Entity.Validation;
Using System.Linq;
Using System.Windows.Forms;
Namespace Baseballplayers
{
public partial class Baseballplayers:form
{
Public Baseballplayers ()
{
InitializeComponent ();
}
Private baseballexercises.baseballentities dbcontext = null;
private void Refreshplayers ()
{
if (dbcontext! = null)
DbContext. Dispose ();
DbContext = new Baseballexercises.baseballentities ();
DbContext. Players
. (Entry = entry. LastName)
. ThenBy (Entry = entry. FirstName)
. Load ();
Playerbindingsource.datasource = DbContext. players.local;
Playerbindingsource.movefirst ();
Findtextbox.clear ();
}
private void Baseballplayers_load (object sender, EventArgs e)
{
Refreshplayers ();
}
private void Findbutton_click (object sender, EventArgs e)
{
var lastnamequery =
From address in DbContext. Players
where address. Lastname.startswith (Findtextbox.text)
Address. LastName, address. FirstName
Select Address;
Playerbindingsource.datasource = Lastnamequery.tolist ();
Playerbindingsource.movefirst ();
bindingnavigatoraddnewitem.enabled = false;
bindingnavigatordeleteitem.enabled = false;
}
private void Browseallbutton_click (object sender, EventArgs e)
{
Bindingnavigatoraddnewitem.enabled = true;
Bindingnavigatordeleteitem.enabled = true;
Refreshplayers ();
}
private void Playerbindingnavigatorsaveitem_click (
Object sender, EventArgs e)
{
Validate ();
Playerbindingsource.endedit ();
Try
{
DbContext. SaveChanges ();
}
catch (Dbentityvalidationexception)
{
MessageBox.Show ("Columns cannot be empty",
"Entity Validation Exception");
}
Refreshplayers ();
}
private void Averagebutton_click (object sender, EventArgs e)
{
Decimal minimum = Convert.todecimal (Minimumtextbox.text);
Decimal maximum = Convert.todecimal (Maximumtextbox.text);
var averagequery =
From player in DbContext. Players
where player. Battingaverage >= Minimum
&& player. Battingaverage <= Maximum
by player. playerID
Select Player;
Playerbindingsource.datasource = Averagequery.tolist ();
Playerbindingsource.movefirst ();
bindingnavigatoraddnewitem.enabled = false;
bindingnavigatordeleteitem.enabled = false;
}
}
}
Namespace Baseballexercises
{
Using System;
Using System.Data.Entity;
Using System.Data.Entity.Infrastructure;
public partial class Baseballentities:dbcontext
{
Public Baseballentities ()
: Base ("Name=baseballentities")
{
}
protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
throw new Unintentionalcodefirstexception ();
}
Public dbset<player> Players {get; set;}
}
}
Namespace Baseballexercises
{
Using System;
Using System.Collections.Generic;
public partial class Player
{
public int playerID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
Public decimal Battingaverage {get; set;}
}
}

C # University Course (fifth edition) After class exercise 22.7 modifying the baseball database program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.