using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinForm_CreateGenerate_ForOracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
         /// <summary>
/// 窗口加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
txtNamespaceProfix.Text = "WinForm_UserInfo";
txtdbconnStr.Text = @"Data Source=(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
);User Id=scott;Password=XXXXXX";
}

窗口加载

         /// <summary>
///点击 链接 链接Oracle数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
string connStr = txtdbconnStr.Text.Trim();
string sql = "select * from user_tables"; //查询Oracle数据库中当前用户的所有表的信息
try
{
using (OracleConnection conn = new OracleConnection(connStr))
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
conn.Open();
using (OracleDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string tableName = reader[].ToString();
cmbTables.Items.Add(tableName);
}
}
}
}
catch (Exception ex)
{
throw new Exception("出错:" + ex.Message.ToString());
}
}

点击 链接 链接Oracle数据库

         /// <summary>
/// 如果当前列可Null,并且数据类型不是String,需要拼接"?"
/// </summary>
/// <param name="isNull"></param>
/// <param name="cType"></param>
/// <returns></returns>
private string CheckJoinWenHao(string isNull,string cType)
{
if(isNull=="Y" && cType!="String")
{
return cType + "?";
}
else
{
return cType;
}
}

如果当前列可Null,并且数据类型不是String,需要拼接"?"

         /// <summary>
/// 获得列名连接字符串、参数连接字符串、Model连接字符串、ToModel连接字符串
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="newClassName">新类名</param>
/// <param name="columnNameWithConsListStr">列名连接字符串</param>
/// <param name="paraListStr">参数连接字符串</param>
private void GetColumnListStrAndParaListStr(string tableName, string newClassName, out string columnNameWithConsListStr, out string columnNameWithEqualListStr, out string paraListStr, out string rowtomodelStr, out string modelStr)
{
//查询当前表所有列名
string sql = "Select column_name,data_type,Nullable From user_tab_columns where table_name = :table_name"; //在当前用户的所有列中查询指定表的列信息
List<string> columnNameList = new List<string>();
List<string> columnNameWithConsList = new List<string>(); //带':'的列名集合
List<string> columnNameWithEqualList = new List<string>(); //带'='的列名集合
List<string> paraList = new List<string>(); //参数集合
rowtomodelStr = "";
StringBuilder sbModel = new StringBuilder(); try
{
using (OracleConnection conn = new OracleConnection(txtdbconnStr.Text.Trim()))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.Parameters.Add(new OracleParameter(":table_name", tableName));
using (OracleDataReader reader = cmd.ExecuteReader())
{
long count = reader.RowSize; //行数 while (reader.Read())
{
string columnName = reader[].ToString();
string typeName = reader[].ToString();
string isDbNull = reader[].ToString();
string lowClassName = newClassName.ToLower(); //小写类名,表示实例
#region 拼接Model中的列字符串
//public string Address { get; set; }
//拼接Model中的列字符串
sbModel.Append("public ").Append(CheckJoinWenHao(isDbNull, DbTypeToCtype(typeName))).Append(" ").Append(columnName).AppendLine(" { get; set; }\n");
#endregion
#region 拼接RowToModel中的行转换
#region 冗余
//user.Id = Convert.ToInt32(row["Id"]);
//user.UserName = (string)row["UserName"]; //1 根据列的类型返回一个C#类型,2 判断是否为DbNull,3 判断类型是否为string
//user.Email = row["Email"] == DBNull.Value ? null : Convert.ToString(row["Email"]);
//拼接RowToModel
#endregion
//拼接RowToModel中的行转换
StringBuilder sb = new StringBuilder();
if (reader[].ToString() == "Y")
{
sb.Append(lowClassName).Append(".").Append(columnName).Append(" = row[\"").Append(columnName).Append("\"] == DBNull.Value ? null : Convert.To").Append(DbTypeToCtype(typeName)).Append("(row[\"").Append(columnName).AppendLine("\"]);");
}
else
{
sb.Append(newClassName.ToLower()).Append(".").Append(reader[].ToString()).Append(" = Convert.To").Append(DbTypeToCtype(reader[].ToString())).Append("(row[\"").Append(reader[].ToString()).AppendLine("\"]);");
}
rowtomodelStr += sb.ToString(); //累积RowToModel字符串
#endregion
#region 拼接列名和参数字符串
//拼接列名和参数字符串
if (reader[].ToString() != "ID") //去除ID项
{
columnNameList.Add(reader[].ToString());
columnNameWithConsList.Add(":" + reader[].ToString());
columnNameWithEqualList.Add(columnName + "=:" + columnName); //USERNAME=:USERNAME
//new OracleParameter(":Email", user.Email==null?(object)DBNull.Value:user.Email),
if (reader[].ToString() == "Y")
{
paraList.Add("new OracleParameter(\":" + columnName + "\", " + lowClassName + "." + columnName + "==null?(object)DBNull.Value:" + lowClassName + "." + columnName + ")");
}
else
{
paraList.Add("new OracleParameter(\":" + columnName + "\", " + lowClassName + "." + columnName + ")");
}
}
#endregion
}
}
}
}
}
catch (Exception ex)
{
throw new Exception("出错:" + ex.Message.ToString());
}
columnNameWithConsListStr = string.Join(",", columnNameWithConsList);
columnNameWithEqualListStr = string.Join(",", columnNameWithEqualList);
paraListStr = string.Join(",", paraList);
modelStr = sbModel.ToString(); //model中的列字符串
}

获得列名连接字符串、参数连接字符串、Model连接字符串、ToModel连接字符串

         /// <summary>
/// 把Oracle中的数据类型转C#中的数据类型
/// </summary>
/// <param name="dbType">Oracle中的数据类型 字符串</param>
/// <returns>C#中的数据类型 字符串</returns>
private string DbTypeToCtype(string dbType)
{
string ctype = "";
switch (dbType)
{
case "NUMBER": ctype = "Int32"; //未考虑小数点
break;
case "VARCHAR2": ctype = "String";
break;
case "DATE": ctype = "DateTime";
break;
default: throw new Exception("未知类型,需要添加新的类型");
}
return ctype;
}

