将两个sql查询结果行的值合并为一个c#中的数据网格

发布于 2024-08-17 19:07:30 字数 2288 浏览 3 评论 0原文

我有一个包含员工信息的员工表和一个包含员工电话号码的联系人详细信息表。员工拥有超过 2 个电话号码。

现在,为了显示员工信息,我有一个数据网格。我想要做的是在数据网格中显示前 2 个数字以及员工信息。

我使用以下方法填充数据网格

    public static void SignUpControllerDay(DateTime Date, System.Windows.Forms.DataGridView PassedGrid)
    {
        string sql_SignUp = String.Format(@"SELECT e.Emp_ID as Emp_ID,
                                      e.First_Name+ ' ' +e.Last_Name as Name,
                                      sum(o.Quantity) as Sum
                                      FROM Employee e,OT_hours o,Position p,Signup_Sheet s
                                      WHERE e.Emp_ID=o.Emp_ID
                                      and e.Emp_ID = s.Employee_ID
                                      and s.Day_Shift = 1
                                      and e.Position_ID = p.Position_ID
                                      and p.Position_Name = 'Controller'
                                      and o.Quantity NOT IN(0.3)
                                            and s.Date = '{0}'
                                      and o.Date <= CONVERT(VARCHAR,'{0}',101) AND o.Date > CONVERT(VARCHAR,DATEADD(YYYY,-1,'{0}'),101)
                                      GROUP BY e.Emp_ID,e.First_Name+' '+e.Last_Name,p.Position_Name
                                      ORDER BY Sum", Date);

        SqlConnection sqlConn = null;
        SqlCommand cmd_SignUp;
        SqlDataReader dr_SignUp;
        try
        {
            sqlConn = new SqlConnection(databaseConnectionString);
            sqlConn.Open();
            cmd_SignUp = new SqlCommand(sql_SignUp, sqlConn);
            dr_SignUp = cmd_SignUp.ExecuteReader();

            while (dr_SignUp.Read())
            {
                PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), dr_SignUp["Sum"].ToString());
            }

        }
        catch (Exception e)
        {
            MessageBox.Show("Error found in SignUpControllerDay..." + Environment.NewLine + e.ToString());
        }
        finally
        {
            if (sqlConn != null)
            {
                sqlConn.Close();
            }
        }
    }

上述方法显示员工的 empid、name、sum。我想要做的就是显示 contact_details 表中的任意 2 个电话号码。我尝试使用数据读取器根据员工 ID 获取电话号码,但没有成功。

请帮忙....

I have a employee table which contains employee information and a Contact details table which contains the phone numbers of the employees. the employees have more than 2 phone numbers.

now, to display the employee information, i have a datagrid. what i want to do is display the first 2 numbers along with the employee information in the datagrid.

i use the following method to fill the data grid

    public static void SignUpControllerDay(DateTime Date, System.Windows.Forms.DataGridView PassedGrid)
    {
        string sql_SignUp = String.Format(@"SELECT e.Emp_ID as Emp_ID,
                                      e.First_Name+ ' ' +e.Last_Name as Name,
                                      sum(o.Quantity) as Sum
                                      FROM Employee e,OT_hours o,Position p,Signup_Sheet s
                                      WHERE e.Emp_ID=o.Emp_ID
                                      and e.Emp_ID = s.Employee_ID
                                      and s.Day_Shift = 1
                                      and e.Position_ID = p.Position_ID
                                      and p.Position_Name = 'Controller'
                                      and o.Quantity NOT IN(0.3)
                                            and s.Date = '{0}'
                                      and o.Date <= CONVERT(VARCHAR,'{0}',101) AND o.Date > CONVERT(VARCHAR,DATEADD(YYYY,-1,'{0}'),101)
                                      GROUP BY e.Emp_ID,e.First_Name+' '+e.Last_Name,p.Position_Name
                                      ORDER BY Sum", Date);

        SqlConnection sqlConn = null;
        SqlCommand cmd_SignUp;
        SqlDataReader dr_SignUp;
        try
        {
            sqlConn = new SqlConnection(databaseConnectionString);
            sqlConn.Open();
            cmd_SignUp = new SqlCommand(sql_SignUp, sqlConn);
            dr_SignUp = cmd_SignUp.ExecuteReader();

            while (dr_SignUp.Read())
            {
                PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), dr_SignUp["Sum"].ToString());
            }

        }
        catch (Exception e)
        {
            MessageBox.Show("Error found in SignUpControllerDay..." + Environment.NewLine + e.ToString());
        }
        finally
        {
            if (sqlConn != null)
            {
                sqlConn.Close();
            }
        }
    }

