C# asp.net中的OleDbDataReader查询语法错误
我试图执行一个链接不同数据库的查询OleDbDataReader,但查询字符串中出现错误,请注意,此查询是正确的,并且在sql studio manager中成功运行 但是在.Net中,我在C# asp.net中的OleDbDataReader查询语法错误,c#,asp.net,sql,oledb,oledbdatareader,C#,Asp.net,Sql,Oledb,Oledbdatareader,我试图执行一个链接不同数据库的查询OleDbDataReader,但查询字符串中出现错误,请注意,此查询是正确的,并且在sql studio manager中成功运行 但是在.Net中,我在 OleDbDataReader reader = myCommand.ExecuteReader(); 错误消息: “PR1”附近的语法不正确 我的代码长查询字符串 protected void Page_Load(object sender, EventArgs e) { string strC
OleDbDataReader reader = myCommand.ExecuteReader();
错误消息:
“PR1”附近的语法不正确
我的代码长查询字符串
protected void Page_Load(object sender, EventArgs e)
{
string strConString=System.Configuration.ConfigurationManager.ConnectionStrings["WorkflowConnStr"].ConnectionString.ToString();
string sqlstr = "select coalesce(engdir ,'Total') [engdir]," +
"SUM(case when a.Status = 'Delivered' then Total else 0 end) as [Delivered],"+
"SUM(case when a.Status = 'Completed' then Total else 0 end) as [Completed],"+
"SUM(case when a.Status = 'Pending' then Total else 0 end) as [Pending],"+
"SUM(case when (a.Status ='Rejected') then Total Else 0 end) as [Rejected],"+
"COUNT(*) as Total from "+
"(select count(*) as Total, CurrentActorUID,ReferenceNo, Status,RequestedDate from tbl_ServiceTracking "+
"where CurrentActorUID is not null and CurrentActorUID <> '' and "+
"(Status='rejected' or Status='Completed' or Status='Pending' or Status='Delivered' )and (ServiceNo is not null )"+
"group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a"+
"JOIN PR1.dbo.GetUserDetailsE AS b "+
"ON a.CurrentActorUID = b.PERUserName"+
"WHERE --b.DirCode=@DirCode "+
"(CAST(a.RequestedDate AS DATETIME)>='1/1/2014' AND CAST(a.RequestedDate AS DATETIME)<='4/30/2014') "+
"GROUP BY b.engdir "+
"WITH ROLLUP HAVING engdir IS NOT NULL OR GROUPING(b.engdir) = 1 ORDER BY "+
"CASE WHEN b.engdir IS NULL THEN 1 ELSE 0 END ,b.engdir ";
OleDbConnection myConnection = new OleDbConnection(strConString);
try {myConnection.Open();}
catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.Message); }
OleDbCommand myCommand = new OleDbCommand(sqlstr, myConnection);
OleDbDataReader reader = myCommand.ExecuteReader();
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
Chart1.DataBindCrossTable(
reader,
"Excellent",
"Satisfied",
"Not Satisfied",
"Total");
}
有什么建议吗?您错过了一个空白:
"group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a"+
应该是:
"group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a "+
此外,您可以使用文字'@'而不是使用串联,如下所示:
string sqlstr = @"SELECT COALESCE(engdir, 'Total') [engdir],
SUM(case when a.Status = 'Delivered' then Total else 0 end) as [Delivered],
SUM(case when a.Status = 'Completed' then Total else 0 end) as [Completed],
SUM(case when a.Status = 'Pending' then Total else 0 end) as [Pending],
SUM(case when (a.Status ='Rejected') then Total Else 0 end) as [Rejected],
COUNT(*) as Total from
(select count(*) as Total, CurrentActorUID,ReferenceNo, Status,RequestedDate from tbl_ServiceTracking
where CurrentActorUID is not null and CurrentActorUID <> '' and
JOIN PR1.dbo.GetUserDetailsE AS b
ON a.CurrentActorUID = b.PERUserName
WHERE --b.DirCode=@DirCode
(CAST(a.RequestedDate AS DATETIME)>='1/1/2014' AND CAST(a.RequestedDate AS DATETIME)<='4/30/2014')
GROUP BY b.engdir
WITH ROLLUP HAVING engdir IS NOT NULL OR GROUPING(b.engdir) = 1 ORDER BY
CASE WHEN b.engdir IS NULL THEN 1 ELSE 0 END ,b.engdir";
在您的sql平台中运行查询,从那里您可以轻松地检查错误的来源。只需将查询复制到文本字符串@中 习惯使用换行符等格式设置查询字符串。如果你一眼就能看出你的字符串连接是正确分隔的,空格是完整的,那么这真的有助于发现这样的错误。谢谢你的回答,这真是一个愚蠢的错误:DDon别担心,连Michael Jordan也犯了一个愚蠢的错误D