【问题标题】:Return multiple SQL query results to text boxes将多个 SQL 查询结果返回到文本框
【发布时间】:2015-05-17 05:45:01
【问题描述】:

我目前有三个表,Student、Less/Stu、Lessons 和 Class

使用 sql 查询,我设法使用组合框将类代码返回到文本框。现在我创建了一个 SELECT 查询,它应该返回与该类代码链接的课程中的值。由于有六节课与一个课程相关联,我希望这些行填充多个文本框。

课程代码课程名称课程学分...是课程表中的三列。所以应该有六行结果来填充文本框。我目前的代码

string conString = "DATA SOURCE HERE";
string query = @"SELECT [Lesson ID],[LessonName],[LessonCredits] from Lessons WHERE ClassID ='" + txtClassID.Text + "' ;";
SqlConnection conDateBase = new SqlConnection(conString);
SqlCommand cmdDatabase = new SqlCommand(query, conDateBase);

conDateBase.Open();
SqlDataReader myReader = cmdDatabase.ExecuteReader();

while (myReader.Read())
{
    string sLessID = myReader.GetString(myReader.GetOrdinal("Lesson ID"));
    string sLessNam = myReader.GetString(myReader.GetOrdinal("LessonName"));
    string sLessCred = myReader.GetString(myReader.GetOrdinal("LessonCredits"));

    txtLessId1.Text= sLessID;
    txtLessName1.Text = sLessNam;
    txtLessCred1.Text = sLessCred;
}

此代码仅返回课程表中与班级 ID 相关的最后一个值,我觉得我需要将查询结果放入一个循环中,然后存储到一个数组中,然后将文本框分配给数组?

【问题讨论】:

  • 您的文本框只能显示一条记录(即数据读取器中的单个循环提取的数据),在您覆盖之前的文本的下一个循环中。所以,除非你对连接字符串没问题,否则你应该选择另一种适合这种情况的控件,(ListView 或 DataGridView)
  • 在你的 sql 中使用参数。从头开始做:csharp-station.com/Tutorial/AdoDotNet/lesson06
  • 我在测试中使用了 DataGridView,但我需要从该数据网格中选择数据并将其与学生信息相结合。因此,当学生注册到班级时(通过选择当前组合框)他们选择班级 ID,这反过来会选择所有课程并将学生 ID、课程 ID 和课程名称添加到 Less/Stud 表中。这可能吗?
  • 在这种情况下,给定一个classid,您可以选择多行。所以你使用的文本框可以是一个列表框或其他东西,填充每一列。而不是你的 textbox.text = value,它应该是 listbox.add。
  • 你确实有循环。您不能将数组分配给 TextBox。

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


【解决方案1】:

当然它只显示表格的最后一行。因为你正在经历一个循环,循环在最后一行结束。对于你的要求,我会给你两个选择..

1。创建两个按钮“上一课”和“下一课”,并在单击此按钮的同时移动行

在表单类中添加两个变量,以便在 button_click 事件中访问它们

用于存储课程数据的 System.Data.DataTable 变量和

一个用于存储索引的 int 变量

DataTable Lessons;
    int lessonIndex = 0;
    private void combo_DropDown(object sender, EventArgs e)
    {
        SqlConnection conDateBase = new SqlConnection();
        conDateBase.ConnectionString = connectionString;
        conDateBase.Open();

        string query = "SELECT [Lesson ID],[LessonName],[LessonCredits] from Lessons WHERE ClassID = @CLASS_ID";
        SqlCommand cmdDatabase = new SqlCommand(query, conDateBase);
        cmdDatabase.Parameters.Add("@CLASS_ID", System.Data.SqlDbType.VarChar).Value = txtClassID.Text;
        SqlDataAdapter adapter = new SqlDataAdapter(cmdDatabase);
        DataSet dSet = new System.Data.DataSet();
        adapter.Fill(dSet, "Lessons");
        Lessons = dSet.Tables["Lessons"];
        lessonIndex = 0;
        refreshValues();
    }

    private void btnPreviousLesson_Click(object sender, EventArgs e)
    {
        if (lessonIndex > 0 && Lessons != null && Lessons.Rows.Count > 0)
        {
            lessonIndex--;
            refreshValues();
        }
    }

    private void btnNextLesson_Click(object sender, EventArgs e)
    {
        if (Lessons != null && Lessons.Rows.Count > 0 && lessonIndex < Lessons.Rows.Count - 1)
        {
            lessonIndex++;
            refreshValues();
        }
    }

    private void refreshValues()
    {
        txtLessID1.Text = Lessons.Rows[lessonIndex]["Lesson ID"].ToString();
        txtLessName1.Text = Lessons.Rows[lessonIndex]["LessonName"].ToString();
        txtLessCred1.Text = Lessons.Rows[lessonIndex]["LessonCredits"].ToString();
    }

.

2。在 DataGridView 上显示课程数据

combo_DropDown 事件的代码与上面相同,跳过最后两行和其他事件/函数并添加这些。

  DataSet dSet = new System.Data.DataSet();
  adapter.Fill(dSet, "Lessons");
  Lessons = dSet.Tables["Lessons"];
  dataGridView1.DataSource = Lessons;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2010-09-25
    • 2020-10-23
    相关资源
    最近更新 更多