我知道这可能是个愚蠢的问题。但是这个查询不起作用。我搜索解决方案超过1小时。 请帮忙
public static bool ChangeEventStatus(Connector cn, EventData eventData)
{
int updatedRows = 0;
using (OleDbCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";
cmd.Parameters.Add("IsProcessed", OleDbType.Boolean).Value = true;
cmd.Parameters.Add("EventId", OleDbType.BigInt).Value = eventData.EventId;
updatedRows = cmd.ExecuteNonQuery();
}
return (updatedRows == 1);
}
我的代码有什么问题。 ChangeEventStatus 方法返回true,但数据库记录没有变化。
答案 0 :(得分:1)
public static bool ChangeEventStatus(Connector cn, EventData eventData)
{
int updatedRows = 0;
using (OleDbConnection conn = new OleDbConnection(someConnectionString));
{
conn.Open();
using (OleDbCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";
cmd.Parameters.Add("@IsProcessed", OleDbType.Boolean).Value = true;
cmd.Parameters.Add("@EventId", OleDbType.BigInt).Value = eventData.EventId;
updatedRows = cmd.ExecuteNonQuery();
}
return (updatedRows == 1);
}
}
答案 1 :(得分:0)
我发现了问题。感谢大家。我在更新后忘了提交交易
答案 2 :(得分:0)
我稍微更改了代码并编写了测试。一切正常。 检查:
您是否通过了正确的EventId功能?
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(true, ChangeEventStatus(new EventData() {EventId = 3}));
}
private static bool ChangeEventStatus(EventData eventData)
{
int updatedRows = 0;
using (var cn = new OleDbConnection("Provider=sqloledb;Data Source=localhost;Initial Catalog=FastExperiments;User Id = sa; Password = pass; "))
{
using (OleDbCommand cmd = cn.CreateCommand())
{
cn.Open();
cmd.CommandText = "Update EventList Set IsProcessed = ? Where EventId = ?";
cmd.Parameters.Add("IsProcessed", OleDbType.Boolean).Value = true;
cmd.Parameters.Add("EventId", OleDbType.BigInt).Value = eventData.EventId;
updatedRows = cmd.ExecuteNonQuery();
}
}
return (updatedRows == 1);
}
private class EventData
{
public int EventId { get; set; }
}
}