这个是自己用的,不一定符合您的需求。
using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateMaterialCode { /// <summary> /// 获取新物料编号(要查询的字段,物料分类编号,要查询的数据库表名);编码为7位 /// </summary> /// <param name="Fieldname"></param> /// <param name="MaterialTypeID"></param> /// <param name="TableName"></param> /// <returns></returns> public static string GetNewCode(EXDataControl.EXDataCenter dc, string Fieldname, string ClassCode, string TableName) { if (string.IsNullOrEmpty(Fieldname) == true || string.IsNullOrEmpty(ClassCode) == true || string.IsNullOrEmpty(TableName) == true) { Utility.MsgBox.MessageShowErrors("GetNewCode方法的参数传递有空值!系统将返回null值。"); return null; } //参数 string shortCode = ""; string strClassCode = ClassCode; string tableName = TableName; string key_fieldname = Fieldname; //查找到的最大物料编号 string oldMaxCode = ""; #region 取出最大编号 //string sqlSearch2 = "select Max(" + key_fieldname + ")as DocNum from " + tableName + " Where " + key_fieldname + " like‘" + shortCode + "%‘"; string sqlSearch2 = "select Max(" + key_fieldname + ")as DocNum from " + tableName + " Where " + key_fieldname + " like‘" + strClassCode + "[^A-z]%‘"; //第N位不为字母 DataTable dtTemp2 = new DataTable(); try { dtTemp2 = dc.getDataSetBySql(sqlSearch2).Tables[0]; if (dtTemp2.Rows.Count > 0) { oldMaxCode = dtTemp2.Rows[0]["DocNum"].ToString(); } } catch (Exception ex) { Utility.MsgBox.MessageShowErrors("查询最大旧编号错误:" + ex.ToString()); return null; } #endregion //将取出的物料编号处理 //如果没找到,则默认为什么,否则累计增1 string startnum = "1"; if ((oldMaxCode == null) || (string.IsNullOrEmpty(oldMaxCode.ToString()) == true)) { try { startnum = "1"; //取得起始值 } catch { } } else { string maxID = oldMaxCode.ToString(); string oldnum = maxID.Substring(strClassCode.Length); //截去字母位长度 int newnum = int.Parse(oldnum) + 1; startnum = newnum.ToString(); } startnum = "0000000000" + startnum; //10位编码 string NewCode = strClassCode + startnum.Substring(startnum.Length - (10 - strClassCode.Length)); //从指定位置,取指定长度(startnum-10的位置开动始,取10位) return NewCode; } } }
.Net常用技巧_生成物料编号(根据分类代码生成),布布扣,bubuko.com
时间: 2024-10-15 00:35:56