public static string AutoCreatID(string TableName, string FieldName) { string dbDateTime = DateTime.Now.ToString("yyyyMMdd"); string Str = "select max(" + FieldName + ") as id from " + TableName; //查询表中的字段 object obj = SQLDBHelper.GetSingle(Str); //把查询字符串Str放到GetSingle()中执行查询(Command)并返回查询结果(对象) string maxID = ""; if (obj != null)//返回(查询字符串)对象 { maxID = SQLDBHelper.GetSingle(Str).ToString(); } string Result = ""; if (maxID == "") //没有最大编号 { Result = dbDateTime + "0001"; //CG200902250001 } else { //截取字符 //string strFirstEight = maxID.Substring(4, 8); string strLastFour = maxID.Substring(8, 4); //if (dbDateTime == strFirstEight)//截取的最大编号(20090225)是否和数据库服务器系统时间相等 //{ string strNewFour = (Convert.ToInt32(strLastFour) + 1).ToString("0000"); //0000+1 Result = dbDateTime + strNewFour; //CG200902250001 //} //else //{ // Result = dbDateTime + "0001"; //} } return(Result); }
public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = SQLDBHelper.GetSingle(strsql); if (obj == null) { return(1); } else { return(int.Parse(obj.ToString())); } }
public static string AutoNumber(string TableName, string FieldName) { string Str = "select max(" + FieldName + ") as id from " + TableName; //查询表中的字段 object obj = SQLDBHelper.GetSingle(Str); //把查询字符串Str放到GetSingle()中执行查询(Command)并返回查询结果(对象) string maxID = ""; if (obj != null)//返回(查询字符串)对象 { maxID = SQLDBHelper.GetSingle(Str).ToString(); } string Result = ""; if (maxID == "")//没有最大编号 { Result = "0001"; } else { string strNewFour = (Convert.ToInt32(maxID) + 1).ToString("0000"); //0000+1 Result = strNewFour; //CG200902250001 } return(Result); }