the above method displays the empid,name,sum of the employees. all i want to do is display any 2 phone numbers from the contact_details table. i tried using a data reader to get the phone numbers based on the employee id, but it did not work.

please help....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-08-24 19:07:30

我想我知道 reggie 想要做什么,但在我给你任何帮助之前,reggie 让我们看一下你可能想要看的一些事情:

  1. 如上所述不要使用 String.Format 或动态 SQL,如果您在 SQL Server 上,请使用带有参数化 SQL Command 对象的存储过程
  2. 您让客户端(Web 应用程序或 Windows 应用程序)为真正的数据密集型操作(SQL 工作而不是 C# 工作)做太多工作
  3. 您根本不需要循环,甚至不需要使用数据读取器,因为它只是让您的代码变得臃肿并且不必要。
  4. 在您的finally子句中,您关闭了连接,但您从未将连接或实际的sql命令对象设置为null。我知道 C# 垃圾收集,但这样做是一个很好的实践。
  5. SQL Server 2005 及更高版本为我们带来了PIVOT 关键字,请在BOL 中查找。

关于您的问题,如果您使用的是 SQL Server 2005+,则可以使用 PIVOT。鉴于您提到可以有 2 个联系号码,让我们假设一个是工作号码,一个是家里号码。

您可以这样做:

SELECT 
      FullName, 
      [Work], 
      [Home] 
FROM
(SELECT 
       l.FullName, 
       p.PhoneType, 
       p.PhoneNumber 
 FROM 
       Login l 
 INNER JOIN 
       Phone p 
 ON p.LoginID = l.LoginID) ps
PIVOT
(
 MAX(ps.PhoneNumber)
FOR
 ps.PhoneType IN ([Home], [Work])
) AS pvt

登录只是您的员工表,电话是包含电话号码的联系人表。我假设可以在联系人表中找到员工 ID(即 Login.LoginID = Phone.LoginID),或者在您的情况下为 Employee.EmpID=Contacts.EmpID。

所以这个:

替代文本 http://img513.imageshack.us/img513/6126/onetime .jpg

对于不使用数据透视的标准查询,则变为...:

alt 文本 http://img513.imageshack.us/img513/7395/2time.jpg

当使用数据透视表时。

I think I know what reggie wants to do but before I give you any help reggie lets take a look at a couple of things that you may want to look at:

  1. As mentioned do not use String.Format or Dynamic SQL, if you are on SQL Server make use of stored procedures with parameterized SQL Command objects
  2. You are having the client (web app or windows app) doing too much work for something that is really data intensive (SQL work rather then C# work)
  3. You do not need to loop at all or even use a datareader as it is just bloating your code and unnecessary
  4. In your finally clause you close the connection but you never set your connection or the actual sql command object to null. I know C# garbage collects but it is a good point of practice to do so.
  5. SQL Server 2005 and up brings to us the PIVOT key word, look it up in BOL

In regards to your question if you are using SQL Server 2005+ you can use PIVOT. Given that you mentioned there can be 2 contact numbers let us assume one is work and one is home.

You could do this:

SELECT 
      FullName, 
      [Work], 
      [Home] 
FROM
(SELECT 
       l.FullName, 
       p.PhoneType, 
       p.PhoneNumber 
 FROM 
       Login l 
 INNER JOIN 
       Phone p 
 ON p.LoginID = l.LoginID) ps
PIVOT
(
 MAX(ps.PhoneNumber)
FOR
 ps.PhoneType IN ([Home], [Work])
) AS pvt

Login is simply your Employees table and Phone is your contacts table with the phone number. I assume an employee id can be found in the contacts table (that is Login.LoginID = Phone.LoginID) or in your case Employee.EmpID=Contacts.EmpID.

So this:

alt text http://img513.imageshack.us/img513/6126/onetime.jpg

With a standard query not using pivot then becomes this...:

alt text http://img513.imageshack.us/img513/7395/2time.jpg

When using pivot.

静谧幽蓝 2024-08-24 19:07:30

您不在 SQL 中的任何位置引用 contact_details 表。您需要连接 Employee 和 contact_details 表以包含电话号码。

You don't reference the contact_details table anywhere in the SQL. You need to join the Employee and contact_details tables to include the phone numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文