【发布时间】:2016-11-24 02:28:49
【问题描述】:
我的angularjs controller 中有一个foreach loop
AccountController
angular.forEach($scope.accountList,function (account) {
AccountService.insertNewAccount(account).then(function (msg)
//some codes
});
});
在我的 AccountController.cs 中
int accountNo = GenerateRandomNo();
var chk = db.sp_check_account_number(accountNo).ToList(); //check if there's redudancy in generated AccountNumber in Account Table
var cnt = chk.Count;
if (cnt < 1)//If there is no redundancy
{
db.InsertNewAccount(name, accountNo);
}
else
{
//if there's redundancy generate another account number
while(cnt!=0)
{
accountNo = GenerateRandomNo();
chk = db.GetROPAAccount(accountNo).ToList();
cnt = chk.Count;
}
db.InsertNewAccount(name, accountNo);
}
db.SaveChanges();
我在AccountController.cs 中的插入方法运行良好,但问题是当它完成插入来自$scope.accountList 数组的数据时,表记录中插入的AccountNumber 有很多冗余。我发现我的foreach loop 随机插入了数据,我不知道为什么。我还检查了我的sp_check_account_number,它工作正常。有人可以帮我弄这个吗? :(
【问题讨论】:
-
“有很多冗余”是什么意思?
-
我们可以查看您的
GenerateRandomNo()代码吗? -
示例我插入了 10 条记录,大部分插入的帐号都重复或一式三份。而且我不知道为什么会这样,因为我有存储过程
sp_check_account_number检查生成的帐号中是否存在重复 -
这是我在
GenerateRandomNo()public int GenerateRandomNo() { int min = 1000; int max = 9999; Random rdm = new Random(); return rdm.Next(min, max); }中的代码 -
这是您的问题 -
Random rdm = new Random();- 如果快速连续调用,您通常会得到相同的种子值,因此这些值不是随机的。将此行作为类级变量移出,然后重试。到时候就可以了。
标签: c# angularjs asp.net-mvc-4 sql-server-2012