如何在列表框中显示查询结果?

时间:2017-11-28 10:41:19

标签: c# sql database wpf tsql

我有一个实体AuthorId(int)的数据库(AuthorId是主键),Name(nvarchar)和Nationality(nvarchar)。我还有一个列表框,它显示该数据库中的名称,以及三个文本框,我想在列表框中显示所选项目的ID,名称和国籍,每个文本框一个。凭借我现在拥有的内容,我收到以下错误:

  

将varchar值转换为数据类型int时转换失败。

以下是我如何获取数据并将其显示在列表框中:

public void GetAuthorData()
{
    string connectionString = //connection string here
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();
        var query = "SELECT * FROM Author";
        using (SqlCommand cmd = new SqlCommand(query, con))
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Author a = new Author();
                a.Id = reader.GetInt32(0);
                a.Name = reader.GetString(1);
                a.Nationality = reader.GetString(2);

                AuthorListBox.Items.Add(a);
            }
        }
    }
}

以下是我在文本框中显示数据的方式:

private void AuthorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DeleteAuthortButton.IsEnabled = true;
    SaveChangesButton.IsEnabled = true;

    var aa = sender as ListBox;
    if (aa.SelectedItem != null)
    {
       var author = aa.SelectedItem.ToString();
        string connectionString = //connection string here
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            var query = "SELECT * FROM Author where Name = '" + author + "'";
            using (SqlCommand cmd = new SqlCommand(query, con))
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Author a = new Author();
                    a.Id = reader.GetInt32(0);
                    a.Name = reader.GetString(1);
                    a.Nationality = reader.GetString(2);

                    IdTextBox.Text = a.Id.ToString();
                    AuthorNameTextBox.Text = a.Name;
                    NationalityTextBox.Text = a.Nationality;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您无需检索数据库数据。有关作者的所有信息都在列表中。

    private void AuthorListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DeleteAuthortButton.IsEnabled = true;
        SaveChangesButton.IsEnabled = true;

        var aa = sender as ListBox;
        if (aa.SelectedItem != null)
        {
           var author = aa.SelectedItem as Author;

           IdTextBox.Text = author.Id.ToString();
           AuthorNameTextBox.Text = author.Name;
           NationalityTextBox.Text = author.Nationality; 
        }
    }

答案 1 :(得分:0)

Sql Number没有必要对应.Net int

      a.Id = reader.GetInt32(0); // May fail even if Id is Number

尝试使用Convert

      // Whatever Author.Id is 
      // (e.g. Number(10) which will be mapped to Int64, not int or
      //       Number(30) which can be mapped to String), 
      // try convert it into `Int32 == int`
      a.Id = Convert.ToInt32(reader[0]);
      a.Name = Convert.ToString(reader[1]);
      a.Nationality = Convert.ToString(reader[2]);