15
2012
04

获取某个表中的ID,得到下一个ID值

通过查询某个表中最大ID值,判断如果ID值不为空,也就是该表中已经有值,则根据该值得到下一个的值(如果有值有6为的,使用(6-length)得到需要在整数值前面加多少个“0”),如果ID值为空,则返回“000001”六位的下一个ID值
public string GetID()
{
object MaxId = null;
string id = "";
try
{
String sql = "select Max(Convert(int,ID)) from Table";
MaxId = DataClass.GetOneValue(getConnectionString(), sql);
if (MaxId != null && MaxId.ToString().Trim() != "")
{
int lenght = MaxId.ToString().Length;
id = Convert.ToString(Convert.ToInt32(MaxId.ToString().Trim()) + 1);
for (int i = 0; i < 6 - lenght; i++)
{
id = "0" + id;
}
}
else
{
id = "000001";
}
}
catch (Exception ex)
{
throw ex;
}
return id;
}
« 上一篇下一篇 »

相关文章:

一个用C#获取硬件信息的类  (2012-4-4 10:58:15)

C# Winfrom中获取当前日期  (2012-3-28 21:2:16)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。