我正在开发一个etime历史应用程序,数据从adp应用程序带入sql。
要求
用户希望查看特定员工的旧时间表信息
过程
用户点击1)paygroup从下拉菜单2)员工下拉菜单按薪资组分组3)选择(从日期)和(到日期)4)点击运行报告并填写数据< /强>
完美填充下拉列表的工作,当我点击运行报告时,只有列paygroup,empid和日期被填充但是列DeptNo,WorkCenterID,JobCode,InPunch,OutPunch没有被填满。哦,正在填写的列在EtimePunchDetail表中,其他人在EtimeEmployees中,我如何使用EtimeEmployees和我的SqlCommand中的字段是我的问题?这些字段不是参数,所以我感到困惑,因为其他的是参数......
我用来填充列的代码
private void button1_Click(object sender, EventArgs e) //run report button loads all the pieces onto the report.
{
if (FromDate.Value > ToDate.Value)
{
MessageBox.Show("From Date Must be Less Than To Date");
}
else
//fileexport
using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
{
timepunchnew = new EtimeHistoryDataSet();
connection.Open();
MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT * From EtimePunchDetail WHERE (EmpID = @empid) And (Paygroup = @paygroup) And (TransDate >= @fromdate) And (TransDate <= @todate)", connection);
{
MSSQL.SqlParameter parmEmp = new MSSQL.SqlParameter();
parmEmp.ParameterName = "@empid";
parmEmp.Value = Employee.SelectedValue;
command.Parameters.Add(parmEmp);
MSSQL.SqlParameter paramPayGroup = new MSSQL.SqlParameter();
paramPayGroup.ParameterName = "@paygroup";
paramPayGroup.Value = Paygroup.SelectedValue;
command.Parameters.Add(paramPayGroup);
MSSQL.SqlParameter paramFromDate = new MSSQL.SqlParameter();
paramFromDate.ParameterName = "@fromdate";
paramFromDate.DbType = DbType.DateTime;
paramFromDate.SqlDbType = SqlDbType.DateTime;
paramFromDate.Value = FromDate.Value.ToString("");
command.Parameters.Add(paramFromDate);
MSSQL.SqlParameter paramToDate = new MSSQL.SqlParameter();
paramToDate.ParameterName = "@todate";
paramToDate.DbType = DbType.DateTime;
paramToDate.SqlDbType = SqlDbType.DateTime;
paramToDate.Value = ToDate.Value;;
command.Parameters.Add(paramToDate);
MSSQL.SqlDataAdapter EtimePunchDetailTableAdapter = new System.Data.SqlClient.SqlDataAdapter();
EtimePunchDetailTableAdapter.SelectCommand = command;
EtimePunchDetailTableAdapter.Fill(timepunchnew, "Etime");
string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
string reportPath = exeFolder + @"\Report1.rdlc";
reportViewer1.LocalReport.DisplayName = MakeValidFileName(Employee.Text) + "-" + FromDate.Value.ToString("MM") + "!" + (FromDate.Value.ToString("dd")) + "!" + FromDate.Value.ToString("yyyy") + "-" + ToDate.Value.ToString("MM") + "!" + ToDate.Value.ToString("dd") + "!" + ToDate.Value.ToString("yyyy");
reportViewer1.LocalReport.ReportPath = reportPath;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("EtimeHistoryDataSet_Etime", timepunchnew.Etime));
reportViewer1.RefreshReport();
reportViewer1.Refresh();
reportViewer1.Visible = true;
}
}
}
答案 0 :(得分:0)
您可以在SQL语句中添加连接以从另一个表中获取记录。
在你的情况下......
SELECT pd.*, e.EmployeeNameOrWhateverFieldYouWant
FROM EtimePunchDetail pd INNER JOIN EtimeEmployee e ON e.EmpID = pd.EmpID
WHERE (pd.EmpID = @empid) And (pd.Paygroup = @paygroup)
And (pd.TransDate >= @fromdate) And (pd.TransDate <= @todate)
请注意添加表别名,这使得引用表更方便,因为您需要限定两个表中存在的任何字段(否则SQL无法告诉您哪个表)试图从中获取数据并抛出错误。)
如果您需要使用它们来过滤结果,您只需要将employee表中的字段作为参数添加。他们在选择列表中工作得很好。