【问题标题】:Windows Forms get data from database and display in labelWindows 窗体从数据库中获取数据并显示在标签中
【发布时间】:2019-01-06 04:44:31
【问题描述】:

我想根据用户在列表视图中选择的内容从数据库中获取数据并显示在标签中。

我将举一个使用两个列表视图执行此操作的示例,但是当我将数据发送到标签时,我不知道该怎么做。

这是我正在使用的列表视图示例(我的标签代码在此下方)

private void PopulateRecipeIngredients()
{
    string query = "SELECT a.Name FROM Ingredient a " +
        "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId " +
        "WHERE b.RecipeId = @RecipeId";
    // @ is a parameter

    using (connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(query, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {

        // whatever recipe is selected in lstRecipes box, get the id of that and pass into query above
        command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);


        // DataTable holds the data return from query
        DataTable ingredientTable = new DataTable();

        // SqlDataAdapter object adapter fills the ingredientTable DataTable object with results from query
        adapter.Fill(ingredientTable);

        // Display value of Name ex. salad
        lstIngredients.DisplayMember = "Name";

        // Id column is how we reference
        lstIngredients.ValueMember = "Id";
        // connect list box on form to data in recipeTable
        lstIngredients.DataSource = ingredientTable;
    }
}

我的代码:

  private void PopulateCourseDetails()
   {
      string query = "SELECT * FROM Course_Info WHERE Id = @CourseId";

    using (connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(query, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        command.Parameters.AddWithValue("@CourseId", lstCourses.SelectedValue);

        DataTable courseTable = new DataTable();

        adapter.Fill(courseTable);

        lblCourseId.Text = "Course_id";
        lblCourseSection.Text = "Course_section";
        lblCourseName.Text = "Course_name";
        lblCourseDay.Text = "Course_day";
        lblCourseStartTime.Text = "Course_start_time";
        lblCourseEndTime.Text = "Course_end_time";
        lblCourseProfessor.Text = "Course_professor";
        lblCourseProfessorEmail.Text = "Course_professor_email";


        lstCourses.ValueMember = "Id";

    }
}

【问题讨论】:

  • 你忘记了Id,查询应该是: string query = "SELECT a.Id, a.Name FROM Ingredient a " + "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId " + "WHERE b.RecipeId = @RecipeId";
  • 谢谢,但配方代码是我用来编写代码的示例。配方代码根据在另一个列表框中选择的内容将信息显示到列表框中。有用。我想要做的是根据在列表框中选择的内容在标签中显示信息。我不知道如何从 DataTable 对象中获取信息并进入标签。

标签: c# sql-server visual-studio winforms ado.net


【解决方案1】:
lblCourseId.Text = (string)courseTable.Rows[0]["Course_id"]

只要结果表中有一行就应该可以工作

【讨论】:

  • 我收到错误“无法将索引应用于数据表”。我用来学习如何做到这一点的配料代码有一行额外的代码,而我没有有一个 DataSource 方法
  • 你说得对,不能直接索引。见更新。 Label没有DataSource,因为它只有一个值,绑定到表上没有多大意义。
  • 您的代码有效。我没有将该方法添加到表单加载方法中,因此它没有显示出来。现在工作。谢谢。
【解决方案2】:

假设您的 DataTable 现在已正确填充,您现在有一个包含许多行的表。

首先你需要拉出第一行

var row = courseTable.Rows.FirstOrDefault();

现在您已经有了包含许多列的行。您可以通过索引或列名访问每一列。

lblCourseId.Text = row[0];

如果你想让标签保持它的标题,你可以做类似的事情

lblCourseId.Text = "Course_id: " + row[0];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多