用C#实现?C#还不是操纵数据库实现啊,哈哈 给你一个自动编号的存储过程,在C#里去调用set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go Create PROCEDURE [dbo].[AutoNo] @strTableName varchar(50),--要编号的表名 @strColumnName varchar(50),--要编号的列名 @strPreString varchar(50),--编号前缀字符 @iStartPos int,--编号起始位 @iLength int,--长度 @strWhere varchar(800),--外部强制条件,可为空字符"" @bDiscontinuity bit--是否补缺号 AS declare @ReturnNo int if len(@strWhere) > 0 set @strWhere = @strWhere + ' and ' if @bDiscontinuity = 0 begin exec('select identity(int,1,1) xh,cast(substring('+@strColumnName+','+ @iStartPos+','+@iLength+') as int) as CurCode into ##temp from (select '+@strColumnName+' from '+ @strTableName+' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls') set @ReturnNo = (select isnull(min(##temp.xh),0) as ReturnNo from ##temp where ##temp.xh<>##temp.CurCode) drop table ##temp if @ReturnNo = 0 --说明没有检测到断号,此时要返回的是数据表中最大编号 begin exec('select cast(substring('+@strColumnName+','+@iStartPos+','+@iLength+ ') as int) as MaxNo into ##temp1 from (select '+@strColumnName+' from '+@strTableName+ ' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls') select isnull(max(##temp1.MaxNo),0)+1 as ReturnNo from ##temp1 drop table ##temp1 end else select @ReturnNo as ReturnNo
end else begin exec('select cast(substring('+@strColumnName+','+@iStartPos+','+@iLength+ ') as int) as MaxNo into ##temp from (select '+@strColumnName+' from '+@strTableName+ ' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls') exec('select isnull(max(##temp.MaxNo),0)+1 as ReturnNo from ##temp') drop table ##temp end
给你一个自动编号的存储过程,在C#里去调用set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
Create PROCEDURE [dbo].[AutoNo]
@strTableName varchar(50),--要编号的表名
@strColumnName varchar(50),--要编号的列名
@strPreString varchar(50),--编号前缀字符
@iStartPos int,--编号起始位
@iLength int,--长度
@strWhere varchar(800),--外部强制条件,可为空字符""
@bDiscontinuity bit--是否补缺号
AS
declare @ReturnNo int
if len(@strWhere) > 0
set @strWhere = @strWhere + ' and '
if @bDiscontinuity = 0
begin
exec('select identity(int,1,1) xh,cast(substring('+@strColumnName+','+
@iStartPos+','+@iLength+') as int) as CurCode into ##temp from (select '+@strColumnName+' from '+
@strTableName+' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls')
set @ReturnNo = (select isnull(min(##temp.xh),0) as ReturnNo from ##temp where ##temp.xh<>##temp.CurCode)
drop table ##temp
if @ReturnNo = 0 --说明没有检测到断号,此时要返回的是数据表中最大编号
begin
exec('select cast(substring('+@strColumnName+','+@iStartPos+','+@iLength+
') as int) as MaxNo into ##temp1 from (select '+@strColumnName+' from '+@strTableName+
' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls')
select isnull(max(##temp1.MaxNo),0)+1 as ReturnNo from ##temp1
drop table ##temp1
end
else
select @ReturnNo as ReturnNo
end
else
begin
exec('select cast(substring('+@strColumnName+','+@iStartPos+','+@iLength+
') as int) as MaxNo into ##temp from (select '+@strColumnName+' from '+@strTableName+
' where '+@strwhere+@strColumnName+' like '''+@strPreString+'%'') as ls')
exec('select isnull(max(##temp.MaxNo),0)+1 as ReturnNo from ##temp')
drop table ##temp
end
先取出最后一条记录,如果此记录为空,则编号为00000001,否则的话取出值转换一下成整数,加1再组成字符串。就可以了吧。你试试看
string sTempId = "";
SqlConnection objConn = new SqlConnection(SqlHelper.GetConnStr());
objConn.Open();
SqlCommand objCmd = new SqlCommand("select id='00000001' ", objConn);
//当没有记录时
//SqlCommand objCmd = new SqlCommand("select id='00000001' where 1!=1 ", objConn);
SqlDataReader dr = objCmd.ExecuteReader();
if (dr.Read())
{ sTempId = "00000000" + Convert.ToString(Convert.ToInt64(dr["id"]) + 1);
sNextId = sTempId.Substring(sTempId.Length - 8, 8);
}
else
{
sNextId = "00000001";
}
objConn.Close();
objConn = null; Response.Write(sNextId);
U001U001001...U002001...结合Parent定义,可通过编号灵活生成树