把Oracle中的数据类型转C#中的数据类型

         /// <summary>
/// 获得表名、新类名、命名空间前缀
/// </summary>
/// <param name="tabelName">表名</param>
/// <param name="newClassName">新类名</param>
/// <param name="nameSpaceProfix">命名空间前缀</param>
private void GetTableNameAndClassNameAndSpcaeName(out string tabelName, out string newClassName, out string nameSpaceProfix)
{
//表名
tabelName = cmbTables.SelectedItem.ToString();
//新类名
string typeProfix = txtTypeProfix.Text.Trim();
if (typeProfix.Length <= )
{
newClassName = tabelName;
}
else
{
newClassName = tabelName.Replace(typeProfix, ""); //去掉前缀 或后缀
}
nameSpaceProfix = txtNamespaceProfix.Text.Trim(); //当前类型所在命名空间
if (nameSpaceProfix.Length <= )
{
MessageBox.Show("请填命名空间");
return;
}
}

获得表名、新类名、命名空间前缀

         /// <summary>
/// 点击创建DAL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiDll_Click(object sender, EventArgs e)
{
#region 冗余
////表名
//string tabelName=cmbTables.SelectedItem.ToString();
////新类名
//string typeProfix=txtTypeProfix.Text.Trim();
//string newClassName;
//if (typeProfix.Length <= 0)
//{
// newClassName = tabelName;
//}
//else
//{
// newClassName = tabelName.Replace(typeProfix, ""); //去掉前缀 或后缀
//}
//string nameSpaceProfix = txtNamespaceProfix.Text.Trim(); //当前类型所在命名空间
//if (nameSpaceProfix.Length <= 0)
//{
// MessageBox.Show("请填命名空间");
// return;
//}
#endregion
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName,out newClassName,out nameSpaceProfix);
string lowClassName = newClassName.ToLower();
string columnNameWithConsListStr, paraListStr, rowtomodelStr, columnNameWithEqualListStr, modelStr;
GetColumnListStrAndParaListStr(tabelName, newClassName, out columnNameWithConsListStr, out columnNameWithEqualListStr, out paraListStr, out rowtomodelStr, out modelStr);
//拼接DAL
StringBuilder sb = new StringBuilder();
#region 冗余
//using Oracle.DataAccess.Client;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using WinForm_UserInfo.Model;
#endregion
sb.AppendLine("using Oracle.DataAccess.Client;");
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Data;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".Model;");
#region 冗余
//
//namespace WinForm_UserInfo.DAL
//{
// public class UserInfoDAL
// {
// /// <summary>
// /// 根据用户名获取用户实例
// /// </summary>
// /// <param name="userName">用户名</param>
// /// <returns>一个用户实例</returns>
#endregion
sb.AppendLine("");
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".DAL");
sb.AppendLine("{");
sb.Append(" public class ").Append(newClassName).AppendLine("DAL");
sb.AppendLine(" {"); #region 根据对象ID获取实例
sb.AppendLine(" /// <summary>");
sb.AppendLine(" /// 根据对象ID获取实例");
sb.AppendLine(" /// </summary>");
sb.AppendLine(" /// <param name=\"id\">ID</param>");
sb.AppendLine(" /// <returns>一个实例</returns>");
#region 冗余
//public UserInfo GetUserInfoById(int id)
//{
// string sql = "SELECT * FROM T_USERINFO WHERE ID=:ID";
// DataTable dt = OracleHelper.ExecuteReader(sql, new OracleParameter(":ID", id));
// if (dt.Rows.Count <= 0)
// {
// return null;
// }
// else if (dt.Rows.Count == 1)
// {
// return RowToModel(dt.Rows[0]);
// }
// else
// {
// throw new Exception("数据重复,重复数据为:" + id);
// }
//}
#endregion
sb.Append("\tpublic ").Append(newClassName).Append(" Get").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"SELECT * FROM ").Append(tabelName).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t DataTable dt = OracleHelper.ExecuteReader(sql, new OracleParameter(\":ID\", id));");
sb.AppendLine("\t if (dt.Rows.Count <= 0)");
sb.AppendLine("\t {");
sb.AppendLine("\t return null;");
sb.AppendLine("\t }");
sb.AppendLine("\t else if (dt.Rows.Count == 1)");
sb.AppendLine("\t {");
sb.AppendLine("\t return RowToModel(dt.Rows[0]);");
sb.AppendLine("\t }");
sb.AppendLine("\t else");
sb.AppendLine("\t {");
sb.AppendLine("\t throw new Exception(\"数据重复,重复数据为:\" + id);");
sb.AppendLine("\t }");
sb.AppendLine("\t}");
#endregion #region 获得所有实例
#region 冗余
//
///// <summary>
///// 获得所有用户
///// </summary>
///// <returns>返回所有用户</returns>
//public List<UserInfo> GetAllUserInfoes()
//{
// List<UserInfo> users=new List<UserInfo>();
// string sql = "SELECT * FROM T_USERINFO";
// DataTable dt = OracleHelper.ExecuteReader(sql);
// if (dt.Rows.Count <= 0)
// {
// return null;
// }
// else
// {
// foreach(DataRow row in dt.Rows)
// {
// users.Add(RowToModel(row));
// }
// return users;
// }
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 获得所有实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <returns>返回所有实例</returns>");
sb.Append("\tpublic List<").Append(newClassName).Append("> GetAll").Append(newClassName).AppendLine("es()");
sb.AppendLine("\t{");
sb.Append("\t List<").Append(newClassName).Append("> ").Append(newClassName.ToLower()).Append("s=new List<").Append(newClassName).AppendLine(">();");
sb.Append("\t string sql = \"SELECT * FROM ").Append(tabelName).AppendLine("\";");
sb.AppendLine("\t DataTable dt = OracleHelper.ExecuteReader(sql);");
sb.AppendLine("\t if (dt.Rows.Count <= 0)");
sb.AppendLine("\t {");
sb.AppendLine("\t return null;");
sb.AppendLine("\t }");
sb.AppendLine("\t else");
sb.AppendLine("\t {");
sb.AppendLine("\t foreach(DataRow row in dt.Rows)");
sb.AppendLine("\t {");
sb.Append("\t ").Append(newClassName.ToLower()).AppendLine("s.Add(RowToModel(row));");
sb.AppendLine("\t }");
sb.Append("\t return ").Append(newClassName.ToLower()).AppendLine("s;");
sb.AppendLine("\t }");
sb.AppendLine("\t}");
#endregion #region 根据实例ID删除实例
#region 冗余
//
///// <summary>
///// 根据用户ID删除用户
///// </summary>
///// <param name="id">用户ID</param>
///// <returns>受影响行数</returns>
//public int DeleteUserInfoById(int id)
//{
// string sql = "DELETE FROM T_USERINFO WHERE ID=:ID";
// return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(":ID", id));
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 根据实例ID删除实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Delete").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"DELETE FROM ").Append(tabelName).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(\":ID\", id));");
sb.AppendLine("\t}");
#endregion #region 新增实例
#region 冗余
//
///// <summary>
///// 新增用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>受影响行数</returns>
//public int InsertUserInfo(UserInfo user)
//{
// //SE_T_USERINFO.NEXTVAL
// string sql = "INSERT INTO T_USERINFO VALUES(SE_T_USERINFO.NEXTVAL,:USERNAME,:PWD,:MOBILE,:EMAIL,:ADDRESS,:GENDER)";
// return OracleHelper.ExecuteNonQuery(sql,
// new OracleParameter(":USERNAME", user.UserName),
// new OracleParameter(":PWD", user.Pwd),
// new OracleParameter(":MOBILE", user.Mobile),
// new OracleParameter(":EMAIL", user.Email),
// new OracleParameter(":ADDRESS", user.Address),
// new OracleParameter(":GENDER", user.Gender)
// );
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 新增实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(newClassName.ToLower()).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Insert").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(newClassName.ToLower()).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"INSERT INTO ").Append(tabelName).Append(" VALUES(SE_T_USERINFO.NEXTVAL,").Append(columnNameWithConsListStr).AppendLine(")\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql,");
sb.Append("\t ").AppendLine(paraListStr);
sb.AppendLine("\t );");
sb.AppendLine("\t}");
#endregion #region 更新实例
#region 冗余
///// <summary>
///// 更新用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>受影响行数</returns>
//public int UpdateUserInfo(UserInfo user)
//{
// string sql = "UPDATE T_USERINFO SET USERNAME=:USERNAME,PWD=:PWD,MOBILE=:MOBILE,EMAIL=:EMAIL,ADDRESS=:ADDRESS,GENDER=:GENDER WHERE ID=:ID";
// return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(":USERNAME", user.UserName),
// new OracleParameter(":PWD", user.Pwd),
// new OracleParameter(":MOBILE", user.Mobile),
// new OracleParameter(":EMAIL", user.Email),
// new OracleParameter(":ADDRESS", user.Address),
// new OracleParameter(":GENDER", user.Gender),
// new OracleParameter(":ID", user.Id)
// );
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 更新实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Update").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"UPDATE ").Append(tabelName).Append(" SET ").Append(columnNameWithEqualListStr).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql,");
sb.Append("\t ").AppendLine(paraListStr);
sb.Append("\t\t,new OracleParameter(\":ID\",").Append(lowClassName).AppendLine(".ID)");
sb.AppendLine("\t );");
sb.AppendLine("\t}");
#endregion #region Row转Model
#region 冗余
///// <summary>
///// DataRow转Model
///// </summary>
///// <param name="row">表中一行数据</param>
///// <returns>一个对象实例</returns>
//private UserInfo RowToModel(DataRow row)
//{
// //Id UserName Pwd Mobile Email Address Gender
// UserInfo user = new UserInfo();
// user.Id = Convert.ToInt32(row["Id"]);
// user.UserName = (string)row["UserName"];
// user.Pwd = (string)row["Pwd"];
// user.Mobile = (string)row["Mobile"];
// user.Email = row["Email"] == DBNull.Value ? null : (string)row["Email"];
// user.Address = row["Address"] == DBNull.Value ? null : (string)row["Email"];
// user.Gender = Convert.ToInt32(row["Gender"]);
// return user;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// DataRow转Model");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"row\">表中一行数据</param>");
sb.AppendLine("\t/// <returns>一个对象实例</returns>");
sb.Append("\tprivate ").Append(newClassName).AppendLine(" RowToModel(DataRow row)");
sb.AppendLine("\t{");
sb.Append("\t ").Append(newClassName).Append(" ").Append(lowClassName).Append(" = new ").Append(newClassName).AppendLine("();");
sb.Append("\t ").AppendLine(rowtomodelStr);
sb.Append("\t return ").Append(lowClassName).AppendLine(";");
sb.AppendLine("\t}");
#endregion sb.AppendLine("\t}");
sb.AppendLine("}");
txt.Text = sb.ToString();
}

