点滴积累【C#】---检验编号在本表中自动生成,与其他表无关
检验编号在本表中自动生成,与其他表无关
效果:
描述:在本表中自动生成编号,与其他表无关。
调用:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 Response.Expires = -1; 4 if (!IsPostBack) 5 { 6 if (ActionType == CommonEnum.ActionLevel.Write) 7 { 8 tbsybh.Text = Getsybh(); 9 } 10 } 11 }
1 protected string Getsybh() 2 { 3 string Getsybh = CommonFunction.GetByMenuCode(ThisPositionID, "hxty_lydj", "sybh", 5); ; 4 return Getsybh; 5 }
函数代码:
1 public static string GetByMenuCode(string ThisPositionID, string TableName, string RowName, int Digit) 2 { 3 string GetCode = string.Empty; 4 string Title = string.Empty; 5 string sql = string.Format("SELECT Max(RIGHT({0}," + Digit + ")+1) as RowName,LEFT(Max({0}),LEN(Max({0}))-" + Digit + ") as RowTitle FROM {1} ", RowName, TableName); 6 DbCommand sqlCommand = ERPDataBase.ERPDB.GetSqlStringCommand(sql); 7 DataSet dsData = new DataSet(); 8 ERPDataBase.ERPDB.LoadDataSet(sqlCommand, dsData, "data"); 9 if (dsData.Tables["data"].Rows.Count > 0) 10 { 11 DataRow row = dsData.Tables["data"].Rows[0]; 12 if (row["RowName"] == DBNull.Value) 13 { 14 GetCode = "1"; 15 GetCode = GetCode.PadLeft(Digit, '0'); 16 } 17 else 18 { 19 string second = row["RowName"].ToString(); 20 GetCode = second.PadLeft(Digit, '0'); 21 } 22 if (row["RowTitle"] != DBNull.Value) 23 { 24 Title = row["RowTitle"].ToString(); 25 } 26 } 27 return Title + GetCode; 28 }