2014-04-22 35 views
0

我試圖執行查詢,鏈接不同的數據庫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"); 
} 

什麼建議嗎?

+0

習慣用換行符等格式化查詢字符串。如果你一眼就能看到你的字符串連接被正確分隔並且空白是完整的,它確實有助於發現類似這樣的錯誤。 – Corey

回答

2

你錯過這裏的空間:

"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 [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"; 

在你的sql平臺上運行查詢,並從那裏你可以很容易地檢查錯誤來自哪裏。只需在文本字符串@「」中複製查詢;

+0

感謝您的回覆,這真是一個愚蠢的錯誤:D – Noora

+1

即使邁克爾喬丹也不用擔心,也犯了一個愚蠢的錯誤。 :d – jomsk1e