2016-09-23 64 views
0

该id将递增1,如00001.但是无论何时插入新数据,增量都会重新启动。我希望它从最后一个增量继续。Id的增量

查询

string GetMaxIdSql = 
@"select isnull(Max(convert(int, ApplicationId)),0) as ApplicationId, isnull(Max(convert(int, Id)),0) as Id from Application where isnumeric(id) = 1 and isnumeric(applicationid) = 1"; 

命令

resultMaxId.Id = (Convert.ToInt32(resultMaxId.Id) + 1).ToString();   
string id = string.Format("{0}", resultMaxId.Id.ToString().PadLeft(6, '0')); 
+0

问这可能很愚蠢,但是你确定数据库表中的'ApplicationId'和'Id'列是'int'类型而不是'varchar',或者它们没有不可转换的值不能转换为int)。 –

+0

它可能是因为这些列是字母数字,并试图将它们转换为“int”导致错误。 –

+2

如果您的表中存在垃圾数据(其中ID或ApplicationID由于包含字母或其他字符而不是数字)无法转换,则会发生错误。你可以改变查询来包含'where isnumeric(id)= 1和isnumeric(applicationid)= 1',但实际上你应该修复你的数据。 – ZLK

回答

0

请检查的applicationID和Id列完整的内容。它可能有一些导致此问题的字符。