我有一个Web服务器运行了两年(SQL Server 2017和ASP.NET Web表单)
但这两天出现了重复插入数据。
插入数据前查找数据,检查数据是否为非空,是否为重复数据中的SQL server。
然而,我无法找出解决这个问题的最佳做法。
工具箱.cs#
public string getID(string PId, string TestId)
{
string idCode = "";
char FillUp = '0';
int length = 4;
int one = 0;
if (PId.Length != 10)
{
throw new NullReferenceException("Error");
}
string newId = PId.Replace("-", "");
if (TestId == null)
{
idCode = newId + "0001";
}
else
{
string checkID = TestId.Substring(11, 4);
int trynumber;
int.TryParse(checkID , out trynumber);
int firstNumber = trynumber + 1;
string number = firstNumber.ToString();
string idNumber = number.PadLeft(length, FillUp);
idCode = newId + idNumber;
}
return idCode;
}
添加数据.ashx
using (Entity EntityData= new Entity())
{
var checkID = EntityData.TEST_DATA.AsNoTracking()
.Where(o => o.NUMBER_NO == XXXX_NO)
.OrderByDescending(o => o.ID)
.FirstOrDefault();
toolbox tool = new toolbox();
if (checkID == null)
{
ID = tool.getID(getPID, null);
}
else
{
ID = tool.getID(getPID, checkID.ID);
}
TEST_DATA addDATA = new TEST_DATA();
addDATA.ID = ID;
addDATA.NUMBER_NO = XXXX_NO;
addDATA.RECDATE = DateTime.Now;
addDATA.MODIFY_TIME = DateTime.Now;
EntityData.TEST_DATA.Add(addDATA);
EntityData.SaveChanges();
}
1条答案
按热度按时间bzzcjhmw1#
SQL Server自动添加空白,但C-Sharp获取数据代码不是错误