protected void getData() { string workid = this.gonghaoTxt.Text; if (!workid.Equals(String.Empty)) { //先查询人事资料 string sql1 = "select top 1 EMPID,NAME,SEX,DEPNAME,SEGNAME,EMPSIZE,convert(varchar(10),ENT_DATE,23)as ENT_DATE from"; if (RadioButton3.Checked) sql1 = sql1 + " kqempe"; else sql1 = sql1 + " kqdime"; sql1 = sql1 + " kqempe where empid='" + workid + "'"; DataTable dt1 = new DataTable("kqEMPE"); ConnectSQL sqlconn = new ConnectSQL(); dt1 = sqlconn.SearchHRS(sql1); //查询培训记录 string sql2 = "select * FROM PXVIEW where workid='" + workid + "' order by workdate asc"; DataTable dt2 = new DataTable("PXVIEW"); dt2 = sqlconn.SearchHRS(sql2); if (dt2.Rows.Count > 0) { this.result.Text = ""; this.CrystalReportViewer1.Visible = true; string fact = Session["Factory"].ToString();//所在厂区 string serverName = "";//服务器地址 string DBname = "";//数据库 string username = "";//帐号 string userpwd = "";//密码 switch (fact) { case "SMD": serverName = "192.168.2.12"; DBname = "HRSD"; username = "fkqe"; userpwd = "kqef"; break; case "XFE": serverName = "192.168.2.12"; DBname = "HRSF"; username = "fkqe"; userpwd = "kqef"; break; case "XTE": serverName = "192.168.2.12"; DBname = "HRSZ"; username = "fkqe"; userpwd = "kqef"; break; case "SMX": serverName = "192.168.50.31"; DBname = "HRSX"; username = "fkqe"; userpwd = "kqef"; break; } string strPath = Server.MapPath("EachTrainReport.rpt"); ReportDocument rd = new ReportDocument(); rd.Load(strPath); rd.SetDataSource(dt2); TextObject nameTxt = rd.ReportDefinition.ReportObjects["nameTxt"] as TextObject; TextObject gonghaoTxt = rd.ReportDefinition.ReportObjects["gonghaoTxt"] as TextObject; TextObject entdateTxt = rd.ReportDefinition.ReportObjects["entdateTxt"] as TextObject; TextObject empsizeTxt = rd.ReportDefinition.ReportObjects["empsizeTxt"] as TextObject; TextObject depnameTxt = rd.ReportDefinition.ReportObjects["depnameTxt"] as TextObject; TextObject sextxt = rd.ReportDefinition.ReportObjects["sextxt"] as TextObject; TextObject segnameTxt = rd.ReportDefinition.ReportObjects["segnameTxt"] as TextObject; //将员工资料显示到报表中的文本框 if (dt1.Rows.Count > 0) { nameTxt.Text = dt1.Rows[0]["NAME"].ToString(); gonghaoTxt.Text = dt1.Rows[0]["EMPID"].ToString(); entdateTxt.Text = dt1.Rows[0]["ENT_DATE"].ToString(); empsizeTxt.Text = dt1.Rows[0]["EMPSIZE"].ToString(); depnameTxt.Text = dt1.Rows[0]["DEPNAME"].ToString(); sextxt.Text = dt1.Rows[0]["SEX"].ToString(); segnameTxt.Text = dt1.Rows[0]["SEGNAME"].ToString(); } rd.Refresh(); TableLogOnInfo logOnInfo = new TableLogOnInfo(); // 对报表中的每个表依次循环。 for (int i = 0; i < rd.Database.Tables.Count; i++) { logOnInfo.ConnectionInfo.ServerName = serverName; logOnInfo.ConnectionInfo.DatabaseName = DBname; logOnInfo.ConnectionInfo.UserID = username; logOnInfo.ConnectionInfo.Password = userpwd; rd.Database.Tables[i].ApplyLogOnInfo(logOnInfo); } this.CrystalReportViewer1.ReportSource = rd; } else { this.result.Text = "该员工暂无培训记录"; this.CrystalReportViewer1.Visible = false; } } }