你如何使用MySql IN子句

时间:2011-07-24 02:52:57

标签: c# mysql

我试图弄清楚如何在ASP.NET C#中使用MySql In cluse。这是我的代码

var WebSites = string.Join(",", wsl.Select(x => "'" + x.DomainUrl + "'").ToArray());
string q = "select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN (@Url)";

当我不使用参数时,此代码有效。当我包含参数时,我得不到查询的结果。

这是我的参数代码

cmd.Parameters.Add("@Urls", MySqlDbType.Text).Value = WebSites;

这里是整个代码

 public static IList<Post> FindPostsByWebSiteList(IEnumerable<WebSite> wsl)
 {
        var pl = new List<Post>();
        var WebSites = string.Join(",", wsl.Select(x => "'" + x.DomainUrl + "'").ToArray());
        string q = "select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN (@Urls)";

        using (MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ToString()))
        {
            using (MySqlCommand cmd = new MySqlCommand(q, con))
            {
                cmd.Parameters.Add("@Urls", MySqlDbType.Text).Value = WebSites;
                con.Open();

                var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    var p = new Post();
                    p.Id = reader.GetInt32("Id");
                    p.Url = reader.GetString("Url");
                    p.Title = reader.GetString("Title");
                    p.Date = reader.GetDateTime("Date");
                    p.ImageUrl = reader.GetString("ImageUrl");
                    pl.Add(p);
                }
                return pl;
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

您引用了@Url而不是@Urls

可能只是你问题中的拼写错误

答案 1 :(得分:1)

我找到了答案。这是

    public static IList<Post> FindPostsByWebSiteList(string[] urls)
    {
        var pl = new List<Post>();
        var urlArray = urls.Select((x,y) => "@url" + y.ToString()).ToArray();
        var urlsJoined = string.Join(",", urlArray);
        string q = string.Format("select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ({0})", urlsJoined);

        using (MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ToString()))
        {
            using (MySqlCommand cmd = new MySqlCommand(q, con))
            {
                for (int x = 0; x < urlArray.Length; x++)
                {
                    cmd.Parameters.Add(urlArray[x], MySqlDbType.Text).Value = urls[x];
                }

                con.Open();

                var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    var p = new Post();
                    p.Id = reader.GetInt32("Id");
                    p.Url = reader.GetString("Url");
                    p.Title = reader.GetString("Title");
                    p.Date = reader.GetDateTime("Date");
                    p.ImageUrl = reader.GetString("ImageUrl");
                    pl.Add(p);
                }
                return pl;
            }
        }
    }

答案 2 :(得分:0)

IN语句应该是一个字符串数组,并且您传递的是一个字符串

您的最终SQL看起来像这样:

select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ('url1,url2,url3')

而不是

select Id, Url, Title, Date, ImageUrl from post where WebSiteUrl IN ('url1', 'url2', 'url3')

检查此问题:

Add List<int> to a mysql parameter