.Net常用技巧_生成物料编号(根据分类代码生成)

这个是自己用的,不一定符合您的需求。

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

.Net常用技巧_生成物料编号(根据分类代码生成)的相关文章

.Net常用技巧_生成单据号

自己用的,没整理,代码比较乱,请不要学我. using System; using System.Collections.Generic; using System.Text; using EXDataControl; using System.Data; using System.Data.SqlClient; using Utility; namespace MyTool { public class CreateDocNo { /// <summary> /// 获取单号 /// <

.Net常用技巧_操作Excel知识点

C#操作Excel知识点 近期在使用C#操作excel,主要是读取excel模板,复制其中的模板sheet页,生成多个sheet页填充相应数据后另存到excel文件,所用到的知识点如下. 一.添加引用和命名空间 添加Microsoft.Office.Interop.Excel引用,它的默认路径是C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Off

.Net常用技巧_汉字转拼音类(除汉字外,其余的原样输出)

注:如果是多音字,则默认取第一个拼音. using System; using System.Collections.Generic; using System.Text; using Microsoft.International.Converters.PinYinConverter; namespace MyTool { /// <summary> /// 汉字转拼音类 /// </summary> public class PinYinConver { public PinY

.Net常用技巧_软件注册码

using System; using System.Collections.Generic; using System.Text; using System.Management; using Utility; using EXDataControl; using System.Collections; using Microsoft.Win32; namespace MyTool { public class SoftReg { /// <summary> /// 获取硬盘卷标号 ///

.Net常用技巧_导出 Excel 和相关打印设置

Excel.Application myExcel = new Excel.Application();发 表Excel.Workbook workbookData = myExcel.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);Excel.Worksheet xlSheet = (Worksheet)workbookData.Worksheets[1];//取得sheet1 1) 显示当前窗口: xlSheet.

.Net常用技巧_读取XML某节点例子

注:此例子只是自己在代码中为了读某固定的几个值,写的有点死,所以另作他用的时候请自行修改或扩充 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; namespace MyTool {

.Net常用技巧_传入DataGrid直接导出成Excel

注:非调用OFFICE的DLL方法. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Utility; using System.IO; names

.net常用技巧_窗体中回车键转TAB键

在窗体的KeyDown()事件中 /// <summary> /// 表单的回车转TAB键 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BasicForm_KeyDown(object sender, KeyEventArgs e) { #region 回车转

.Net常用技巧_获取SQL Server表字段的各种属性

-- SQL Server 2000 SELECT a.name AS 字段名, CASE WHEN EXISTS (SELECT 1 FROM sysobjects WHERE xtype = 'PK' AND parent_obj = a.id AND name IN (SELECT name FROM sysindexes WHERE indid IN (SELECT indid FROM sysindexkeys WHERE id = a.id AND colid = a.colid))