0
我在數據庫表中插入數據列表。每件事情都很好,但是當我在數據庫中看到我的表時,它是空的!它沒有顯示任何記錄。C#For循環在事務中但沒有插入數據
這裏是我的功能:
public int addBulkLeadStages(List<LeadStage> allLeadStages)
{
//throw new NotImplementedException();
SqlConnection connection;
SqlCommand cmd;
int effectedRows = 0;
int rowsCount = 0;
int updatedRowsCount = 0;
using (TransactionScope trans = new TransactionScope())
{
using (connection = new SqlConnection(SalesForceDBManager.getConnectionString(SalesForceDB.CONNECTION_STRING)))
{
//Open Connection
connection.Open();
//Craete Command
cmd = connection.CreateCommand();
int noOfLeadStages = allLeadStages.Count;
for (int i = 0; i < noOfLeadStages; i++)
{
//Check If entry already available
if (isLeadStageRecordExist(allLeadStages[i].ID,connection))
{
//Update Existing Record
updatedRowsCount = updateLeadStage(connection, allLeadStages[i].ID, allLeadStages[i]);
}
else
{
cmd.CommandText = "INSERT INTO " + SalesForceDB.LeadStage.TABLE_LEAD_STAGE + "(" +
SalesForceDB.LeadStage.COLUMN_ID + "," + SalesForceDB.LeadStage.COLUMN_NAME + "," +
SalesForceDB.LeadStage.COLUMN_COMMENTS_REMARKS + "," + SalesForceDB.LeadStage.COLUMN_ENTRY_POINT + "," +
SalesForceDB.LeadStage.COLUMN_EXIT_POINT + "," + SalesForceDB.CommonColumns.COLUMN_IS_ACTIVE + "," +
SalesForceDB.LeadStage.COLUMN_SEQUENCE_NO + "," + SalesForceDB.LeadStage.COLUMN_STAGE_DESCRIPTION + "," +
SalesForceDB.CommonColumns.COLUMN_CREATED_BY + "," + SalesForceDB.CommonColumns.COLUMN_CREATED_DATE + "," +
SalesForceDB.CommonColumns.COLUMN_LAST_MODIFIED_BY + "," + SalesForceDB.CommonColumns.COLUMN_LAST_MODIFIED_DATE + ")" +
" VALUES (@ID,@StageName,@CommentsRemarks,@EntryPoint,@ExitPoint,@IsActive,@SequenceNo,@StageDesc,@CreatedBy,@CreatedDate," +
"@LastModifiedBy,@LastModifiedDate)";
//Adding Command Parameters
cmd.Parameters.AddWithValue("@ID", allLeadStages[i].ID);
cmd.Parameters.AddWithValue("@StageName", allLeadStages[i].Name);
cmd.Parameters.AddWithValue("@CommentsRemarks", allLeadStages[i].Comments);
cmd.Parameters.AddWithValue("@EntryPoint", allLeadStages[i].EntryPoint);
cmd.Parameters.AddWithValue("@ExitPoint", allLeadStages[i].ExitPoint);
cmd.Parameters.AddWithValue("@IsActive", allLeadStages[i].IsActive);
cmd.Parameters.AddWithValue("@SequenceNo", allLeadStages[i].SequenceNo);
cmd.Parameters.AddWithValue("@StageDesc", allLeadStages[i].Description);
cmd.Parameters.AddWithValue("@CreatedDate", allLeadStages[i].CreatedDate);
cmd.Parameters.AddWithValue("@CreatedBy", allLeadStages[i].CreatedBy);
cmd.Parameters.AddWithValue("@LastModifiedBy", allLeadStages[i].LastModifiedBy);
cmd.Parameters.AddWithValue("@LastModifiedDate", allLeadStages[i].LastModifiedDate);
//Execute query
effectedRows = cmd.ExecuteNonQuery();
rowsCount += effectedRows;
cmd.Parameters.Clear();
}
}
}
return rowsCount;
}
}
這是我的連接字符串。
Data Source=FAIZAN;Initial Catalog=SalesForceBuild015;Integrated Security=SSPI
它工作正常!輸入列表包含11條記錄,輸出精確的11位數,但數據庫表爲空!我無法弄清楚爲什麼。我在這裏錯過了什麼?
可以使用TransactionScope嗎?
謝謝!
您需要提交事務,默認情況下,如果未執行顯式提交,它們將回滾。 –