/*22.7
(Baseball 数据库程序)建立一个程序,它对Baseball 数据库中的Players 表执行查询,这个数据库和表包含在本章例子目录中的Databases文件夹下。在DataGridView中显示这个表的内容并增加一个文本框和一个按钮,以使用户能够根据姓氏搜索特定的球手。需包含一个卷标来标示这个文本框。单击这个按钮后应执行合适的查询。此外,还需提供一个按钮,它使用户能够回退到浏览全部的球手信息。
(修改Baseball 数据库程序)修改上题,以使用户能够找出平均击球成绩位于一个指定范围内的球手。添加一个minimumTextBox 文本框用于最小平均击球成绩( 默认为0.00),添加一个mumTextBox 文本框用于最大平均击球成绩( 默认为1.000),需包含一个卷标来标示每个文本框。添加一个按钮,用于执行从Players 表中选择行的查询,其中的BttingAverage 列应大于或等于指定的最小值,而应小于或等于指定的最大值。
*/
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
.OrderBy( 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 )
orderby 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
orderby 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; }
}
}