使用MVC4的SQL查询的转换错误(nvarchar到int)

时间:2014-05-09 19:40:08

标签: c# sql asp.net-mvc asp.net-mvc-4

我收到了来自Agent Shark的另一个stackoverflow帖子的代码,并稍微改了一下供我使用。问题是我收到错误:

  

将nvarchar值'39432,39431,39430,39429,39428'转换为数据类型int时转换失败。

问题是,我该如何解决这个问题?

原始的StackOverFlow帖子:

What is the best method to access a large database using MVC 4 and Entity Framework 6

以下是我正在使用的代码:

public void DeleteBatchCampaignContacts(IList<int> ids)
{
    if (ids == null) return;
    if (ids.Count == 0) return;

    var idsToDelete = new StringBuilder();

    foreach (var id in ids)
    {
        idsToDelete.AppendFormat(",{0}", id);
    }

    db.Database.ExecuteSqlCommand("DELETE FROM EmailContactCampaign WHERE EmailContact_EmailContactId in (@contactIds)",
    new SqlParameter("@contactIds", idsToDelete.ToString().Remove(0, 1)));
}

1 个答案:

答案 0 :(得分:1)

Database.ExecuteSqlCommand(string.Format("DELETE FROM CampainContacts WHERE CampaignId in ({0})", idsToDelete.ToString().Remove(0, 1)));