调用存储过程自动生成编号这块的编程代码给写下。
存储过程代码:
CREATE proc proc_AutoID
(@newID varchar (20) output)
as
declare @id varchar(20)
set @id = (select max(ID) from tb_Employee)
if(@id is null)
set @id = 'YGBH0001'
set @newID = (select 'YGBH'+ (select stuff(convert(int,10000+(substring(@id,5,4)+1)),1,1,'')))
GO
调用代码:
sqlcon = getCon();//实例化数据库连接类对象
SqlCommand sqlcmd = new SqlCommand("proc_AutoID", sqlcon);//实例化SqlCommend对象
sqlcmd.CommandType = CommandType.StoredProcedure;//指定执行存储过程
//为存储过程添加参数
SqlParameter outValue = sqlcmd.Parameters.Add("@newID", SqlDbType.VarChar, 20);
outValue.Direction = ParameterDirection.Output;//定义存储过程输出参数
sqlcmd.ExecuteNonQuery();//执行存储过程
sqlcon.Close();//关闭数据库连接
txtID.Text = outValue.Value.ToString();//显示生成的编号