private void btnCreateNo_Click(object sender, EventArgs e)
{
this.txtEmployeeNo.Text = MultipleBLL.NewNo("Employee");
}
public static string NewNo(string noType)
{
DAL.MaxIDTabDAL dal = new DAL.MaxIDTabDAL();
return dal.GetNewNo(noType);
}
public string GetNewNo(string noType)
{
using (SqlConnection conn = new SqlConnection(DBSetting.ConnText))
{
//取出Employee 编号数据
conn.Open();
SqlTransaction tran = conn.BeginTransaction();//事物 锁
string cmdText = "SELECT NoType,NoValue,NoFormatText,NoNumLength FROM MaxIDTab where NoType = @NoType ";
MaxIDTabInfo info = new MaxIDTabInfo();
SqlDataReader rdr = SqlHelper.ExecuteReader(tran, CommandType.Text, cmdText, new SqlParameter("@NoType", noType));
if (rdr.Read())
{
info.NoValue = (int)rdr["NoValue"];
info.NoFormatText = rdr["NoFormatText"].ToString();
info.NoNumLength = (int)rdr["NoNumLength"];
}
rdr.Close();
//更新最大编号//把NoValue加1
string updateCmdText = "UPDATE MaxIDTab SET NoValue = @NoValue WHERE NoType = @NoType ";
SqlHelper.ExecuteNonQuery(tran, CommandType.Text, updateCmdText
, new SqlParameter("@NoValue", (info.NoValue + 1))
, new SqlParameter("@NoType", noType));// 1
tran.Commit();//解锁
//把NoValue左侧补充0,长度为NoNumLengt
//把NoValue格式化为 NoFormatText
return string.Format(info.NoFormatText, info.NoValue.Value.ToString().PadLeft(info.NoNumLength.Value, '0'));
}
} |