当前位置:编程学习 > C#/ASP.NET >>

dotnet代码自动生成机的实现

答案:     在我们写程序的时候,特别是数据库应用程序的时候,经常会遇到这样的情况:对于一个给定的表,写出这个表对应的类(用一句时髦的话说是实现业务实体类),类的数据成员是所有的字段,并且类含有该表的添加修改删除等操作。还有,对于一个给定的存储过程,要完成根据存储过程存取数据或别的数据库操作。如下代码就是我们通常要完成的:
  1.表的业务实体化
  private int iId ;
  public int Id
  {
  get
  {
  return iId ;
  }
  set
  {
  iId = value ;
  }
  }
  
  
  private string strName ;
  public string Name
  {
  get
  {
  return strName ;
  }
  set
  {
  strName = value ;
  }
  }
  
  
  private string strCode ;
  public string Code
  {
  get
  {
  return strCode ;
  }
  set
  {
  strCode = value ;
  }
  }
  
  
  private string strDescription ;
  public string Description
  {
  get
  {
  return strDescription ;
  }
  set
  {
  strDescription = value ;
  }
  }
  
  
  private int iFatherid ;
  public int Fatherid
  {
  get
  {
  return iFatherid ;
  }
  set
  {
  iFatherid = value ;
  }
  }
  
  
  private int iType ;
  public int Type
  {
  get
  {
  return iType ;
  }
  set
  {
  iType = value ;
  }
  }
  
  
  private int iUserId ;
  public int UserId
  {
  get
  {
  return iUserId ;
  }
  set
  {
  iUserId = value ;
  }
  }
  
  
  ublic bool Add()
  {
  SqlConnection conn = SqlConn.Instance().Connection ;
  
  string strSql = "insert into book(id, Name, Code, Description, Fatherid, Type, UserId)"
  +"values(@id, @Name, @Code, @Description, @Fatherid, @Type, @UserId)" ;
  
  SqlCommand command = new SqlCommand(strSql,conn) ;
  
  command.Parameters.Add("@id",SqlDbType.Int ) ;
  command.Parameters["@id"].value = iId ;
  
  command.Parameters.Add("@Name",SqlDbType.NVarChar ,50) ;
  if (strName!= null )
  command.Parameters["@Name"].value = strName ;
  else
  command.Parameters["@Name"].value = DBNull.value ;
  
  command.Parameters.Add("@Code",SqlDbType.NVarChar ,255) ;
  if (strCode!= null )
  command.Parameters["@Code"].value = strCode ;
  else
  command.Parameters["@Code"].value = DBNull.value ;
  
  command.Parameters.Add("@Description",SqlDbType.NVarChar ,255) ;
  if (strDescription!= null )
  command.Parameters["@Description"].value = strDescription ;
  else
  command.Parameters["@Description"].value = DBNull.value ;
  
  command.Parameters.Add("@Fatherid",SqlDbType.Int ) ;
  command.Parameters["@Fatherid"].value = iFatherid ;
  
  command.Parameters.Add("@Type",SqlDbType.Int ) ;
  command.Parameters["@Type"].value = iType ;
  
  command.Parameters.Add("@UserId",SqlDbType.Int ) ;
  command.Parameters["@UserId"].value = iUserId ;
  
  try
  {
  conn.Open() ;
  command.ExecuteNonQuery() ;
  return true ;
  }
  catch(Exception e)
  {
  throw(new Exception("Error in the Database"+e.Message)) ;
  }
  finally
  {
  conn.Close() ;
  }
  }
  public bool Modify()
  {
  SqlConnection conn = SqlConn.Instance().Connection ;
  string strSql ="update book set id = @id, Name = @Name, Code = @Code, Description = @Description, Fatherid = @Fatherid, Type = @Type, UserId = @UserId "
  + " where id =@id " ;
  SqlCommand command = new SqlCommand(strSql,conn) ;
  command.Parameters.Add("@id",SqlDbType.Int ) ;
  command.Parameters["@id"].value = iId ;
  
  command.Parameters.Add("@Name",SqlDbType.NVarChar ,50) ;
  command.Parameters["@Name"].value = strName ;
  
  command.Parameters.Add("@Code",SqlDbType.NVarChar ,255) ;
  command.Parameters["@Code"].value = strCode ;
  
  command.Parameters.Add("@Description",SqlDbType.NVarChar ,255) ;
  command.Parameters["@Description"].value = strDescription ;
  
  command.Parameters.Add("@Fatherid",SqlDbType.Int ) ;
  command.Parameters["@Fatherid"].value = iFatherid ;
  
  command.Parameters.Add("@Type",SqlDbType.Int ) ;
  command.Parameters["@Type"].value = iType ;
  
  command.Parameters.Add("@UserId",SqlDbType.Int ) ;
  command.Parameters["@UserId"].value = iUserId ;
  
  try
  {
  conn.Open() ;
  command.ExecuteNonQuery() ;
  return true ;
  }
  catch(Exception e)
  {
  throw(new Exception("Error in the Database"+e.Message)) ;
  }
  finally
  {
  conn.Close() ;
  }
  }
  
  再看一下存储过程:
  public bool ExeSP_ddms_Modify_Trx(
  int aiPrsn_trx_no,
  int aiUlt_incid_no,
  int aiPrsn_trx_status_cd,
  DateTime adtTrx_cmpl_dt,
  string astrEmail_addr)
  {
  SqlConnection conn = SqlConn.Instance().Connection ;
  
  string strSPName = "ddms_Modify_Trx" ;
  SqlCommand command = new SqlCommand(strSPName,conn) ;
  command.CommandType = CommandType.StoredProcedure ;
  
  command.Parameters.Add("@prsn_trx_no",SqlDbType.SmallInt ) ;
  command.Parameters["@prsn_trx_no"].value= aiPrsn_trx_no ;
  
  command.Parameters.Add("@ult_incid_no",SqlDbType.Int ) ;
  command.Parameters["@ult_incid_no"].value= aiUlt_incid_no ;
  
  command.Parameters.Add("@prsn_trx_status_cd",SqlDbType.Int ) ;
  command.Parameters["@prsn_trx_status_cd"].value= aiPrsn_trx_status_cd ;
  
  command.Parameters.Add("@trx_cmpl_dt",SqlDbType.DateTime ) ;
  if ( adtTrx_c

上一个:nhibernate架构分析(uml图)
下一个:.NET 框架中的 XML:在 .NET 框架中使用 XML 架构执行代码生成(3)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,