.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>
        /// 获取单号
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="TableName">数据表名</param>
        /// <param name="DocField">单据字段</param>
        /// <param name="FirstCode">前缀字母代号</param>
        /// <returns></returns>
        public static string GetNewDocNo(EXDataControl.EXDataCenter dc, string TableName, string DocField, string FirstCode)
        {
            //select max(CustomerOrder) from cs_CustomerOrderMaster where CustomerOrder like‘LF20140605%‘  --LF20140605011

            if (string.IsNullOrEmpty(TableName) == true || string.IsNullOrEmpty(DocField) == true)
            {
                Utility.MsgBox.MessageShowErrors("调用生成单号方法时,参数有空值!系统将返回null值。");
                return null;
            }

            string DocNo = "";
            string CurrentData = "";
            string sqlSearch = "";
            string MaxDoc = "";

            //当天日期
            object objData = dc.ExecuteScalar("Select CONVERT(varchar(100), GETDATE(), 112)");   //取时间20120102
            if (objData != null)
            {
                if (Convert.ToString(objData).Length > 0)
                {
                    CurrentData = Convert.ToString(objData);
                }
            }

            //最大单据号,SQL语句;是否有前缀,有则加上日期
            if (string.IsNullOrEmpty(FirstCode) == false)
            {
                FirstCode = FirstCode + CurrentData;
                sqlSearch = "select max(" + DocField + ") from " + TableName + " where " + DocField + " like‘" + FirstCode + "%‘";
            }
            else
            {
                FirstCode = CurrentData;
                sqlSearch = "select max(" + DocField + ") from " + TableName + " where " + DocField + " like‘" + FirstCode + "%‘";
            }

            //最大单据号,当天最大值
            object objMaxDoc = dc.ExecuteScalar(sqlSearch);   //取当前日期最大值,//要加下划线
            if (objMaxDoc != null)
            {
                if (Convert.ToString(objMaxDoc).Length > 0)
                {
                    MaxDoc = Convert.ToString(objMaxDoc);
                }
            }

            if (string.IsNullOrEmpty(MaxDoc) == false)
            {
                //后4位加1
                string strNum = MaxDoc.Substring(MaxDoc.Length - 4, 4);
                strNum = Convert.ToString(int.Parse(strNum) + 1);
                strNum = "0000" + strNum;
                strNum = strNum.Substring(strNum.Length - 4, 4);

                //前缀 + 日期 + “_” + 后4位(CurrentData前面查询时已加日期)
                //DocNo = FirstCode + CurrentData + "_" + strNum;

                //DocNo = FirstCode + "_" + strNum;

                DocNo = FirstCode + strNum;
            }
            else
            {
                //前缀 + 日期 + “_” + 后4位(CurrentData前面查询时已加日期)
                //DocNo = FirstCode + "_" + "0001";
                DocNo = FirstCode + "0001";
            }
            return DocNo;
        }

    }
}

 

 

.Net常用技巧_生成单据号,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。