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. python补充知识点

    1. 在python2中用xrange,在python3中直接使用range就好了 2. 常数 None在逻辑判断的时候指代False,其他方式不代表True或者False 3. for循环只作用域容 ...

  2. 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...

  3. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  4. 为多个文件夹下的C源代码编写Makefile文件

    上一篇文章写了如何为在同一个文件夹下的C源代码,本篇文章为多个文件夹下的C源代码编写Makefile文件. 建立两个文件夹,分别为abs与src.其最终目录结构如下: 1 $ ls * 2 jun.c ...

  5. 在Java项目中部署使用Memcached[转]

    在项目中使用到Memcached主要的目的是,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态.数据库驱动网站的速度.提高可扩展性.Memcached是一个高性能的分布式内存对象缓存系统,基于 ...

  6. 安装配置Apollo-Prota web中心平台

    首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...

  7. 使用Navicat连接oracle时出现unsupported server character set ZHS16GBK的解决之道

    原文网址http://blog.mn886.net/chenjianhua/show/ba1dc6f835be403ea159b0a5e2685ff2/index.html ORA-12737:Ins ...

  8. hadoop 伪分布模式环境搭建

    一 安装JDK 下载JDK      jdk-8u112-linux-i586.tar.gz 解压JDK     hadoop@ubuntu:/soft$ tar -zxvf jdk-8u112-li ...

  9. jq中append()、prepend()、after()、before()的区别详解

    1.append() - 在被选元素的结尾插入内容(内容的结尾,比如说有个a标签,则是在</a>这个标签之前添加东西) 2.prepend() - 在被选元素的开头插入内容(内容的开始,比 ...

  10. 解决:python命令行运行出错 ImportError: No module named ...

    一. 发现问题 今天在cmd命令行运行一个py文件,本来在pycharm中运行好好的文件,在命令行却报错了,直接提示我:ImportError: No module named 'homeworks' ...