我想在 GridView 中显示结果,但我无法这样做。我正在尝试将我的 C# windows 窗体应用程序连接到我在 SQL 中创建的数据库。请告诉我是否还有其他需要在课堂上解决的问题。我没有收到任何错误,但是当我单击“查看所有运动员”时仍然没有得到任何结果。
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();
}
有什么建议吗?