2010-08-27 94 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