我正在制作一个连接到MS SQL 2008数据库的小型C#程序。 我有以下功能,而我使用一个simmilar来将新数据插入数据库,当我想更新它时,数据库中没有任何变化。我错过了什么吗?
public bool UpdateItem(int invID, int serialNum, string catA, string catB, string itemName, int quantity,
string supplierName, float cost, float price, string comments)
{
_cmd.CommandText = "UPDATE inventory SET serialNum=@serialNum,catID=@catID,itemName=@itemName,quantity=@quantity,supplierId=@supplierId,cost=@cost,price=@price,comments=@comments WHERE invID=@invID;";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@serialNum",serialNum);
_cmd.Parameters.AddWithValue("@catID", getCatID(catA, catB));
_cmd.Parameters.AddWithValue("@itemName",itemName);
_cmd.Parameters.AddWithValue("@quantity",quantity);
_cmd.Parameters.AddWithValue("@supplierId",GetSupplierIdByName(supplierName));
_cmd.Parameters.AddWithValue("@cost",cost);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@comments", comments);
_cmd.Parameters.AddWithValue("@invID", invID);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
return true;
}
这是数据库表结构
table inventory (
invID int IDENTITY(1,1) primary key,
serialNum int not null,
catID int not null,
itemName nvarchar(20) not null,
quantity int not null,
supplierId int not null,
cost real not null,
price real not null,
comments nvarchar (50),
limbo BIT,
foreign key (supplierId)
references suppliers,
foreign key (catID)
references category)
插入
public bool AddNewItem(int serialNum, int catID, string itemName, int quantity,
int supplierId, float cost, float price, string comments)
{
_cmd.CommandText =
"INSERT INTO inventory (serialNum,catID ,itemName ,quantity ,supplierId ,cost ,price ,comments ,limbo ) VALUES " +
"(@serialNum,@catID ,@itemName ,@quantity ,@supplierId ,@cost ,@price ,@comments ,@limbo );";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@serialNum", serialNum);
_cmd.Parameters.AddWithValue("@catID", catID);
_cmd.Parameters.AddWithValue("@itemName", itemName);
_cmd.Parameters.AddWithValue("@quantity", quantity);
_cmd.Parameters.AddWithValue("@supplierId", supplierId);
_cmd.Parameters.AddWithValue("@cost", cost);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@comments", comments);
_cmd.Parameters.AddWithValue("@limbo", 0);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
return true;
}
答案 0 :(得分:-1)
public bool AddNewItem(int serialNum, int catID, string itemName, int quantity,
int supplierId, float cost, float price, string comments)
{
_cmd.CommandText =
"INSERT INTO inventory (serialNum,catID ,itemName ,quantity ,supplierId ,cost ,price ,comments ,limbo ) VALUES " +
"(@serialNum,@catID ,@itemName ,@quantity ,@supplierId ,@cost ,@price ,@comments ,@limbo );";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@serialNum", serialNum);
_cmd.Parameters.AddWithValue("@catID", catID);
_cmd.Parameters.AddWithValue("@itemName", itemName);
_cmd.Parameters.AddWithValue("@quantity", quantity);
_cmd.Parameters.AddWithValue("@supplierId", supplierId);
_cmd.Parameters.AddWithValue("@cost", cost);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@comments", comments);
_cmd.Parameters.AddWithValue("@limbo", 0);
_con.Open();
_cmd.Connection = _con; /* Set Connection for the command*/
_cmd.ExecuteNonQuery();
_con.Close();
return true;
}