首页 > 代码库 > .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> /// 获取单号 /// </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 = http://www.mamicode.com/""; string sqlSearch = ""; string MaxDoc = ""; //当天日期 object objData = http://www.mamicode.com/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; } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。