没有错误,但 delete/update 查询在 C# 中不起作用
No error, but delete/update query not working in c#
我正在开展一个项目,在该项目中我使用的是 Access 2007 数据库 (.accdb
) 而不是 SQL 服务器。为了执行 CRUD 操作,我使用 MS Access 的查询,因为我以前的代码是使用存储过程编写的。我也在使用 SQL 交易。
这是我的代码:
public int OrderId { get; set; }
public string OrderDate { get; set; }
public int ServiceMode { get; set; }
public string DeliveryDate { get; set; }
public int ClientId { get; set; }
public int TotalQty { get; set; }
public decimal TotalAmount { get; set; }
public int Discount { get; set; }
public decimal DiscAmount { get; set; }
public decimal NetTotal { get; set; }
public decimal Advance { get; set; }
public decimal Due { get; set; }
public bool Update(DataTable itemList)
{
using (var conn = new OleDbConnection(Sniper.ConnString))
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
using (var cmd = new OleDbCommand())
{
cmd.Transaction = trans;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format("DELETE FROM OrderMaster WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format("DELETE FROM OrderDetails WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format("DELETE FROM OrderStatus WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
}
using (var cmd = new OleDbCommand("qry_OrderMaster_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@OrderDate", OrderDate);
cmd.Parameters.AddWithValue("@ServiceMode", ServiceMode);
cmd.Parameters.AddWithValue("@DeliveryDate", DeliveryDate);
cmd.Parameters.AddWithValue("@ClientId", ClientId);
cmd.Parameters.AddWithValue("@TotalQty", TotalQty);
cmd.Parameters.AddWithValue("@TotalAmount", TotalAmount);
cmd.Parameters.AddWithValue("@Discount", Discount);
cmd.Parameters.AddWithValue("@DiscAmount", DiscAmount);
cmd.Parameters.AddWithValue("@NetTotal", NetTotal);
cmd.Parameters.AddWithValue("@Advance", Advance);
cmd.Parameters.AddWithValue("@Due", Due);
cmd.ExecuteNonQuery();
}
using (var cmd = new OleDbCommand("qry_OrderStatus_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@IsDelivered", 0);
cmd.Parameters.AddWithValue("@IsCancelled", 0);
cmd.ExecuteNonQuery();
}
foreach (DataRow row in itemList.Rows)
{
using (var cmd = new OleDbCommand("qry_OrderDetails_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@ItemId", row["ItemId"].ToInt());
cmd.Parameters.AddWithValue("@Qty", row["Qty"].ToInt());
cmd.Parameters.AddWithValue("@UnitPrice", row["UnitPrice"].ToPrice());
cmd.Parameters.AddWithValue("@Service", row["Service"].ToString());
cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
cmd.Parameters.AddWithValue("@SI", row["SI"].ToString());
cmd.Parameters.AddWithValue("@TotalPrice", row["TotalPrice"].ToString());
cmd.ExecuteNonQuery();
}
}
trans.Commit();
return true;
}
catch (Exception)
{
trans.Rollback();
return false;
}
}
这段代码执行没有任何错误,但它没有删除记录,也没有将记录作为重复记录插入。
我按照保罗所说的解决了这个问题。 OrderId 的值无效。
你不应该使用 "string.format"。你应该命名参数。
你可以写一个基本的验证器。
if(this.OrderID <= 0)
{
throw new ArgumentOutOfRangeException("Please provide an OrderID");
}
OleDbCommand Command = new OleDbCommand();
Command.Connection = Connection;
OleDbParameter paramOrderId = new OleDbParameter();
Parameter1.OleDbType = OleDbType.VarChar;
Parameter1.ParamterName = "@OrderId";
Parameter1.Value = this.OrderID;
Command.Parameters.Add(paramOrderId );
我正在开展一个项目,在该项目中我使用的是 Access 2007 数据库 (.accdb
) 而不是 SQL 服务器。为了执行 CRUD 操作,我使用 MS Access 的查询,因为我以前的代码是使用存储过程编写的。我也在使用 SQL 交易。
这是我的代码:
public int OrderId { get; set; }
public string OrderDate { get; set; }
public int ServiceMode { get; set; }
public string DeliveryDate { get; set; }
public int ClientId { get; set; }
public int TotalQty { get; set; }
public decimal TotalAmount { get; set; }
public int Discount { get; set; }
public decimal DiscAmount { get; set; }
public decimal NetTotal { get; set; }
public decimal Advance { get; set; }
public decimal Due { get; set; }
public bool Update(DataTable itemList)
{
using (var conn = new OleDbConnection(Sniper.ConnString))
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
using (var cmd = new OleDbCommand())
{
cmd.Transaction = trans;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = string.Format("DELETE FROM OrderMaster WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format("DELETE FROM OrderDetails WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
cmd.CommandText = string.Format("DELETE FROM OrderStatus WHERE OrderId={0}", OrderId);
cmd.ExecuteNonQuery();
}
using (var cmd = new OleDbCommand("qry_OrderMaster_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@OrderDate", OrderDate);
cmd.Parameters.AddWithValue("@ServiceMode", ServiceMode);
cmd.Parameters.AddWithValue("@DeliveryDate", DeliveryDate);
cmd.Parameters.AddWithValue("@ClientId", ClientId);
cmd.Parameters.AddWithValue("@TotalQty", TotalQty);
cmd.Parameters.AddWithValue("@TotalAmount", TotalAmount);
cmd.Parameters.AddWithValue("@Discount", Discount);
cmd.Parameters.AddWithValue("@DiscAmount", DiscAmount);
cmd.Parameters.AddWithValue("@NetTotal", NetTotal);
cmd.Parameters.AddWithValue("@Advance", Advance);
cmd.Parameters.AddWithValue("@Due", Due);
cmd.ExecuteNonQuery();
}
using (var cmd = new OleDbCommand("qry_OrderStatus_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@IsDelivered", 0);
cmd.Parameters.AddWithValue("@IsCancelled", 0);
cmd.ExecuteNonQuery();
}
foreach (DataRow row in itemList.Rows)
{
using (var cmd = new OleDbCommand("qry_OrderDetails_Insert", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;
cmd.Parameters.AddWithValue("@OrderId", OrderId);
cmd.Parameters.AddWithValue("@ItemId", row["ItemId"].ToInt());
cmd.Parameters.AddWithValue("@Qty", row["Qty"].ToInt());
cmd.Parameters.AddWithValue("@UnitPrice", row["UnitPrice"].ToPrice());
cmd.Parameters.AddWithValue("@Service", row["Service"].ToString());
cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
cmd.Parameters.AddWithValue("@SI", row["SI"].ToString());
cmd.Parameters.AddWithValue("@TotalPrice", row["TotalPrice"].ToString());
cmd.ExecuteNonQuery();
}
}
trans.Commit();
return true;
}
catch (Exception)
{
trans.Rollback();
return false;
}
}
这段代码执行没有任何错误,但它没有删除记录,也没有将记录作为重复记录插入。
我按照保罗所说的解决了这个问题。 OrderId 的值无效。
你不应该使用 "string.format"。你应该命名参数。 你可以写一个基本的验证器。
if(this.OrderID <= 0)
{
throw new ArgumentOutOfRangeException("Please provide an OrderID");
}
OleDbCommand Command = new OleDbCommand();
Command.Connection = Connection;
OleDbParameter paramOrderId = new OleDbParameter();
Parameter1.OleDbType = OleDbType.VarChar;
Parameter1.ParamterName = "@OrderId";
Parameter1.Value = this.OrderID;
Command.Parameters.Add(paramOrderId );