結果を GridView に表示したいのですが、表示できません。SQL で作成したデータベースに C# Windows フォーム アプリケーションを接続しようとしています。それを修正するためにクラスに入れる必要があるものが他にあるかどうか教えてください。エラーは発生していませんが、[すべてのアスリートを表示] をクリックしても結果が得られません。
namespace Olympics
{
public partial class Form1 : Form
{
private string connectionString;
public Form1()
{
InitializeComponent();
connectionString = "Data Source=ddb04;Initial Catalog=Vikram;Integrated Security=SSPI;";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class Athlete
{
private string Athlete_Firstname;
private string Athlete_Lastname;
private string Athlete_ID;
private string countryname;
public Athlete()
{
}
public Athlete(string firstname, string lastname, string ID, string country)
{
Athlete_Firstname = firstname;
Athlete_Lastname = lastname;
Athlete_ID = ID;
countryname = country;
}
public string firestname
{
get { return Athlete_Firstname; }
set
{
Athlete_Firstname = value;
}
}
public string lastname
{
get { return Athlete_Lastname; }
set
{
Athlete_Lastname = value;
}
}
public string ID
{
get { return Athlete_ID; }
set
{
Athlete_ID = value;
}
}
public string country
{
get { return countryname; }
set
{
countryname = value;
}
}
}
private void seeAthletesButton_Click(object sender, EventArgs e)
{
//here I access my project's connectionm string...
//string sql = "select Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country from Athlete;";
//here I create new SQLConnection Object...
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//here I create new SQLCommand Object.
using (SqlCommand command = new SqlCommand("select Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country from Athlete;", connection))
{
//command.Open();
SqlDataReader reader = command.ExecuteReader();
List<Athlete> atheletes = new List<Athlete>();
while (reader.Read())
{
string atheleteFirstName = reader.GetString(0); // Athlete_Firstname string
string atheleteLastName = reader.GetString(1); // Athlete_Lastname string
string atheleteId = reader.GetString(2); // Athlete_ID int
string atheleteCountry = reader.GetString(3);
Athlete athelete = new Athlete();
athelete.firestname = atheleteFirstName;
athelete.lastname = atheleteLastName;
athelete.ID = atheleteId;
athelete.country = atheleteCountry;
atheletes.Add(athelete); // List of Athelete objects populated with values from database
}
DataGridView atheleteDataGRidView = new DataGridView();
atheleteDataGRidView.DataSource = atheletes;
}
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void saveButton_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string sql = "insert into Athlete (Athlete_Firstname, Athlete_Lastname, Athlete_ID, Country) values (@firstName, @lastName, @athleteId, @country);";
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.Add("@firstName", SqlDbType.VarChar);
command.Parameters["@firstName"].Value = firstName.Text;
command.Parameters.Add("@lastName", SqlDbType.VarChar);
command.Parameters["@lastName"].Value = lastName.Text;
command.Parameters.Add("@athleteId", SqlDbType.VarChar);
command.Parameters["@athleteId"].Value = athleteId.Text;
command.Parameters.Add("@country", SqlDbType.VarChar);
command.Parameters["@country"].Value = countryName.Text;
command.ExecuteNonQuery();
connection.Close();
}
なにか提案を?