DataReader 不返回任何行,但存在数据
本文关键字:存在 数据 任何行 返回 DataReader | 更新日期: 2023-09-27 18:37:20
我正在尝试按月获取警报统计信息。这是我的 SQL 语句
SELECT datepart(year,incidentDate),datepart(month,incidentDate) ,COUNT(*) AS CNT
From alarm_actions,alarm_licenses
where licType = 'AR' and alarm_actions.alarmNo = ALARM_LICENSES.alarmNo and alarmCount > 0 and
ignoreAlarm = 0 and incidentDate between '5/1/12' and '7/31/14'
group by datepart(year,incidentDate) , datepart(month,incidentdate)
order by datepart(year,incidentDate)
当我在 SQL 2008R2 中执行此查询时,我得到 27 行,但是当我在 Visual Studios 中使用 SqlDataReader 时,我没有得到任何数据。 chartFrom 和 chartTo 是我用来获取日期的文本框。
string alarmCount = ("Select datepart(year,incidentDate) , datepart(month,incidentDate),COUNT(*) as cnt From Alarm_Actions,Alarm_Licenses Where licType = 'AR' and alarm_actions.alarmNo = alarm_licenses.alarmNo and alarmCount > 0 and ignoreAlarm = 0 and incidentDate between " + chartFromDate + " and " + chartToDate + "group by DATEPART(YEAR, incidentDate), DATEPART(MONTH,incidentDate) order by DATEPART(YEAR, incidentDate)");
SqlDataReader dr = data.myDataReader(alarmCount);
while(dr.Read())
{
//Do something
}
这是myDataReader的代码
public SqlDataReader myDataReader(string mSQL)
{
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataReader myReader;
SqlCommand myCommand1;
try
{
myConnection.Open();
//myCommand1 = new System.Data.OleDb.OleDbCommand(mSQL, myConnection);
myCommand1 = new SqlCommand(mSQL, myConnection);
//=============== 1/26/2012 =============
//There have been errors running recently. I am increasing the timeout here
myCommand1.CommandTimeout = 60000;
myReader = myCommand1.ExecuteReader(CommandBehavior.CloseConnection);
//return myReader;
}
catch (Exception e1)
{
MessageBox.Show("An error occurred retrieving the requested data'r'n'r'n" + mSQL + "'r'n'r'n" + e1.Message);
myReader = null;
try
{
string mSentBy = ConfigurationManager.AppSettings["emailSentBy"].ToString();
string mSendTo1 = ConfigurationManager.AppSettings["emailSendTo"].ToString();
string mSubject = ConfigurationManager.AppSettings["emailSubject"].ToString();
string mHost = ConfigurationManager.AppSettings["emailHost"].ToString();
string mSignon = ConfigurationManager.AppSettings["emailSignon"].ToString();
string mPassword = ConfigurationManager.AppSettings["emailPassword"].ToString();
}
catch { }
}
finally
{
//myConnection.Close();
myCommand1 = null;
//myReader = null;
myConnection = null;
//myReader = null;
}
return myReader;
}
谢谢你帮助我
我在日期字段中缺少 '。 感谢大家抽出宝贵时间提供帮助
我没有
测试它,但我希望它对你有用:
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT datepart(year,incidentDate), ......";
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
//Do something
}
con.Close();