【问题标题】:How to count a field which has the most record in the gridview and display them in a label?如何计算gridview中记录最多的字段并将它们显示在标签中?
【发布时间】:2014-02-01 19:33:01
【问题描述】:

我有两张表,分别是医生和医学史,它们彼此有关系。我使用外连接在医生表中显示医生姓名而不是医生 ID。现在我想要的是显示标签中记录最多的医生姓名。看第一张图片,因为 Jack 有 3 条记录,而 John 只有 1 条记录,我想在标签中显示 Jack。所以输出将是 GP: Jack。标签名称称为 lblGP。除了 RadioSpace 方法之外的任何其他帮助,我不知道如何编写数据集。帮忙???

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class member_viewmedicalhistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // call BindGridView
            bindGridView();

        }
    }

    private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT convert(varchar, mCheckupDate, 103) AS CheckUpDate, mReview AS Review, pat.pFirstName AS FirstName, doc.dFirstName as Doctor, ill.illnessType  from medicalhistory AS hist ";
        strCommandText += " LEFT OUTER JOIN PATIENT as pat on hist.patientid = pat.patientid ";
        strCommandText += " LEFT OUTER JOIN DOCTOR as doc on hist.doctorid = doc.doctorid ";
        strCommandText += " LEFT OUTER JOIN ILLNESS as ill on hist.illnessid = ill.illnessid ";
        strCommandText += " WHERE hist.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            GrdViewMedicalHistory.DataSource = dt;
            GrdViewMedicalHistory.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}

【问题讨论】:

    标签: c# gridview count record outer-join


    【解决方案1】:

    我的第一个建议是使用

        string strCommandText = @"SELECT convert(varchar, mCheckupDate, 103) AS CheckUpDate, mReview 
        AS Review, pat.pFirstName AS FirstName, doc.dFirstName as Doctor, ill.illnessType  from    
        medicalhistory AS hist 
         LEFT OUTER JOIN PATIENT as pat on hist.patientid = pat.patientid 
         LEFT OUTER JOIN DOCTOR as doc on hist.doctorid = doc.doctorid 
         LEFT OUTER JOIN ILLNESS as ill on hist.illnessid = ill.illnessid 
         WHERE hist.patientid = " + ID.ToString();
    

    第二个只需使用第二个数据读取器和数据表并编写查询以返回您要查找的内容,然后将其插入您的标签

    或使用 LINQ dT.Select()....First();//return string 输入代码的快捷方式

    关于无类型数据集和 linq 的文章:http://msdn.microsoft.com/en-us/library/bb399399(v=vs.110).aspx

    哦,看起来您使用 VS 应该能够自动生成类型化数据集。这将使事情变得容易得多。

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2014-06-17
      相关资源
      最近更新 更多