将DAL函数转换为mysql中的存储过程

发布于 2024-09-16 18:04:57 字数 1885 浏览 1 评论 0原文

您好,我的数据访问层有一个类,其中包含一个名为 GetCitiesByLocation 的函数,它看起来像这样,

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

我也有我想要的事务 DAL转换为mysql存储过程

Hi I have class for my Data Access Layer that contains a function called GetCitiesByLocation and it looks like this

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;
    }
}

See full version here: http://friendpaste.com/6ZGJHRNxQ9J57ntFD5XZi1

I would like to convert this to MYSQL Stored Procedure

Here is a failed attempt of my friend to convert it to MSSQL Stored Proc

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

I have also transactional DAL which i want to convert to mysql stored proc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-09-23 18:04:57

我不是 MySQL 专家,但您似乎试图以与执行参数化查询相同的方式执行存储过程,但这是行不通的。

如果您的 MSSQL Proc 编译为我期望的那样,我也会感到非常惊讶,您的 proc 将以

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

Or 开头,在 MySQL 中

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

您最希望必须通过以下方式更改您的 DAL

  1. 您现有的 DAL 调用
    获取SqlString命令。你需要
    将其从 GetSqlStringCommand 更改为 GetStoredProcCommand。
  2. 你的参数名称必须
    更改
  3. 文本为 sp_search 而不是“Select count(cc.course_id) as 'course_count...”

I'm not an expert on MySQL but it would seem that you're trying to execute a Stored Procedure the same way you would execute a Parameterized Query and that doesn't work.

I also would be very surprised if your MSSQL Proc compiled as I would expect that your proc would start with

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

Or in MySQL

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

You would most like have to change your DAL in the following ways

  1. Your existing DAL calls
    GetSqlStringCommand. You need to
    change that to GetSqlStringCommand to a GetStoredProcCommand.
  2. your parameter names would have to
    change
  3. the text woud be sp_search instead of "Select count(cc.course_id) as 'course_count..."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文