1

我创建了两种允许使用高级搜索来显示日志表的方法:

void GetLogs()
{
     DateTime start = GetFirstRecord();
     DateTime end = GetLastRecord();
     con.Open();
     SqlCommand cmd = new SqlCommand();
     cmd.Connection = con;
     cmd.CommandText = "SELECT Logs.LogID, Users.LastName, Users.FirstName, " +
         "Logs.LogType, Logs.Description, Logs.Timestamp FROM Logs INNER JOIN Users " +
         "ON Logs.UserID = Users.UserID";

     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     da.Fill(ds, "Report");
     rptLogs report = new rptLogs();
     report.SetDatabaseLogon("username", "password", "(local)", "database");
     report.SetDataSource(ds);
     report.SetParameterValue("Start", start);
     report.SetParameterValue("End", end);
     crvLogs.ReportSource = report;
     crvLogs.Refresh();

     con.Close();
}

void GetLogsByTimestamp()
{
     con.Open();
     SqlCommand cmd = new SqlCommand();
     cmd.Connection = con;
     cmd.CommandText = "SELECT Logs.LogID, Users.LastName, Users.FirstName, " +
         "Logs.LogType, Logs.Description, Logs.Timestamp FROM Logs INNER JOIN Users " +
         "ON Logs.UserID = Users.UserID WHERE Logs.Timestamp BETWEEN @Start AND @End";
     cmd.Parameters.Add("@Start", SqlDbType.DateTime).Value = dtpStart.Value.Date;
     cmd.Parameters.Add("@End", SqlDbType.DateTime).Value = dtpEnd.Value.Date;
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     da.Fill(ds, "Report");
     rptLogs report = new rptLogs();
     report.SetDatabaseLogon("username", "password", "(local)", "database");
     report.SetDataSource(ds);
     report.SetParameterValue("Start", dtpStart.Value.Date);
     report.SetParameterValue("End", dtpEnd.Value.Date);
     crvLogs.ReportSource = report;
     crvLogs.Refresh();

     con.Close();
}

第一种方法在应用程序中正确加载了 Crystal Report。但是,当我触发第二种方法时,只有参数字段值被更新。报告的细节仍然相同。

我错过了什么吗?

4

2 回答 2

1
report.SetDatabaseLogon("username", "password", "(local)", "database");
report.SetDataSource(ds);

除了要推送到其中的数据集之外,您的报表是否还有单独的数据库连接?

于 2013-10-17T16:19:35.627 回答
0
     con.Open();
 SqlCommand cmd = new SqlCommand();
 cmd.Connection = con;
 cmd.CommandText = "SELECT Logs.LogID, Users.LastName, Users.FirstName, " +
     "Logs.LogType, Logs.Description, Logs.Timestamp FROM Logs INNER JOIN Users " +
     "ON Logs.UserID = Users.UserID WHERE Logs.Timestamp BETWEEN @Start AND @End";
 cmd.Parameters.Add("@Start", SqlDbType.DateTime);
 cmd.Parameters["@Start"].Value = dtpStart.Value.Date;
 cmd.Parameters.Add("@End", SqlDbType.DateTime);
 cmd.Parameters["@End"].Value = dtpEnd.Value.Date;
 SqlDataAdapter da = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds, "Report");
 rptLogs report = new rptLogs();
 report.SetDatabaseLogon("username", "password", "(local)", "database");
 report.SetDataSource(ds);
 report.SetParameterValue("Start", dtpStart.Value.Date);
 report.SetParameterValue("End", dtpEnd.Value.Date);
 crvLogs.ReportSource = report;
 crvLogs.Refresh();

 con.Close();

问题仍然是第二种方法是否真的被触发了。如果是,则错误在 SQL 语句中,如果不是,则触发器错误。

于 2013-10-17T15:50:33.583 回答