CodeSmith模板编程(1)---DAL模板
最近重装电脑,把以前用的DAL代码自动生成工具yzCsharp(yyyz.net)弄丢了,而且作者好像没有提供下载了,所以决定自己用codesmith写一个。
先看看生成的一个代码类,如下,当然最后生成的代码还要根据我自己的需要修改一下。
public class DbCart
{
// Private Variables -----------------------------------------------
private long m_cart_id;
private long m_order_id;
private long m_cart_product_id;
private int m_cart_product_quantity;
private double m_sub_save;
private double m_Sub_total;
private string m_dbConnection=null;
private string m_dbSelect=null;
private string m_dbDelete=null;
private string m_dbRecordCount=null;
// Class Constructor -----------------------------------------------

public DbCart()
{
m_dbConnection = ConfigurationSettings.AppSettings["DSN"];
if (m_dbConnection==null) throw new Exception("Error-> DSN not set in Config.web");
}

// Public Properties -----------------------------------------------
public long cart_id
{
get {return m_cart_id; }
set {m_cart_id=value; }
}
public long order_id
{
get {return m_order_id; }
set {m_order_id=value; }
}
public long cart_product_id
{
get {return m_cart_product_id; }
set {m_cart_product_id=value; }
}
public int cart_product_quantity
{
get {return m_cart_product_quantity; }
set {m_cart_product_quantity=value; }
}
public double sub_save
{
get {return m_sub_save; }
set {m_sub_save=value; }
}
public double Sub_total
{
get {return m_Sub_total; }
set {m_Sub_total=value; }
}
// After Creating Class, Pass Database Connection String-------------
public string dbConnection
{
set {m_dbConnection = value; }
}

// Pass Select Statement : Default is All Records -------------------
public string dbSelect
{
set {m_dbSelect = value; }
}

// Pass Delete Statement : Default is Current Record ----------------
public string dbDelete
{
set {m_dbDelete = value; }
}

// Pass Record Statement : Default is All Records -------------------
public string dbRecordCount
{
set {m_dbRecordCount = value; }
}

//PRIVATE: The SetParameters Function is used by the Add/Update Functions
private void SetParameters(SqlCommand m_SqlCommand, bool DoAdd)
{
if (DoAdd==false)
{ // For Updates, Declare the Id Field
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_id", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_id"].Value = m_cart_id;
}

m_SqlCommand.Parameters.Add(new SqlParameter("@m_order_id", SqlDbType.BigInt));
m_SqlCommand.Parameters["@m_order_id"].Value = m_order_id;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_product_id", SqlDbType.BigInt));
m_SqlCommand.Parameters["@m_cart_product_id"].Value = m_cart_product_id;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_product_quantity", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_product_quantity"].Value = m_cart_product_quantity;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_sub_save", SqlDbType.Float));
m_SqlCommand.Parameters["@m_sub_save"].Value = m_sub_save;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_Sub_total", SqlDbType.Float));
m_SqlCommand.Parameters["@m_Sub_total"].Value = m_Sub_total;
if (DoAdd==true)
{// For Add Function, Set Return Parameter of Id Field
SqlParameter returnParam = m_SqlCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
returnParam.Direction = ParameterDirection.Output;
}
} // End SetParameters Function

