码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-16 23:15:12      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   数据   2014   

自己用的,没整理,代码比较乱,请不要学我。

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常用技巧_生成单据号,布布扣,bubuko.com

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

标签:style   blog   color   os   数据   2014   

原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!