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