//PRIVATE: Used by the Select Function to Read the Results
private void ReadDataReader(SqlDataReader m_SqlDataReader)
{
if (!Convert.IsDBNull(m_SqlDataReader["cart_id"])) m_cart_id=Convert.ToInt64(m_SqlDataReader["cart_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["order_id"])) m_order_id=Convert.ToInt64(m_SqlDataReader["order_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["cart_product_id"])) m_cart_product_id=Convert.ToInt64(m_SqlDataReader["cart_product_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["cart_product_quantity"])) m_cart_product_quantity=Convert.ToInt32(m_SqlDataReader["cart_product_quantity"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["sub_save"])) m_sub_save=Convert.ToDouble(m_SqlDataReader["sub_save"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["Sub_total"])) m_Sub_total=Convert.ToDouble(m_SqlDataReader["Sub_total"].ToString());
} // End ReadDataReader Function

//PUBLIC: Add a NEW Record ------------------------------------------------
public void Add() // Add Table Entry
{
string insertCmd = "INSERT INTO db_cart (order_id,cart_product_id,cart_product_quantity,sub_save,Sub_total) VALUES (@m_order_id,@m_cart_product_id,@m_cart_product_quantity,@m_sub_save,@m_Sub_total); ";
insertCmd += " SELECT @thisId=SCOPE_IDENTITY() FROM db_cart";
// for pre-SQL 2000 use the @@Identity global varaible - note in
// a high transaction enviornment, the result returned by @@IDENTITY
// may not always be accurate since it is global and will return
// the id of the last record added - not nexessarily your record.
// insertCmd += " SELECT @Id=@@identity FROM aEvents ";

SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(insertCmd, m_SqlConnection);
SetParameters(m_SqlCommand,true);
try
{
m_SqlConnection.Open();
m_SqlCommand.ExecuteScalar();
m_cart_id = (int)m_SqlCommand.Parameters["@thisId"].Value;
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Add()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Add

//PUBLIC: Update an Existing Record ---------------------------------------
public void Update() // Update Table Entry
{
string updateCmd = "UPDATE db_cart SET order_id=@m_order_id, cart_product_id=@m_cart_product_id, cart_product_quantity=@m_cart_product_quantity, sub_save=@m_sub_save, Sub_total=@m_Sub_total WHERE cart_id= @m_cart_id";
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(updateCmd, m_SqlConnection);
SetParameters(m_SqlCommand,false);
try
{
m_SqlCommand.Connection.Open();
m_SqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Update()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Update

//PUBLIC: Select an Existing Record : See dbSelect ------------------------
public bool Select() // Select Entry From Table
{
bool Found = false;
string selectCmd = "SELECT * FROM db_cart WHERE cart_id="+m_cart_id;
if (m_dbSelect!=null) selectCmd = m_dbSelect;;

SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(selectCmd, m_SqlConnection);
try
{
m_SqlCommand.Connection.Open();
SqlDataReader m_SqlDataReader = m_SqlCommand.ExecuteReader();
// Get 1st Row - true if record found -------------
if (m_SqlDataReader.Read())
{
Found = true;
ReadDataReader(m_SqlDataReader);
}
m_SqlDataReader.Close();
} // end try
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Select()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return Found;
} // end Select

//PUBLIC: Delete an Existing Record : See dbDelete -------------------------
public void Delete() // Delete Table Entry
{
string deleteCmd = "DELETE from db_cart where cart_id="+m_cart_id;
if (m_dbDelete!=null) deleteCmd = m_dbDelete;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(deleteCmd, m_SqlConnection);
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_id", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_id"].Value = m_cart_id;
try
{
m_SqlCommand.Connection.Open();
m_SqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Delete()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Delete

//PUBLIC: Return DataSet : See dbSelect -------------------------
public DataSet GetDataSet(string selectCmd) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(selectCmd,m_SqlConnection);
try
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,"Table0");
} // end try
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:GetDataSet()-> " + e.ToString());
}
finally
{
m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds;
} // end GetDataSet

public DataSet GetByOrderIdDataSet(long orderId) // Select From Table
{
DataSet ds = null;
string selectCmd = "Select * from db_cart where order_id =" + orderId;
ds = GetDataSet(selectCmd);
return ds;
}

}
先看看生成的一个代码类,如下,当然最后生成的代码还要根据我自己的需要修改一下。
public class DbCart
{
// Private Variables -----------------------------------------------
private long m_cart_id;
private long m_order_id;
private long m_cart_product_id;
private int m_cart_product_quantity;
private double m_sub_save;
private double m_Sub_total;
private string m_dbConnection=null;
private string m_dbSelect=null;
private string m_dbDelete=null;
private string m_dbRecordCount=null;
// Class Constructor -----------------------------------------------
public DbCart()
{
m_dbConnection = ConfigurationSettings.AppSettings["DSN"];
if (m_dbConnection==null) throw new Exception("Error-> DSN not set in Config.web");
}
// Public Properties -----------------------------------------------
public long cart_id
{
get {return m_cart_id; }
set {m_cart_id=value; }
}
public long order_id
{
get {return m_order_id; }
set {m_order_id=value; }
}
public long cart_product_id
{
get {return m_cart_product_id; }
set {m_cart_product_id=value; }
}
public int cart_product_quantity
{
get {return m_cart_product_quantity; }
set {m_cart_product_quantity=value; }
}
public double sub_save
{
get {return m_sub_save; }
set {m_sub_save=value; }
}
public double Sub_total
{
get {return m_Sub_total; }
set {m_Sub_total=value; }
}
// After Creating Class, Pass Database Connection String-------------
public string dbConnection
{
set {m_dbConnection = value; }
} 
// Pass Select Statement : Default is All Records -------------------
public string dbSelect
{
set {m_dbSelect = value; }
} 
// Pass Delete Statement : Default is Current Record ----------------
public string dbDelete
{
set {m_dbDelete = value; }
} 
// Pass Record Statement : Default is All Records -------------------
public string dbRecordCount
{
set {m_dbRecordCount = value; }
} 
//PRIVATE: The SetParameters Function is used by the Add/Update Functions
private void SetParameters(SqlCommand m_SqlCommand, bool DoAdd)
{
if (DoAdd==false)
{ // For Updates, Declare the Id Field
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_id", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_id"].Value = m_cart_id;
}
m_SqlCommand.Parameters.Add(new SqlParameter("@m_order_id", SqlDbType.BigInt));
m_SqlCommand.Parameters["@m_order_id"].Value = m_order_id;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_product_id", SqlDbType.BigInt));
m_SqlCommand.Parameters["@m_cart_product_id"].Value = m_cart_product_id;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_product_quantity", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_product_quantity"].Value = m_cart_product_quantity;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_sub_save", SqlDbType.Float));
m_SqlCommand.Parameters["@m_sub_save"].Value = m_sub_save;
m_SqlCommand.Parameters.Add(new SqlParameter("@m_Sub_total", SqlDbType.Float));
m_SqlCommand.Parameters["@m_Sub_total"].Value = m_Sub_total;
if (DoAdd==true)
{// For Add Function, Set Return Parameter of Id Field
SqlParameter returnParam = m_SqlCommand.Parameters.Add(new SqlParameter("@thisId",SqlDbType.Int));
returnParam.Direction = ParameterDirection.Output;
}
} // End SetParameters Function 
//PRIVATE: Used by the Select Function to Read the Results
private void ReadDataReader(SqlDataReader m_SqlDataReader)
{
if (!Convert.IsDBNull(m_SqlDataReader["cart_id"])) m_cart_id=Convert.ToInt64(m_SqlDataReader["cart_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["order_id"])) m_order_id=Convert.ToInt64(m_SqlDataReader["order_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["cart_product_id"])) m_cart_product_id=Convert.ToInt64(m_SqlDataReader["cart_product_id"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["cart_product_quantity"])) m_cart_product_quantity=Convert.ToInt32(m_SqlDataReader["cart_product_quantity"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["sub_save"])) m_sub_save=Convert.ToDouble(m_SqlDataReader["sub_save"].ToString());
if (!Convert.IsDBNull(m_SqlDataReader["Sub_total"])) m_Sub_total=Convert.ToDouble(m_SqlDataReader["Sub_total"].ToString());
} // End ReadDataReader Function 
//PUBLIC: Add a NEW Record ------------------------------------------------
public void Add() // Add Table Entry
{
string insertCmd = "INSERT INTO db_cart (order_id,cart_product_id,cart_product_quantity,sub_save,Sub_total) VALUES (@m_order_id,@m_cart_product_id,@m_cart_product_quantity,@m_sub_save,@m_Sub_total); ";
insertCmd += " SELECT @thisId=SCOPE_IDENTITY() FROM db_cart";
// for pre-SQL 2000 use the @@Identity global varaible - note in
// a high transaction enviornment, the result returned by @@IDENTITY
// may not always be accurate since it is global and will return
// the id of the last record added - not nexessarily your record.
// insertCmd += " SELECT @Id=@@identity FROM aEvents ";
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(insertCmd, m_SqlConnection);
SetParameters(m_SqlCommand,true);
try
{
m_SqlConnection.Open();
m_SqlCommand.ExecuteScalar();
m_cart_id = (int)m_SqlCommand.Parameters["@thisId"].Value;
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Add()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Add
//PUBLIC: Update an Existing Record ---------------------------------------
public void Update() // Update Table Entry
{
string updateCmd = "UPDATE db_cart SET order_id=@m_order_id, cart_product_id=@m_cart_product_id, cart_product_quantity=@m_cart_product_quantity, sub_save=@m_sub_save, Sub_total=@m_Sub_total WHERE cart_id= @m_cart_id";
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(updateCmd, m_SqlConnection);
SetParameters(m_SqlCommand,false);
try
{
m_SqlCommand.Connection.Open();
m_SqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Update()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Update
//PUBLIC: Select an Existing Record : See dbSelect ------------------------
public bool Select() // Select Entry From Table
{
bool Found = false;
string selectCmd = "SELECT * FROM db_cart WHERE cart_id="+m_cart_id;
if (m_dbSelect!=null) selectCmd = m_dbSelect;;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(selectCmd, m_SqlConnection);
try
{
m_SqlCommand.Connection.Open();
SqlDataReader m_SqlDataReader = m_SqlCommand.ExecuteReader();
// Get 1st Row - true if record found -------------
if (m_SqlDataReader.Read())
{
Found = true;
ReadDataReader(m_SqlDataReader);
}
m_SqlDataReader.Close();
} // end try
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Select()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return Found;
} // end Select
//PUBLIC: Delete an Existing Record : See dbDelete -------------------------
public void Delete() // Delete Table Entry
{
string deleteCmd = "DELETE from db_cart where cart_id="+m_cart_id;
if (m_dbDelete!=null) deleteCmd = m_dbDelete;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlCommand m_SqlCommand = new SqlCommand(deleteCmd, m_SqlConnection);
m_SqlCommand.Parameters.Add(new SqlParameter("@m_cart_id", SqlDbType.Int));
m_SqlCommand.Parameters["@m_cart_id"].Value = m_cart_id;
try
{
m_SqlCommand.Connection.Open();
m_SqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:Delete()-> " + e.ToString());
}
finally
{
m_SqlCommand.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
} // end Delete
//PUBLIC: Return DataSet : See dbSelect -------------------------
public DataSet GetDataSet(string selectCmd) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(selectCmd,m_SqlConnection);
try
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,"Table0");
} // end try
catch (Exception e)
{
throw new Exception("Error in Cdb_cart:GetDataSet()-> " + e.ToString());
}
finally
{
m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds;
} // end GetDataSet
public DataSet GetByOrderIdDataSet(long orderId) // Select From Table
{
DataSet ds = null;
string selectCmd = "Select * from db_cart where order_id =" + orderId;
ds = GetDataSet(selectCmd);
return ds;
} 
}

浙公网安备 33010602011771号