码迷,mamicode.com
首页 > 其他好文 > 详细

自动生成流水线号

时间:2015-04-20 10:54:40      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

1、使用C#自动生成四位凭证号

//如果没有数据则返回结果为0,如果有则取出最大的id值
DataTable dtPnum =sqlhelper.ExecuteDataTable("select case when max(a_id) is null then‘0‘ end as id FROM tb_account_base");
//准备插入的id为刚刚返回的id+1
int pnum =Convert.ToInt32(dtPnum.Rows[0][0]) + 1;
//从右向左截取4位字符串,显示至页面TextBox
string str = "0000" +pnum.ToString();
labPnum.Text =str.Substring(str.Length - 4, 4);
 
*从右向左截取字符串的一般写法
str.Substring(str.Length -i, i);
str.Length -i:从倒数第几位开始截取,i:截取几个字符
 
2、在SQL Server中自动生成

--创建函数实现数字部分的自增
CREATE FUNCTION f_NextXH()
RETURNS nvarchar(12)
AS
BEGIN
RETURN(SELECT ‘XH‘+CONVERT(varchar(4),year(GETDATE()))+RIGHT(1000001+ISNULL(RIGHT(MAX(StudentGuid),6),0),6) FROM StudentInfo WITH(XLOCK,PAGLOCK))
END
GO

--在表中应用函数
CREATE TABLE StudentInfo(
StudentGuid nvarchar(50) PRIMARY KEY DEFAULT dbo.f_NextXH(),
StudentName nvarchar(50) NOT NULL,
Sex nvarchar(50) NOT NULL,
[Identity] nvarchar(50) NOT NULL,
Introduction nvarchar(50) NOT NULL,
)

再例如:

--创建函数实现数字部分的自增
CREATE FUNCTION f_NextCJ()
RETURNS nvarchar(15)
AS
BEGIN
RETURN(SELECT ‘CJ‘+CONVERT(varchar(100), GETDATE(), 112)+RIGHT(10001+ISNULL(RIGHT(MAX(ScoreGuid),5),0),5) FROM ScoreInfo WITH(XLOCK,PAGLOCK))
END
GO

--在表中应用函数
CREATE TABLE [ScoreInfo](
ScoreGuid nvarchar(50) PRIMARY KEY DEFAULT dbo.f_NextCJ(),
StudentGuid nvarchar(50) NOT NULL,
[Year] nvarchar(50) NOT NULL,
[Semester] nvarchar(50) NOT NULL,
[Subject] nvarchar(50) NOT NULL,
[Score] numeric(8, 2) NOT NULL,
[ScoreTime] datetime NOT NULL,)

自动生成流水线号

标签:

原文地址:http://www.cnblogs.com/zhouhb/p/4440710.html

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