点击创建DAL

         /// <summary>
/// 点击创建BLL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiBll_Click(object sender, EventArgs e)
{
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName, out newClassName, out nameSpaceProfix);
string lowClassName=newClassName.ToLower();
#region 冗余
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using WinForm_UserInfo.DAL;
//using WinForm_UserInfo.Model;
#endregion
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".DAL;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".Model;");
#region 冗余
//namespace WinForm_UserInfo.BLL
//{
// public class UserInfoBLL
// {
// UserInfoDAL userDal = new UserInfoDAL(); // /// <summary>
// /// 根据用户名获取用户实例
// /// </summary>
// /// <param name="userName">用户名</param>
// /// <returns>一个用户实例</returns>
#endregion
sb.AppendLine();
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".BLL");
sb.AppendLine("{");
sb.Append(" public class ").Append(newClassName).AppendLine("BLL");
sb.AppendLine(" {");
sb.Append("\t").Append(newClassName).Append("DAL ").Append(lowClassName).Append("Dal = new ").Append(newClassName).AppendLine("DAL();"); #region 根据ID获得实例
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 根据ID获得实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>一个实例</returns>");
#region 冗余
//public UserInfo GetUserInfoById(int id)
//{
// return userDal.GetUserInfoById(id);
//}
#endregion
sb.Append("\tpublic ").Append(newClassName).Append(" Get").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t return ").Append(lowClassName).Append("Dal.Get").Append(newClassName).AppendLine("ById(id);");
sb.AppendLine("\t}");
#endregion #region 获得所有实例
#region 冗余
///// <summary>
///// 获得所有用户
///// </summary>
///// <returns>返回所有用户</returns>
//public List<UserInfo> GetAllUserInfoes()
//{
// return userDal.GetAllUserInfoes();
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 获得所有实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <returns>返回所有实例</returns>");
sb.Append("\tpublic List<").Append(newClassName).Append("> GetAll").Append(newClassName).AppendLine("es()");
sb.AppendLine("\t{");
sb.Append("\t return ").Append(lowClassName).Append("Dal.GetAll").Append(newClassName).AppendLine("es();");
sb.AppendLine("\t}");
#endregion #region 根据实例ID删除实例
#region 冗余
// /// <summary>
///// 根据用户ID删除用户
///// </summary>
///// <param name="id">用户ID</param>
///// <returns>是否删除成功</returns>
//public bool DeleteUserInfoById(int id)
//{
// int i = userDal.DeleteUserInfoById(id);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t /// <summary>");
sb.AppendLine("\t/// 根据实例ID删除实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>是否删除成功</returns>");
sb.Append("\tpublic bool Delete").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Delete").Append(newClassName).AppendLine("ById(id);");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion #region 新增实例
#region 冗余
///// <summary>
///// 新增用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>是否新增成功</returns>
//public bool InsertUserInfo(UserInfo user)
//{
// int i = userDal.InsertUserInfo(user);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 新增实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>是否新增成功</returns>");
sb.Append("\tpublic bool Insert").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Insert").Append(newClassName).Append("(").Append(lowClassName).AppendLine(");");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion #region 更新实例
#region 冗余
///// <summary>
///// 更新用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>师傅更新成功</returns>
//public bool UpdateUserInfo(UserInfo user)
//{
// int i = userDal.UpdateUserInfo(user);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 更新实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>是否更新成功</returns>");
sb.Append("\tpublic bool Update").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Update").Append(newClassName).Append("(").Append(lowClassName).AppendLine(");");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion sb.AppendLine(" }");
sb.AppendLine("}");
txt.Text = sb.ToString();
}

