请教各位,现在我想打印产品流水号,流水号前面字符固定为ZKS-,后面为000000至999999,
即ZKS-000000至ZKS-999999,该如何写,麻烦大家了!

解决方案 »

  1.   

    第一有个流水帐号ID 自动增加+1型 
     
    create TABLE test
    (
    codeID Int IDENTITY(1,1)
    primary key(codeID)
    )
    select 'ZKS-' + right('000000'+ Convert(varchar(1000), codeID ),6) youCode from test
    drop table test
      

  2.   


    create TABLE test
    (
    codeID Int IDENTITY(1,1),
    value int
    primary key(codeID)
    )
    insert into test (value) values(1)
    insert into test (value) values(2)
    insert into test (value) values(3)
    insert into test (value) values(4)
    select 'ZKS-' + right('000000'+ Convert(varchar(1000), codeID ),6) youCode from test
    drop table test
      

  3.   

    填充,或者添加的时候就填充好
    create TABLE aaa
    (
    codeID Int IDENTITY(1,1),
    value int
    primary key(codeID)
    )
    go
    insert into aaa (value) values(1)
    insert into aaa (value) values(2)
    insert into aaa (value) values(3)
    insert into aaa (value) values(4)
    goselect 'ZKS-' + right('000000'+ Convert(varchar(1000), codeID ),6) youCode from aaa
    go
    drop table aaa
    go
      

  4.   


    /*创建表BillNo*/
    if exists (select * from sysobjects where name='BillNo')
    drop table BillNo
    go
    CREATE TABLE [dbo].[BillNo](
    [BillName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,/*前缀字符串,如'ZKS-',*/
    [CurrentNo] [int] NOT NULL,/*当前序号*/
    [Length] [int] NOT NULL,/*后缀序号长度*/
     CONSTRAINT [PK_BillNo] PRIMARY KEY CLUSTERED 
    (
    [BillName] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]
    go
    /*插入*/
    insert into BillNo(BillName,CurrentNo,Length) values ('ZKS-',0,6)
    go/*创建存储过程*/
    if exists (select * from sysobjects where name='GetBillNo' and type='p')
    drop procedure GetBillNo
    gocreate procedure GetBillNo 
    @BillName nvarchar(50)/*前缀字符串,如'ZKS-',这要和BillNo表中的BillName相对应*/
    as declare @currentno int
    declare @currentstring nvarchar(10)
    declare @NoLength int
    /*获得当前号码*/
    select @currentno=CurrentNo,@NoLength=Length from BillNo where BillName=@BillName
    /*如果BillNo表存在前缀为@BillName*/
    if(@currentno is not null)
    begin
    set @currentno=@currentno+1
    update BillNo set CurrentNo=@currentno where BillName=@BillName
    end
    /*BillNo表不存在前缀为@BillName,永远返回0*/
    else set @currentno=0set @currentstring=convert(nvarchar(10[code=C#]),@currentno)
    while(len(@currentstring)<@NoLength)
    set @currentstring='0'+@currentstring;
    select @BillName+@currentstringgoexec GetBillNo 'ZKS-'[/code]
    这种方法,复用性好一点,只要在BillNo表中添加不同的编码规则就可以了.你还可以对BillNo表进行扩展,如单据编号需要时间,你可以添加时间字段,然后修改上面的存储过程,反正思路是这样的.
      

  5.   

    不好意思,上面代码后面部分贴的有问题
    set @currentstring=convert(nvarchar(10),@currentno) 
    while(len(@currentstring) <@NoLength) 
        set @currentstring='0'+@currentstring; 
    select @BillName+@currentstring as CurrentBillNogo exec GetBillNo 'ZKS-' 
      

  6.   

    C#方法    public string GetCode(int codeID)
        {
            string returnStr = "";
            returnStr = "000000" + codeID.ToString();
            returnStr = returnStr.Substring((returnStr.Length - 7), (returnStr.Length - 1));
            return returnStr;    }sql的方法一样
      

  7.   

    楼上哥们写的方法还有所欠妥,CodeID加到returnStr时,进位出现问题
    理想中应该是这样:如codeID为6时,returnStr为000006;codeID为123时,returnStr为000123
      

  8.   

    那就更简单了///id是序号
    ///length是长度
    public string GetID(int id,int length)
    {
       string strID =id.ToString();
       strID=strID.PadLeft(length, '0');
       return strID;
                
    }
      

  9.   

    这次我把问题说清楚,产品流水号由固定值ZKS-(降低难度)和6位自加数字组成,每生产一个产品,流水号就加1,但数字始终是6位(即最大值为ZKS-999999,最小值为ZKS-000000),如生产了第24个产品,则流水号为ZKS-000024,第25个产品的流水号为ZKS-000025,循环自加,但不会重复。
      

  10.   

            /// <summary>
            /// 获取固定长度数字字符串
            /// </summary>
            /// <param name="number">要格式化的数字</param>
            /// <param name="length">格式化后字符串长度</param>
            /// <returns></returns>
    public static string GetString(int number, int length)
            {
                string result = number.ToString();
                int len = result.Length;
                for (int i = 0; i < length - len; i++)
                {
                    result = "0" + result;
                }
                return result;
            }
      

  11.   

    1.查询目前最后一个产品的流水号,获取后6位转换为int。
    2.将转换后的数字加1。
    3.设置新流水号"ZKS-"+ newInt.toString("#####0")。
    4.将新流水号插入数据库。