0
下面是代碼,我嘗試使用ado.net執行。我究竟做錯了什麼?使用ado.net刪除帶有「in」子句的記錄
Trying to delete records using ado.net
private const string SqlDeleteMultiple = @"
DELETE FROM [Board]
WHERE
[Id] in (@Ids)
";
public void DeleteMultiple(string items)
{
ArgumentValidator.ThrowOnNull("item", items);
/* var sbItems = new StringBuilder();
sbItems.Append("'");
sbItems.Append(items);
sbItems.Replace(",", "','");
sbItems.Append("'");*/
try
{
using (var conn = new SqlConnection(_connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(SqlDeleteMultiple,conn))
{
cmd.Parameters.Add(new SqlParameter("@Ids", SqlDbType.VarChar) {Value = items});
cmd.ExecuteNonQuery();
}
}
}
catch (Exception err)
{
_logger.Log(LogLevel.Error, err, "Error calling Delete");
throw err;
}
}
將varchar值'9,8'轉換爲數據類型int時轉換失敗。如果我將SqlDbType更改爲Int ..我得到另一個錯誤。