点击创建BLL

         /// <summary>
/// 点击创建Model
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiModel_Click(object sender, EventArgs e)
{
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName, out newClassName, out nameSpaceProfix);
string columnNameWithConsListStr, paraListStr, rowtomodelStr, columnNameWithEqualListStr, modelStr;
GetColumnListStrAndParaListStr(tabelName, newClassName, out columnNameWithConsListStr, out columnNameWithEqualListStr, out paraListStr, out rowtomodelStr, out modelStr);
#region 冗余
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
#endregion
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
#region 冗余
//namespace WinForm_UserInfo.Model
//{
// public class UserInfo
// {
// //Id UserName Pwd Mobile Email Address Gender
// public int Id { get; set; }
// public string UserName { get; set; }
// public string Pwd { get; set; }
// public string Mobile { get; set; }
// public string Email { get; set; }
// public string Address { get; set; }
// public int Gender { get; set; }
// }
//}
#endregion
sb.AppendLine();
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".Model");
sb.AppendLine("{");
sb.Append(" public class ").AppendLine(newClassName);
sb.AppendLine(" {");
sb.AppendLine(modelStr);
sb.AppendLine(" }");
sb.AppendLine("}");
txt.Text = sb.ToString();
}

点击创建Model

     }
}
 using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinForm_CreateGenerate_ForOracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} /// <summary>
