我无法手动将CurrentSystemTime添加到我的查询中。 Query存储在字符串变量中。但是,分段部分给出了错误:
查询为:
<?xml version="1.0"?>
<QUANDATASET>
<XMLFILE>
<DATASET>
<GROUPDATA>
<GROUP>
<METHODDATA/>
<SAMPLELISTDATA>
<SAMPLE id="1" groupid="1" name="Routine_2016_05_30_002">
<COMPOUND id="1" sampleid="1" groupid="1" name="Leu">
<PEAK foundscan="0" analconc="0.023423456">
<ISPEAK/>
</PEAK>
</COMPOUND>
<COMPOUND id="2" sampleid="1" groupid="1" name="Iso">
<PEAK foundscan="0" analconc="0.123456789">
<ISPEAK/>
</PEAK>
</COMPOUND>
<COMPOUND id="3" sampleid="1" groupid="1" name="Thre">
...
...
...
<SAMPLE id="2" groupid="1" name="Routine_2016_05_30_003">
<COMPOUND id="1" sampleid="2" groupid="1" name="Leu">
...
...
...
问题部分:
ASD.eventDateTime&gt; =&#39;&#34; + DateTime.Today.ToString(&#34; yyyy-MM-dd h:mm:ss&#34;)+&#34;&#39;
错误是:
将文本表示为unicode字符Newline in constant。
答案 0 :(得分:4)
您忘记使用@
启动字符串的第二部分,这就是编译器修改换行符无效的原因:
string myQuery= @"SELECT MAX(ASD.eventDateTime) AS second, R.resourceID,R.resourceLoginID,ASD.agentID
FROM AgentStateDetail AS ASD INNER JOIN
Resource AS R ON ASD.agentID = R.ResourceID WHERE ASD.eventDateTime >='" + DateTime.Today.ToString("yyyy-MM-dd h:mm:ss") + @"'AND ASD.eventDateTime <='2016-10-18 23:59:59'
GROUP BY R.ResourceID,R.resourceLoginID,ASD.agentID"
答案 1 :(得分:1)
您可以使用参数化查询,如下所示
string sql = "SELECT MAX(ASD.eventDateTime) AS second, R.resourceID, R.resourceLoginID, ASD.agentID FROM AgentStateDetail AS ASD INNER JOIN Resource AS R ON ASD.agentID = R.ResourceID WHERE ASD.eventDateTime >= @dateTimeStart AND ASD.eventDateTime <= @dateTimeEnd GROUP BY R.ResourceID,R.resourceLoginID,ASD.agentID";
using (SqlConnection connection = new SqlConnection(/* connection info */))
using (SqlCommand command = new SqlCommand(sql, connection))
{
var dateTimeStart = new SqlParameter("dateTimeStart", SqlDbType.DateTime);
dateTimeStart.Value = new DateTime("yyyy-MM-dd h:mm:ss");
var dateTimeEnd = new SqlParameter("dateTimeEnd", SqlDbType.DateTime);
dateTimeEnd.Value = new DateTime("yyyy-MM-dd h:mm:ss");
command.Parameters.Add(dateTimeStart);
command.Parameters.Add(dateTimeEnd);
var results = command.ExecuteReader();
}