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. Eclipse与Tomcat的集成(无插件)

    1.下载Eclipse(https://www.eclipse.org/downloads/)和Tomcat(http://tomcat.apache.org/),具体的安装略: 2.打开Eclips ...

  2. Anaconda创建环境、删除环境、激活环境、退出环境

    Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6  删除环境(不要乱删啊啊啊) conda re ...

  3. Luogu-3966 [TJOI2013]单词

    这道题应该是后缀数组的套路题啊,把单词连接起来,中间用没有出现过且互不相同的字符来分隔开,求一下\(height\)数组. 对于一个单词来说,设单词长\(len\),所在的后缀为\(i\),如果某后缀 ...

  4. mysql安装失败后重装的方法

  5. Python-flask中数据库连接池DBUtils

    一.DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 连接池的三种模式: 第一种模式:             它的缺点:每一次请求反复创建数据库的链接,链接的次数太多 ...

  6. 调用摄像头并将其显示在UGUI image上自适应屏幕大小

    参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html 不过该博主是竖屏,我的是横屏 代码修改: using UnityEngine; using S ...

  7. windows live writer 原始图片大小设置

          点击图片,右面对图片参数进行设置,然后点击保存默认设置. 那么以后再插入图片,就不要重新操作了.    

  8. 时间服务器: NTP 服务器及客户端搭建

    时间服务器: NTP 服务器及客户端搭建 一. NTP 服务器的安装与设定 1. NTP 服务器的安装与设定前言 2. 所需软件与软件结构 3. 主要配置文件 ntp.conf 的处理 4. NTP ...

  9. vue router按需加载

    import Vue from 'vue' import Router from 'vue-router' Vue.use(Router); //按需加载,当渲染其他页面时才加载其组件,并缓存,减少首 ...

  10. SVN中如何为文件夹中的所有文件加锁

    经过一段时间的试用,发现不加锁的共享式开发还是不太方便.还是全部设置为独占式加锁,如有共享式修改需求再设置为不加锁比较好. 经过一番摸索,总结出如下的加锁方式是可行的: 注:第一步是必须的,必须完成第 ...