/// 窗口加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
txtNamespaceProfix.Text = "WinForm_UserInfo";
txtdbconnStr.Text = @"Data Source=(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
);User Id=scott;Password=abcd5226584";
} /// <summary>
///点击 链接 链接Oracle数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
string connStr = txtdbconnStr.Text.Trim();
string sql = "select * from user_tables"; //查询Oracle数据库中当前用户的所有表的信息
try
{
using (OracleConnection conn = new OracleConnection(connStr))
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
conn.Open();
using (OracleDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string tableName = reader[].ToString();
cmbTables.Items.Add(tableName);
}
}
}
}
catch (Exception ex)
{
throw new Exception("出错:" + ex.Message.ToString());
}
} /// <summary>
/// 如果当前列可Null,并且数据类型不是String,需要拼接"?"
/// </summary>
/// <param name="isNull"></param>
/// <param name="cType"></param>
/// <returns></returns>
private string CheckJoinWenHao(string isNull,string cType)
{
if(isNull=="Y" && cType!="String")
{
return cType + "?";
}
else
{
return cType;
}
} /// <summary>
/// 获得列名连接字符串、参数连接字符串、Model连接字符串、ToModel连接字符串
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="newClassName">新类名</param>
/// <param name="columnNameWithConsListStr">列名连接字符串</param>
/// <param name="paraListStr">参数连接字符串</param>
private void GetColumnListStrAndParaListStr(string tableName, string newClassName, out string columnNameWithConsListStr, out string columnNameWithEqualListStr, out string paraListStr, out string rowtomodelStr, out string modelStr)
{
//查询当前表所有列名
string sql = "Select column_name,data_type,Nullable From user_tab_columns where table_name = :table_name"; //在当前用户的所有列中查询指定表的列信息
List<string> columnNameList = new List<string>();
List<string> columnNameWithConsList = new List<string>(); //带':'的列名集合
List<string> columnNameWithEqualList = new List<string>(); //带'='的列名集合
List<string> paraList = new List<string>(); //参数集合
rowtomodelStr = "";
StringBuilder sbModel = new StringBuilder(); try
{
using (OracleConnection conn = new OracleConnection(txtdbconnStr.Text.Trim()))
{
conn.Open();
using (OracleCommand cmd = new OracleCommand(sql, conn))
{
cmd.Parameters.Add(new OracleParameter(":table_name", tableName));
using (OracleDataReader reader = cmd.ExecuteReader())
{
long count = reader.RowSize; //行数 while (reader.Read())
{
string columnName = reader[].ToString();
string typeName = reader[].ToString();
string isDbNull = reader[].ToString();
string lowClassName = newClassName.ToLower(); //小写类名,表示实例
#region 拼接Model中的列字符串
//public string Address { get; set; }
//拼接Model中的列字符串
sbModel.Append("public ").Append(CheckJoinWenHao(isDbNull, DbTypeToCtype(typeName))).Append(" ").Append(columnName).AppendLine(" { get; set; }\n");
#endregion
#region 拼接RowToModel中的行转换
#region 冗余
//user.Id = Convert.ToInt32(row["Id"]);
//user.UserName = (string)row["UserName"]; //1 根据列的类型返回一个C#类型,2 判断是否为DbNull,3 判断类型是否为string
//user.Email = row["Email"] == DBNull.Value ? null : Convert.ToString(row["Email"]);
//拼接RowToModel
#endregion
//拼接RowToModel中的行转换
StringBuilder sb = new StringBuilder();
if (reader[].ToString() == "Y")
{
sb.Append(lowClassName).Append(".").Append(columnName).Append(" = row[\"").Append(columnName).Append("\"] == DBNull.Value ? null : Convert.To").Append(DbTypeToCtype(typeName)).Append("(row[\"").Append(columnName).AppendLine("\"]);");
}
else
{
sb.Append(newClassName.ToLower()).Append(".").Append(reader[].ToString()).Append(" = Convert.To").Append(DbTypeToCtype(reader[].ToString())).Append("(row[\"").Append(reader[].ToString()).AppendLine("\"]);");
}
rowtomodelStr += sb.ToString(); //累积RowToModel字符串
#endregion
#region 拼接列名和参数字符串
//拼接列名和参数字符串
if (reader[].ToString() != "ID") //去除ID项
{
columnNameList.Add(reader[].ToString());
columnNameWithConsList.Add(":" + reader[].ToString());
columnNameWithEqualList.Add(columnName + "=:" + columnName); //USERNAME=:USERNAME
//new OracleParameter(":Email", user.Email==null?(object)DBNull.Value:user.Email),
if (reader[].ToString() == "Y")
{
paraList.Add("new OracleParameter(\":" + columnName + "\", " + lowClassName + "." + columnName + "==null?(object)DBNull.Value:" + lowClassName + "." + columnName + ")");
}
else
{
paraList.Add("new OracleParameter(\":" + columnName + "\", " + lowClassName + "." + columnName + ")");
}
}
#endregion
}
}
}
}
}
catch (Exception ex)
{
throw new Exception("出错:" + ex.Message.ToString());
}
columnNameWithConsListStr = string.Join(",", columnNameWithConsList);
columnNameWithEqualListStr = string.Join(",", columnNameWithEqualList);
paraListStr = string.Join(",", paraList);
modelStr = sbModel.ToString(); //model中的列字符串
} /// <summary>
/// 把Oracle中的数据类型转C#中的数据类型
/// </summary>
/// <param name="dbType">Oracle中的数据类型 字符串</param>
/// <returns>C#中的数据类型 字符串</returns>
private string DbTypeToCtype(string dbType)
{
string ctype = "";
switch (dbType)
{
case "NUMBER": ctype = "Int32"; //未考虑小数点
break;
case "VARCHAR2": ctype = "String";
break;
case "DATE": ctype = "DateTime";
break;
default: throw new Exception("未知类型,需要添加新的类型");
}
return ctype;
} /// <summary>
/// 获得表名、新类名、命名空间前缀
/// </summary>
/// <param name="tabelName">表名</param>
/// <param name="newClassName">新类名</param>
/// <param name="nameSpaceProfix">命名空间前缀</param>
private void GetTableNameAndClassNameAndSpcaeName(out string tabelName, out string newClassName, out string nameSpaceProfix)
{
//表名
tabelName = cmbTables.SelectedItem.ToString();
//新类名
string typeProfix = txtTypeProfix.Text.Trim();
if (typeProfix.Length <= )
{
newClassName = tabelName;
}
else
{
newClassName = tabelName.Replace(typeProfix, ""); //去掉前缀 或后缀
}
nameSpaceProfix = txtNamespaceProfix.Text.Trim(); //当前类型所在命名空间
if (nameSpaceProfix.Length <= )
{
MessageBox.Show("请填命名空间");
return;
}
} /// <summary>
/// 点击创建DAL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiDll_Click(object sender, EventArgs e)
{
#region 冗余
////表名
//string tabelName=cmbTables.SelectedItem.ToString();
////新类名
//string typeProfix=txtTypeProfix.Text.Trim();
//string newClassName;
//if (typeProfix.Length <= 0)
//{
// newClassName = tabelName;
//}
//else
//{
// newClassName = tabelName.Replace(typeProfix, ""); //去掉前缀 或后缀
//}
//string nameSpaceProfix = txtNamespaceProfix.Text.Trim(); //当前类型所在命名空间
//if (nameSpaceProfix.Length <= 0)
//{
// MessageBox.Show("请填命名空间");
// return;
//}
#endregion
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName,out newClassName,out nameSpaceProfix);
string lowClassName = newClassName.ToLower();
string columnNameWithConsListStr, paraListStr, rowtomodelStr, columnNameWithEqualListStr, modelStr;
GetColumnListStrAndParaListStr(tabelName, newClassName, out columnNameWithConsListStr, out columnNameWithEqualListStr, out paraListStr, out rowtomodelStr, out modelStr);
//拼接DAL
StringBuilder sb = new StringBuilder();
#region 冗余
//using Oracle.DataAccess.Client;
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using WinForm_UserInfo.Model;
#endregion
sb.AppendLine("using Oracle.DataAccess.Client;");
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Data;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".Model;");
#region 冗余
//
//namespace WinForm_UserInfo.DAL
//{
// public class UserInfoDAL
// {
// /// <summary>
// /// 根据用户名获取用户实例
// /// </summary>
// /// <param name="userName">用户名</param>
// /// <returns>一个用户实例</returns>
#endregion
sb.AppendLine("");
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".DAL");
sb.AppendLine("{");
sb.Append(" public class ").Append(newClassName).AppendLine("DAL");
sb.AppendLine(" {"); #region 根据对象ID获取实例
sb.AppendLine(" /// <summary>");
sb.AppendLine(" /// 根据对象ID获取实例");
sb.AppendLine(" /// </summary>");
sb.AppendLine(" /// <param name=\"id\">ID</param>");
sb.AppendLine(" /// <returns>一个实例</returns>");
#region 冗余
//public UserInfo GetUserInfoById(int id)
//{
// string sql = "SELECT * FROM T_USERINFO WHERE ID=:ID";
// DataTable dt = OracleHelper.ExecuteReader(sql, new OracleParameter(":ID", id));
// if (dt.Rows.Count <= 0)
// {
// return null;
// }
// else if (dt.Rows.Count == 1)
// {
// return RowToModel(dt.Rows[0]);
// }
// else
// {
// throw new Exception("数据重复,重复数据为:" + id);
// }
//}
#endregion
sb.Append("\tpublic ").Append(newClassName).Append(" Get").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"SELECT * FROM ").Append(tabelName).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t DataTable dt = OracleHelper.ExecuteReader(sql, new OracleParameter(\":ID\", id));");
sb.AppendLine("\t if (dt.Rows.Count <= 0)");
sb.AppendLine("\t {");
sb.AppendLine("\t return null;");
sb.AppendLine("\t }");
sb.AppendLine("\t else if (dt.Rows.Count == 1)");
sb.AppendLine("\t {");
sb.AppendLine("\t return RowToModel(dt.Rows[0]);");
sb.AppendLine("\t }");
sb.AppendLine("\t else");
sb.AppendLine("\t {");
sb.AppendLine("\t throw new Exception(\"数据重复,重复数据为:\" + id);");
sb.AppendLine("\t }");
sb.AppendLine("\t}");
#endregion #region 获得所有实例
#region 冗余
//
///// <summary>
///// 获得所有用户
///// </summary>
///// <returns>返回所有用户</returns>
//public List<UserInfo> GetAllUserInfoes()
//{
// List<UserInfo> users=new List<UserInfo>();
// string sql = "SELECT * FROM T_USERINFO";
// DataTable dt = OracleHelper.ExecuteReader(sql);
// if (dt.Rows.Count <= 0)
// {
// return null;
// }
// else
// {
// foreach(DataRow row in dt.Rows)
// {
// users.Add(RowToModel(row));
// }
// return users;
// }
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 获得所有实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <returns>返回所有实例</returns>");
sb.Append("\tpublic List<").Append(newClassName).Append("> GetAll").Append(newClassName).AppendLine("es()");
sb.AppendLine("\t{");
sb.Append("\t List<").Append(newClassName).Append("> ").Append(newClassName.ToLower()).Append("s=new List<").Append(newClassName).AppendLine(">();");
sb.Append("\t string sql = \"SELECT * FROM ").Append(tabelName).AppendLine("\";");
sb.AppendLine("\t DataTable dt = OracleHelper.ExecuteReader(sql);");
sb.AppendLine("\t if (dt.Rows.Count <= 0)");
sb.AppendLine("\t {");
sb.AppendLine("\t return null;");
sb.AppendLine("\t }");
sb.AppendLine("\t else");
sb.AppendLine("\t {");
sb.AppendLine("\t foreach(DataRow row in dt.Rows)");
sb.AppendLine("\t {");
sb.Append("\t ").Append(newClassName.ToLower()).AppendLine("s.Add(RowToModel(row));");
sb.AppendLine("\t }");
sb.Append("\t return ").Append(newClassName.ToLower()).AppendLine("s;");
sb.AppendLine("\t }");
sb.AppendLine("\t}");
#endregion #region 根据实例ID删除实例
#region 冗余
//
///// <summary>
///// 根据用户ID删除用户
///// </summary>
///// <param name="id">用户ID</param>
///// <returns>受影响行数</returns>
//public int DeleteUserInfoById(int id)
//{
// string sql = "DELETE FROM T_USERINFO WHERE ID=:ID";
// return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(":ID", id));
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 根据实例ID删除实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Delete").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"DELETE FROM ").Append(tabelName).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(\":ID\", id));");
sb.AppendLine("\t}");
#endregion #region 新增实例
#region 冗余
//
///// <summary>
///// 新增用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>受影响行数</returns>
//public int InsertUserInfo(UserInfo user)
//{
// //SE_T_USERINFO.NEXTVAL
// string sql = "INSERT INTO T_USERINFO VALUES(SE_T_USERINFO.NEXTVAL,:USERNAME,:PWD,:MOBILE,:EMAIL,:ADDRESS,:GENDER)";
// return OracleHelper.ExecuteNonQuery(sql,
// new OracleParameter(":USERNAME", user.UserName),
// new OracleParameter(":PWD", user.Pwd),
// new OracleParameter(":MOBILE", user.Mobile),
// new OracleParameter(":EMAIL", user.Email),
// new OracleParameter(":ADDRESS", user.Address),
// new OracleParameter(":GENDER", user.Gender)
// );
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 新增实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(newClassName.ToLower()).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Insert").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(newClassName.ToLower()).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"INSERT INTO ").Append(tabelName).Append(" VALUES(SE_T_USERINFO.NEXTVAL,").Append(columnNameWithConsListStr).AppendLine(")\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql,");
sb.Append("\t ").AppendLine(paraListStr);
sb.AppendLine("\t );");
sb.AppendLine("\t}");
#endregion #region 更新实例
#region 冗余
///// <summary>
///// 更新用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>受影响行数</returns>
//public int UpdateUserInfo(UserInfo user)
//{
// string sql = "UPDATE T_USERINFO SET USERNAME=:USERNAME,PWD=:PWD,MOBILE=:MOBILE,EMAIL=:EMAIL,ADDRESS=:ADDRESS,GENDER=:GENDER WHERE ID=:ID";
// return OracleHelper.ExecuteNonQuery(sql, new OracleParameter(":USERNAME", user.UserName),
// new OracleParameter(":PWD", user.Pwd),
// new OracleParameter(":MOBILE", user.Mobile),
// new OracleParameter(":EMAIL", user.Email),
// new OracleParameter(":ADDRESS", user.Address),
// new OracleParameter(":GENDER", user.Gender),
// new OracleParameter(":ID", user.Id)
// );
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 更新实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>受影响行数</returns>");
sb.Append("\tpublic int Update").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t string sql = \"UPDATE ").Append(tabelName).Append(" SET ").Append(columnNameWithEqualListStr).AppendLine(" WHERE ID=:ID\";");
sb.AppendLine("\t return OracleHelper.ExecuteNonQuery(sql,");
sb.Append("\t ").AppendLine(paraListStr);
sb.Append("\t\t,new OracleParameter(\":ID\",").Append(lowClassName).AppendLine(".ID)");
sb.AppendLine("\t );");
sb.AppendLine("\t}");
#endregion #region Row转Model
#region 冗余
///// <summary>
///// DataRow转Model
///// </summary>
///// <param name="row">表中一行数据</param>
///// <returns>一个对象实例</returns>
//private UserInfo RowToModel(DataRow row)
//{
// //Id UserName Pwd Mobile Email Address Gender
// UserInfo user = new UserInfo();
// user.Id = Convert.ToInt32(row["Id"]);
// user.UserName = (string)row["UserName"];
// user.Pwd = (string)row["Pwd"];
// user.Mobile = (string)row["Mobile"];
// user.Email = row["Email"] == DBNull.Value ? null : (string)row["Email"];
// user.Address = row["Address"] == DBNull.Value ? null : (string)row["Email"];
// user.Gender = Convert.ToInt32(row["Gender"]);
// return user;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// DataRow转Model");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"row\">表中一行数据</param>");
sb.AppendLine("\t/// <returns>一个对象实例</returns>");
sb.Append("\tprivate ").Append(newClassName).AppendLine(" RowToModel(DataRow row)");
sb.AppendLine("\t{");
sb.Append("\t ").Append(newClassName).Append(" ").Append(lowClassName).Append(" = new ").Append(newClassName).AppendLine("();");
sb.Append("\t ").AppendLine(rowtomodelStr);
sb.Append("\t return ").Append(lowClassName).AppendLine(";");
sb.AppendLine("\t}");
#endregion sb.AppendLine("\t}");
sb.AppendLine("}");
txt.Text = sb.ToString();
} /// <summary>
/// 点击创建BLL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiBll_Click(object sender, EventArgs e)
{
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName, out newClassName, out nameSpaceProfix);
string lowClassName=newClassName.ToLower();
#region 冗余
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using WinForm_UserInfo.DAL;
//using WinForm_UserInfo.Model;
#endregion
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".DAL;");
sb.Append("using ").Append(nameSpaceProfix).AppendLine(".Model;");
#region 冗余
//namespace WinForm_UserInfo.BLL
//{
// public class UserInfoBLL
// {
// UserInfoDAL userDal = new UserInfoDAL(); // /// <summary>
// /// 根据用户名获取用户实例
// /// </summary>
// /// <param name="userName">用户名</param>
// /// <returns>一个用户实例</returns>
#endregion
sb.AppendLine();
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".BLL");
sb.AppendLine("{");
sb.Append(" public class ").Append(newClassName).AppendLine("BLL");
sb.AppendLine(" {");
sb.Append("\t").Append(newClassName).Append("DAL ").Append(lowClassName).Append("Dal = new ").Append(newClassName).AppendLine("DAL();"); #region 根据ID获得实例
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 根据ID获得实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>一个实例</returns>");
#region 冗余
//public UserInfo GetUserInfoById(int id)
//{
// return userDal.GetUserInfoById(id);
//}
#endregion
sb.Append("\tpublic ").Append(newClassName).Append(" Get").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t return ").Append(lowClassName).Append("Dal.Get").Append(newClassName).AppendLine("ById(id);");
sb.AppendLine("\t}");
#endregion #region 获得所有实例
#region 冗余
///// <summary>
///// 获得所有用户
///// </summary>
///// <returns>返回所有用户</returns>
//public List<UserInfo> GetAllUserInfoes()
//{
// return userDal.GetAllUserInfoes();
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 获得所有实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <returns>返回所有实例</returns>");
sb.Append("\tpublic List<").Append(newClassName).Append("> GetAll").Append(newClassName).AppendLine("es()");
sb.AppendLine("\t{");
sb.Append("\t return ").Append(lowClassName).Append("Dal.GetAll").Append(newClassName).AppendLine("es();");
sb.AppendLine("\t}");
#endregion #region 根据实例ID删除实例
#region 冗余
// /// <summary>
///// 根据用户ID删除用户
///// </summary>
///// <param name="id">用户ID</param>
///// <returns>是否删除成功</returns>
//public bool DeleteUserInfoById(int id)
//{
// int i = userDal.DeleteUserInfoById(id);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t /// <summary>");
sb.AppendLine("\t/// 根据实例ID删除实例");
sb.AppendLine("\t/// </summary>");
sb.AppendLine("\t/// <param name=\"id\">实例ID</param>");
sb.AppendLine("\t/// <returns>是否删除成功</returns>");
sb.Append("\tpublic bool Delete").Append(newClassName).AppendLine("ById(int id)");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Delete").Append(newClassName).AppendLine("ById(id);");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion #region 新增实例
#region 冗余
///// <summary>
///// 新增用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>是否新增成功</returns>
//public bool InsertUserInfo(UserInfo user)
//{
// int i = userDal.InsertUserInfo(user);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 新增实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>是否新增成功</returns>");
sb.Append("\tpublic bool Insert").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Insert").Append(newClassName).Append("(").Append(lowClassName).AppendLine(");");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion #region 更新实例
#region 冗余
///// <summary>
///// 更新用户
///// </summary>
///// <param name="user">用户实例</param>
///// <returns>师傅更新成功</returns>
//public bool UpdateUserInfo(UserInfo user)
//{
// int i = userDal.UpdateUserInfo(user);
// return i > 0;
//}
#endregion
sb.AppendLine();
sb.AppendLine("\t/// <summary>");
sb.AppendLine("\t/// 更新实例");
sb.AppendLine("\t/// </summary>");
sb.Append("\t/// <param name=\"").Append(lowClassName).AppendLine("\">实例</param>");
sb.AppendLine("\t/// <returns>是否更新成功</returns>");
sb.Append("\tpublic bool Update").Append(newClassName).Append("(").Append(newClassName).Append(" ").Append(lowClassName).AppendLine(")");
sb.AppendLine("\t{");
sb.Append("\t int i = ").Append(lowClassName).Append("Dal.Update").Append(newClassName).Append("(").Append(lowClassName).AppendLine(");");
sb.AppendLine("\t return i > 0;");
sb.AppendLine("\t}");
#endregion sb.AppendLine(" }");
sb.AppendLine("}");
txt.Text = sb.ToString();
} /// <summary>
/// 点击创建Model
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiModel_Click(object sender, EventArgs e)
{
string tabelName, newClassName, nameSpaceProfix;
GetTableNameAndClassNameAndSpcaeName(out tabelName, out newClassName, out nameSpaceProfix);
string columnNameWithConsListStr, paraListStr, rowtomodelStr, columnNameWithEqualListStr, modelStr;
GetColumnListStrAndParaListStr(tabelName, newClassName, out columnNameWithConsListStr, out columnNameWithEqualListStr, out paraListStr, out rowtomodelStr, out modelStr);
#region 冗余
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
#endregion
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Text;");
sb.AppendLine("using System.Threading.Tasks;");
#region 冗余
//namespace WinForm_UserInfo.Model
//{
// public class UserInfo
// {
// //Id UserName Pwd Mobile Email Address Gender
// public int Id { get; set; }
// public string UserName { get; set; }
// public string Pwd { get; set; }
// public string Mobile { get; set; }
// public string Email { get; set; }
// public string Address { get; set; }
// public int Gender { get; set; }
// }
//}
#endregion
sb.AppendLine();
sb.Append("namespace ").Append(nameSpaceProfix).AppendLine(".Model");
sb.AppendLine("{");
sb.Append(" public class ").AppendLine(newClassName);
sb.AppendLine(" {");
sb.AppendLine(modelStr);
sb.AppendLine(" }");
sb.AppendLine("}");
txt.Text = sb.ToString();
}
}
}

