1. 程式人生 > >C#大學課程(第五版)課後習題22.7修改Baseball 數據庫程序

C#大學課程(第五版)課後習題22.7修改Baseball 數據庫程序

個數 chan refresh sage tab ges initial IE dispose

/*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; }
}
}

C#大學課程(第五版)課後習題22.7修改Baseball 數據庫程序