1

我想在 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();
    }

有什么建议吗?

4

2 回答 2

2

你需要写

atheleteDataGRidView.DataSource = atheletes;
atheleteDataGRidView.DataBind();

在私人无效seeAthletesButton_Click(对象发送者,EventArgs e)

于 2012-09-14T21:29:46.223 回答
0

除了 Oedum 所说的之外,您正在访问 ID,当它不能被转换为字符串时,它会SqlDataReader.GetString抛出一个。InvalidCastException但是您自己评论说它的 DataType 是int.

不执行任何转换;因此,检索到的数据必须已经是一个字符串。

所以这可能更好:

string atheleteId = reader.GetInt32(2).ToString(); // Athlete_ID int
于 2012-09-14T21:34:33.890 回答