完整三层源代码

三层自动生成 完整源代码(for oracle)的更多相关文章

  1. [原创]超强C#图片上传,加水印,自动生成缩略图源代码

    <%@ Page Language=“C#“ AutoEventWireup=“true“ %> <%@ Import Namespace=“System“ %> <%@ ...

  2. (转)MyEclipse自动生成Hibernate实体类, oracle篇

    转自http://blog.csdn.net/hejinwei_1987/article/details/9465529 1.打开 windows -> Open Perspective -&g ...

  3. iBatis自动生成的主键 (Oracle,MS Sql Server,MySQL)【转】

    iBatis的sqlMap配置文件的selectKey元素有个type属性,可以指定pre或者post表示前生成(pre)还是后生成(post). Oracle设置 <!-- Oracle SE ...

  4. 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)

    前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...

  5. 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)

    利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引) ##应用情景 项目中有大量的SQL,尤其是涉及到统计报表时,表关联比较多,当初开发建表时也没搞好索引关联的,上线后 ...

  6. 基于OCILIB的oracle数据库操作总结及自动生成Model和Dao的工具

    基于OCILIB的oracle数据库操作总结 1.       类图 2.       基类BaseOCIDao的设计与实现 BaseOCIDao.h头文件 #pragma once /* ----- ...

  7. Oracle 每天自动生成AWR报告

    经验丰富的老员工希望能够每天为数据库生成1个AWR报告,以便于后期分析数据库的性能变化,手动生成太麻烦,查了一下资料,发现可以自动生成,过程如下. 数据库环境:11gR2 RAC(双节点) AWR报告 ...

  8. Oracle 函数 “自动生成订单号”

    create or replace function get_request_code return varchar2 AS --函数的作用:自动生成订单号 v_mca_no mcode_apply_ ...

  9. SSM 框架基于ORACLE集成TKMYBATIS 和GENERATOR自动生成代码(Github源码)

    基于前一个博客搭建的SSM框架 https://www.cnblogs.com/jiangyuqin/p/9870641.html 源码:https://github.com/JHeaven/ssm- ...

