2010-08-27 74 views
0

您好我有課對我的數據訪問層,它包含一個名爲GetCitiesByLocation,它看起來像這樣轉換DAL功能轉換爲存儲過程在MySQL

public DataTable GetCitiesByLocation(long pStateID, string pKeyword) 
{ 

    //create database object 
    Database db = DBHelper.CreateDatabase(); 

    //create SELECT sql statement to be executed using string builder 
    //add WHERE 1 = 1 at the end 

    //add where [optional] statements 
    if (String.IsNullOrEmpty(pKeyword) == false) 
    { 
     //Create AND statement for ?Keyword 
    } 

    //add group by order by queries 
    sql.Append(" GROUP BY c.city_id "); 
    sql.Append(" ORDER BY city_name "); 

    //Convert SQL String To DbCommand Object 
    DbCommand comm = db.GetSqlStringCommand(sql.ToString()); 

    //Map Variables to mysql ?Parameters 
    db.AddInParameter(comm, "?StateID", DbType.Int64, pStateID); 
    db.AddInParameter(comm, "?Keyword", DbType.String, pKeyword); 

    try 
    { 
     //execute sql statement and return results 
    } 

    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

功能查看完整的版本在這裏:http://friendpaste.com/6ZGJHRNxQ9J57ntFD5XZi1

我會喜歡將其轉換爲MySQL存儲過程

這裏是我的朋友的一個失敗的嘗試將其轉換到MSSQL存儲過程

ALTER PROCEDURE [dbo].[sp_Search] 
@Keyword varchar(30) 

AS 

SELECT count(cc.course_id) as 'course_count', c.*, con.* FROM city c 
LEFT JOIN countries con on con.country_id = c.country_id 
LEFT JOIN courses cc on cc.city_id = c.city_id 
WHERE 1 = 1 
AND CASE @Keyword WHEN (@Keyword <> NULL) THEN (AND c.state_name like @Keyword) END 
AND CASE @Keyword WHEN (pStateID > 0) THEN (AND c.state_id = @StateID AND c.country_id = 69 AND c.province_id = 0) END 
AND CASE @Keyword WHEN (pProvinceID > 0) THEN (AND c.province_id = @ProvinceID AND c.country_id = 52 AND c.state_id = 0) END 

我也有,我想轉換到MySQL存儲過程

回答

0

事務DAL我不是MySQL的專家,但它似乎你試圖執行一個存儲過程,你將執行同樣的方式參數化查詢並且不起作用。

我也感到非常驚訝,如果你的MSSQL PROC編譯成我期望你的PROC將與

ALTER PROCEDURE [dbo].[sp_Search] 
    @Keyword varchar(30), 
    @StateID int = null, 
    @ProvinceID int = null 

或者在MySQL

CREATE PROCEDURE sp_Search(Keyword varhar(30), 
StateID int, 
ProviceId int) 

你最想必須改變你的開始DAL以下列方式

  1. 您現有的DAL調用 GetSqlStringComman d。您需要將 更改爲GetSqlStringCommand以GetStoredProcCommand。
  2. 您的參數名稱必須 變化
  3. 文本woud被sp_search代替「SELECT COUNT(cc.course_id)爲「course_count ......」
+0

嗨,我知道如何訪問一個存儲過程和存儲過程示例中的參數被有意地截斷,以表示我想要的示例輸出。我想將具有一系列if語句的DAL後面的現有代碼轉換爲存儲過程。 – 2010-09-01 23:31:54