黑马程序员技术交流社区

标题: 三层架构自动生成E{……}类型的编号 [打印本页]

作者: 霍世勇    时间: 2012-4-4 19:00
标题: 三层架构自动生成E{……}类型的编号
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'));
        
            }


        }
作者: 何智杰    时间: 2012-4-4 22:52
不错。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2