1
我有一個空的數據庫。 我想添加多個記錄到數據庫中。 雖然插入記錄到數據庫我想檢查我的產品插入在同一日期donot再次添加它(我想改變它的一些它的提交和更新它的內容)。 我用這個代碼,但它只是添加一些數據到數據庫(它不能檢查現有產品。)同時插入並更新多個項目到數據庫
var AllData = ClsDataBase.Database.InsertProductTbls;
foreach(item in AllData)
{
//Update
if (Exist(datefa))
{
var query = ClsDataBase.Database.CustomerProductTbls.SingleOrDefault
(data => data.CustomerId == AllData .CustomerId);
int? LastProductTotal = query.CustomerProducTtotal;
query.CustomerProducTtotal = LastProductTotal + ClsInsertProduct._InsertProductNumber;
}
//Insert
else
{
_CustomerProductTbl = new CustomerProductTbl();
_CustomerProductTbl.CustomerId = AllData ._CustomerId;
_CustomerProductTbl.CustomerProductDateFa = AllData.datefa
.
.
.
ClsDataBase.Database.AddToCustomerProductTbls(_CustomerProductTbl);
}
}
}
ClsDataBase.Database.SaveChanges();
如果我使用ClsDataBase.Database.SaveChanges();
兩個更新和插入部分,我將返回此錯誤:
An error occurred while starting a transaction on the provider connection. See the inner exception for details.
請幫忙。
您發佈的文字只是說有一個內部異常。請張貼它也。 – sloth 2012-08-07 07:46:17
準確地說,我想添加多個字段到數據庫中,同時添加檢查存檔日期存在,如果存在donot添加it.any解決方案? – KF2 2012-08-07 08:02:44