将数据库中的多个数据项检索到一个标签中

时间:2017-01-23 03:55:10

标签: c# asp.net sql-server

我想从数据库中检索多个值并在一个标签上显示。

我的数据库设计目前是:

courseid  scholarshipid  course
--------------------------------
    1           1        maths
    2           1        english
    3           1        science

scholarshipid   schName    
-----------------------
      1         moon 
      2         start
      3         light

我想将此信息检索到一个标签中,例如

Maths, English, Science

然而,现在我设法检索其中一个只是数学。

这是我的代码背后代码:

protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL getRecord = new courseBLL();
    addcourse courseRetrieve = new addcourse();
    courseRetrieve = getRecord.getCourseDetail(selectedIdtoPass);

    lbCourse.Text = courseRetrieve.course1 + "," + courseRetrieve.course1;
}

在我的课程中

public addcourse getCourseDetail(int idcourse)
{
        addcourse userObj = new addcourse();
        courseDAL objtoPass = new courseDAL();
        userObj = objtoPass.displayCourseInfo(idcourse);
        return userObj;
}

在我的DAL中

public addcourse displayCourseInfo(int idcourse)
{
        string strCommandText = "Select course from course where schokarshipid = @schokarshipid";

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", idcourse);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();
        addcourse userObj = new addcourse();

        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["courseID"].ToString());

            string course = myReader["course"].ToString();

            userObj = new addcourse(sID, course);
        }

        myReader.Close();
        return userObj;
}

编辑:

public addcourse displayCourseInfo(int idcourse)
{
        var courses = new List<addcourse>();
        string strCommandText = "Select statement here"

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", idcourse);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["scholarshipid"].ToString());

            string course = myReader["course"].ToString();

            courses.Add(new addcourse(sID,course));
        }

        myReader.Close();

        return courses;
}

我为此目的删除了一些字段,以便我不会混淆你

2 个答案:

答案 0 :(得分:1)

您应该从List<addcourse>getCourseDetail()返回displayCourseInfo,然后如果您的addCourse拥有Course属性,则可以执行此操作:

lbCourse.Text = string.Join(", ",  courses.Select(x => x.Course));

如果属性名称不同,请将x.Course更改为该名称。

所以在你的读者中你会这样做

    var courses = new List<addcourse>();
    while (myReader.Read())
    {
        int sID = Convert.ToInt32(myReader["courseID"].ToString());

        string course = myReader["course"].ToString();


        courses.Add( new addcourse(sID, course)) ;
   } 
   return courses;

你的查询也错了:

Select  course, from course ss inner join  where courseID =@courseID

所以你应该修复它并确保你获得所需的列,因为它现在只有course列并且它有语法错误。使用management studio直接对数据库运行查询。一旦你的查询工作,复制粘贴它并用你现在的方式用参数替换where参数。

修改

看来你仍然坚持这个,所以我会给你一个更详细的答案;但是,你应该真正研究一些基本的编程概念。无论如何,这里是:

您的代码背后:

protected void btnShowDetails_Click(object sender, EventArgs e)
{
    string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
    // Make sure this is the scholarshipid or the whole thing will
    // not work.
    int selectedIdtoPass = Convert.ToInt32(Selectedid);

    courseBLL courseRetriever = new courseBLL();

    // Remember you will get a list here
    List<addcourse> courses = courseRetriever.getCourseDetail(selectedIdtoPass);

    // This will take the list and separate the course property with a comma and space
    var courseNames = string.Join(", ", courses);

    // Now assign it to the text property
    lbCourse.Text = courseNames;
}

你的BLL:

// See here you are retuning a list
// But you need to pass the scholarshipid so you can get the courses for that scholarshipid
public List<addcourse> GerCourses(int scholarshipId)
{
        courseDAL courseRetriever = new courseDAL();

        // Get the list
        List<addcourse> courses = courseRetriever.GetCourses(scholarshipId);
        return courses;
}

你的DAL:

// See here you are retuning a list
// But you need to pass the scholarshipid so you can get the courses for that scholarshipid
public List<addcourse> GetCourses(int scholarshipId)
{
        // Make sure your query is correct, see you have spelling mistakes
        // it should be scholarshipid not schokarshipid. it has to match the column in your database
        // Make sure your table is called course and it has 3 columns: 1. courseid 2. course
        // 3. scholarshipid or this query will not work
        string strCommandText = "Select courseid, course from course where scholarshipid = @scholarshipId";

        SqlCommand cmd = new SqlCommand(strCommandText, conn);
        cmd.Parameters.AddWithValue("@scholarshipid", scholarshipId);

        conn.Open();

        SqlDataReader myReader = cmd.ExecuteReader();

        // create a list so we can hold all the courses
        List<addcourse> userObjs = new List<addcourse>();

        // This will read one row at a time so keep reading until there are no more records
        while (myReader.Read())
        {
            int sID = Convert.ToInt32(myReader["courseid"].ToString());

            string course = myReader["course"].ToString();

            addcourse userObj = new addcourse(sID, course);

            // add it to the list
            userObjs.Add(userObj);
        }

        myReader.Close();

        // return the list
        return userObjs;
}

还有一些提示:

  1. 为您的方法提供更有意义的名称。看看我给他们的名字GetCourses,现在看起来像英文,很容易理解。
  2. 您的班级addcourse应该被称为Courseaddcourse听起来像是一个“添加课程”的动作,如果你要添加课程,这可能是一个方法的好名字。对类名和方法名也使用 Pascal 表示法。使用 Camel casing 作为参数,参数和局部变量。
  3. 了解如何使用Visual Studio调试器。在Visual Studio Debugger上观看一些YouTube视频。

答案 1 :(得分:0)

创建一个for循环,然后将其添加到您正在使用的变量