0

異なるデータベース OleDbDataReader をリンクするクエリを実行しようとしていますが、クエリ文字列でエラーが発生しています。このクエリは正しく、SQL Studio Manager では正常に実行されていますが、.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 --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");
}

なにか提案を ?

4

1 に答える 1

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 --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 プラットフォームでクエリを実行すると、そこからエラーの原因を簡単に確認できます。そして、リテラル文字列 @""; 内のクエリをコピーするだけです。

于 2014-04-22T06:03:59.413 に答える