我有一个名为 Inventory 的表,我想删除它的第一行。为此,我创建了一个名为 InventoryDAL 的类。这是代码:
public class InventoryDAL
{
private string cnString = string.Empty;
private SqlDataAdapter da = null;
public InventoryDAL(string connectionString)
{
cnString = connectionString;
da = new SqlDataAdapter("Select CarID, Make, Color, PetName From Inventory",
connectionString);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.DeleteCommand = builder.GetDeleteCommand();
da.InsertCommand = builder.GetInsertCommand();
da.UpdateCommand = builder.GetUpdateCommand();
}
public DataTable Inventory()
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public void UpdateInventory(DataTable modifiedTable)
{
da.Update(modifiedTable);
}
}
我还创建了一个小程序来尝试它:
class Program
{
static void Main(string[] args)
{
InventoryDAL inv = new InventoryDAL(@"Data Source=MYPC;Initial Catalog=AutoLot;Integrated Security=True;Pooling=False");
DataTable dt = inv.Inventory();
dt.Rows.RemoveAt(0);
inv.UpdateInventory(dt);
Console.ReadKey(true);
}}
但它不起作用。经过一些尝试,我意识到.Update()
只有在插入数据时才有效。