我的代码生成器生成的样本代码(C#)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace JoeyCodeGenerator
{
public class UserDataBase
{
#region "Properties"
public const string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
#endregion
#region "Methods"
#region "Insert"
public static void Insert(User instance)
{
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "User_Insert";
comm.Parameters.AddWithValue("@UserName", instance.UserName);
SqlParameter sp;
sp = comm.Parameters.Add("@UserID", SqlDbType.Int);
sp.Direction = ParameterDirection.Output;
sp = new SqlParameter();
sp.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(sp);
conn.Open();
comm.ExecuteNonQuery();
if ((int)sp.Value != 0)
{
throw new Exception("Errors happened when executing the stored procedure");
}
instance.UserID = (Int32)comm.Parameters["@UserID"].Value;
conn.Close();
}
}
}
#endregion
#region "Select"
public static void Select(User instance)
{
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "User_Select";
comm.Parameters.AddWithValue("@UserID", instance.UserID);
SqlParameter sp;
sp = new SqlParameter();
sp.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(sp);
conn.Open();
using (SqlDataReader sdr = comm.ExecuteReader(CommandBehavior.CloseConnection))
{
if (sdr.Read())
{
instance.UserName = (String)sdr["UserName"];
}
sdr.Close();
}
if ((int)sp.Value != 0)
{
throw new Exception("Errors happened when executing the stored procedure");
}
}
}
}
#endregion
#region "Update"
public static void Update(User instance)
{
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "User_Update";
comm.Parameters.AddWithValue("@UserID", instance.UserID);
comm.Parameters.AddWithValue("@UserName", instance.UserName);
SqlParameter sp;
sp = new SqlParameter();
sp.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(sp);
conn.Open();
comm.ExecuteNonQuery();
if ((int)sp.Value != 0)
{
throw new Exception("Errors happened when executing the stored procedure");
}
conn.Close();
}
}
}
#endregion
#region "Delete"
public static void Delete(User instance)
{
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "User_Delete";
comm.Parameters.AddWithValue("@UserID", instance.UserID);
SqlParameter sp;
sp = new SqlParameter();
sp.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(sp);
conn.Open();
comm.ExecuteNonQuery();
if ((int)sp.Value != 0)
{
throw new Exception("Errors happened when executing the stored procedure");
}
conn.Close();
}
}
}
#endregion
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
namespace JoeyCodeGenerator
{
public class User
{
#region "Properties"
private Int32 _UserID;
private String _UserName;
public Int32 UserID
{
get
{
return this._UserID;
}
set
{
this._UserID = value;
}
}
public String UserName
{
get
{
return this._UserName;
}
set
{
this._UserName = value;
}
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
namespace JoeyCodeGenerator
{
public class UserBroker
{
#region "Methods"
#region "Insert"
public static void Insert(User instance)
{
//add business logic here
UserDataBase.Insert(instance);
}
#endregion
#region "Select"
public static void Select(User instance)
{
//add business logic here
UserDataBase.Select(instance);
}
#endregion
#region "Update"
public static void Update(User instance)
{
//add business logic here
UserDataBase.Update(instance);
}
#endregion
#region "Delete"
public static void Delete(User instance)
{
//add business logic here
UserDataBase.Delete(instance);
}
#endregion
#endregion
}
}