如何在使用并集和显示计数到标签时使用计数并全选

时间:2019-03-13 14:05:56

标签: c# sql select count union-all

我想寻求帮助。我在条件条件下使用所有联合,我的查询将显示在datagridview中。我的问题是,是否有一种方法可以对生成的行进行计数,并在datagridview下面的标签或文本框中显示计数?这是我的代码:

string query = @"Select * FROM
                    (
                        SELECT date AS [Date],b.fname+' '+b.mname+' '+b.lname AS Person,
                        b.pk+ ', '+b.by+', '+b.my AS Address, b.sex,
                        'PoNa' AS [Transaction] FROM dbo.POST AS a INNER JOIN
                        [dbo].PERCORD AS b ON b.idp = a.idp

                        UNION ALL
                        SELECT date AS [Date],b.fname+' '+b.mname+' '+b.lname AS Person,
                        b.pk+ ', '+b.by+', '+b.my AS Address, b.sex,
                        'PreNa' AS [Transaction] FROM dbo.PRENA AS a INNER JOIN
                        [dbo].PERCORD AS b ON b.idp = a.idp

                        UNION ALL
                        SELECT date AS [Date],b.fname+' '+b.mname+' '+b.lname AS Person,
                        b.pk+ ', '+b.by+', '+b.mty AS Address, b.sex AS Sex,
                        'Check' AS [Transaction] FROM dbo.CHECK AS a INNER JOIN
                        [dbo].PERCORD AS b ON b.idp = a.idp
                    )
                        as T where date between '" + fromdt.Value.ToString() + "' and '" + todt.Value.ToString() + "' and [Transaction] = '" + ttypecb.Text + "'";

                using (SqlConnection xcon = new SqlConnection(@"Server=QWERTY\SQLEXPRESS;Database=QWERTY;Integrated Security=SSPI;"))
                {
                    using (SqlCommand xcom = new SqlCommand(query, xcon))
                    {
                        SqlDataAdapter xdapter = new SqlDataAdapter(xcom);
                        try
                        {
                            xcon.Open();
                            DataTable xlist = new DataTable();
                            xdapter.Fill(xlist);
                            trans.DataSource = xlist;

                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            xcon.Close();
                        }
                    }
                }
            } 

1 个答案:

答案 0 :(得分:0)

  

我的问题是,有没有一种方法可以对将生成的行进行计数,并在datagridview下面的标签或文本框中显示计数?这是我的代码

调用DataTable方法后,Fill中将包含所有行。因此,您可以使用slist.Rows.Count来获取该信息。

我认为DataGridView不会在任何地方自动显示总行数。您可以使用任何选择的Control显示它。如果有帮助,您可以在DataGridView中使用行号