将两个sql查询结果行的值合并为一个c#中的数据网格
我有一个包含员工信息的员工表和一个包含员工电话号码的联系人详细信息表。员工拥有超过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我知道 reggie 想要做什么,但在我给你任何帮助之前,reggie 让我们看一下你可能想要看的一些事情:
PIVOT
关键字,请在BOL 中查找。关于您的问题,如果您使用的是 SQL Server 2005+,则可以使用 PIVOT。鉴于您提到可以有 2 个联系号码,让我们假设一个是工作号码,一个是家里号码。
您可以这样做:
登录只是您的员工表,电话是包含电话号码的联系人表。我假设可以在联系人表中找到员工 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:
PIVOT
key word, look it up in BOLIn 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:
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.
您不在 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.