我試圖執行查詢,鏈接不同的數據庫OleDbDataReader,但我在查詢字符串中出現錯誤,請注意,此查詢是正確的,並在sql studio manager中運行成功 但在.Net我在OleDbDataReader在asp.net中查詢語法錯誤
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 [email protected] "+
"(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");
}
什麼建議嗎?
習慣用換行符等格式化查詢字符串。如果你一眼就能看到你的字符串連接被正確分隔並且空白是完整的,它確實有助於發現類似這樣的錯誤。 – Corey