随机推荐

  1. php数组函数-array_key_exists()

    array_key_exists()函数判断某个数组中是否存在指定的key,如果key存 在,则返回true,否则返回flase array_key_exists(key,array); key:必需 ...

  2. 在IOS开发中,项目的目录结构如何搭建?

    网上有很多关于IOS开发的学习资料.然而却很少有关于设计一个项目时,如何设计其目录结构?这对于自学IOS的程序猿们,无疑有诸多不利.接下来,我就简单的谈下真正在公司中,项目的目录结构如何搭建: 以上为 ...

  3. SpringBoot Mybatis 入门

    Mybatis for Java API官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html Mybatis语法介绍 @Select 查询,所有 ...

  4. Kubernetes Metrics-Server

    github地址:https://github.com/kubernetes-incubator/metrics-server 官网介绍:https://kubernetes.io/docs/task ...

  5. JMeter学习(八)JDBC Request

    [step_1]:“测试计划”--(右键)à添加à线程组: [step_2]:选择step_1中添加的线程组—(右键)à添加à配置元件àJDBC Connection Configuration,添加 ...

  6. HIVE HSQL 基本操作命令

    创建表: hive>create table tablename(id int,name string,password string); 创建一个名字为tablename的表,表的属性有int ...

  7. UOJ104 【APIO2014】Split the sequence

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. FilterDispatcher处理流程

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 前身是:org.apache.struts2.dispatc ...

  9. 机器学习(八)—GBDT 与 XGBOOST

    RF.GBDT和XGBoost都属于集成学习(Ensemble Learning),集成学习的目的是通过结合多个基学习器的预测结果来改善单个学习器的泛化能力和鲁棒性.  根据个体学习器的生成方式,目前 ...

  10. 机器学习(六)—随机森林Random Forest

    1.什么是随机采样? Bagging可以简单的理解为:放回抽样,多数表决(分类)或简单平均(回归): Bagging的弱学习器之间没有boosting那样的联系,不存在强依赖关系,基学习器之间属于并列 ...