[置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)
自己写代码生成器之生成Dal层代码(获取数据库所有表名称)
--得到数据库birthday所有表名称
select name from sysobjects where [type]='U'
--select [TABLE_NAME] from INFORMATION_SCHEMA.TABLES where [TABLE_TYPE]='BASE TABLE'
--获取列信息,不获取数据
select top 0 * from userInfo
思路:拼接字符串
/// <summary>
/// 加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Index_Load(object sender, EventArgs e)
{
string sql = "select [TABLE_NAME] from INFORMATION_SCHEMA.TABLES where [TABLE_TYPE]='BASE TABLE'";
DataTable data = SqlHelper.ExeccutDataTable(sql);
if (data.Rows.Count > 0)
{
this.cmb_tableName.Items.Clear();
foreach (DataRow row in data.Rows)
{
string name = row["TABLE_NAME"].ToString();
this.cmb_tableName.Items.Add(name); }
this.cmb_tableName.SelectedIndex = 0;
}
} /// <summary>
/// 生成代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_generate_Click(object sender, EventArgs e)
{
string tableName = this.cmb_tableName.SelectedItem.ToString();
string sql = "select top 0 * from " + tableName;
DataTable data = SqlHelper.ExeccutDataTable(sql); ///生成Dal层代码
GenerateDalCode(tableName, data);
} #region Dal
/// <summary>
/// 生成Dal层代码
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
private void GenerateDalCode(string tableName, DataTable data)
{
StringBuilder str = new StringBuilder(); //引用信息
str.AppendLine("using System;");
str.AppendLine("using System.Collections.Generic;");
str.AppendLine("using System.Data;");
str.AppendLine("using System.Data.SqlClient;");
str.AppendLine("using System.Linq;");
str.AppendLine("using System.Text;");
str.AppendLine("\r");
//命名空间
str.AppendLine("namespace AutoCodeKldder");
str.AppendLine("{");
//注释信息
str.AppendLine("\t/// <summary>");
str.AppendLine("\t/// " + tableName + "Service");
str.AppendLine("\t/// <summary>");
str.AppendLine("\t[Serializable]");
str.AppendLine("\tpublic class " + tableName + "Service");
str.AppendLine("\t{"); //GetModel
GetModel(tableName, data, str); //Add
Add(tableName, data, str); //AddOutPutField
AddOutPutField(tableName, data, str); //Update
Update(tableName, data, str); //Delete
Delete(tableName, data, str); //FullDataTable
FullDataTable(tableName, data, str); //FullDataSet
FullDataSet(tableName, data, str); //FullDataRow
FullDataRow(tableName, data, str); //GetRecordCount
GetRecordCount(tableName, data, str); str.AppendLine("\t}");
str.AppendLine("}");
this.txt_dalcode.Text = str.ToString().Trim();
} /// <summary>
/// 得到一条实体
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void GetModel(string tableName, DataTable data, StringBuilder str)
{
//方法一
str.AppendLine("\t\t#region GetModel");
//方法注释
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t///得到一条" + tableName + "实体");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"_row\">DataRow</param>");
str.AppendLine("\t\t /// <returns>" + tableName + "实体</returns>");
str.AppendLine("\t\tpublic " + tableName + " GetModel(DataRow _row)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\t" + tableName + " _" + tableName + " = new " + tableName + "();");
foreach (DataColumn dc in data.Columns)
{
//字段名称
string _name = dc.ColumnName;
//字段类型
string _type = dc.DataType.ToString();
str.AppendLine("\t\t\tif (_row[\"" + _name + "\"] != null)");
str.AppendLine("\t\t\t{");
str.AppendLine("\t\t\t\t_" + tableName + "." + _name + " = (" + _type + ")_row[\"" + _name + "\"];");
str.AppendLine("\t\t\t}");
}
str.AppendLine("\t\t\treturn _" + tableName + ";");
str.AppendLine("\t\t}");
str.AppendLine(""); //方法二
//方法注释
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t///得到一条" + tableName + "实体");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t /// <returns>" + tableName + "实体</returns>");
str.AppendLine("\t\tpublic " + tableName + " GetModel(string sql)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn GetModel(SqlHelper.ExecuteDataRow(sql));");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 添加
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void Add(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region Add");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 添加");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"_" + tableName + "\">" + tableName + "实体</param>");
str.AppendLine("\t\t/// <returns>是否成功</returns>");
str.AppendLine("\t\tpublic bool Add(" + tableName + " _" + tableName + ")");
str.AppendLine("\t\t{");
//拼接T-sql语句和SqlParameter[]
string str_sql_1 = "\"insert into [dbo].[userInfo](";
string str_sql_2 = "\r\n\t\t\t_sql+= \"vlaues(";
string str_param = "SqlParameter[] _param = {";
int idx = 0;
foreach (DataColumn dc in data.Columns)
{
string _name = dc.ColumnName;
string _dh = ",";
//最后一个字段没有逗号
if (idx == data.Columns.Count - 1)
{
_dh = "";
}
//不是自增长字段
if (!dc.AutoIncrement)
{
str_sql_1 += "[" + _name + "]" + _dh;
str_sql_2 += "@" + _name + _dh;
//str_param += "\r\n\t\t\t\t\t\t\tnew SqlParameter(\"@" + _name + "\", System.Data.SqlDbType.Int) { Value = _" + tableName + "." + _name + " }" + _dh;
str_param += "\r\n\t\t\t\t\t\t\tnew SqlParameter(\"@" + _name + "\",_userInfo." + _name + ")" + _dh;
}
idx++;
}
str_sql_1 += ")\";";
str_sql_2 += ")\";";
str_param += "\r\n\t\t\t\t\t\t};";
str.AppendLine("\t\t\tstring _sql = " + str_sql_1 + str_sql_2);
str.AppendLine("\t\t\t" + str_param);
str.AppendLine("\t\t\treturn SqlHelper.ExecuteNonQuery(_sql,_param) > 0;");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 添加并返回指定列
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void AddOutPutField(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region AddOutPutField");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 添加并返回指定列");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"_" + tableName + "\">" + tableName + "实体</param>");
str.AppendLine("\t\t/// <param name=\"_field\">指定列</param>");
str.AppendLine("\t\t/// <returns>指定列</returns>");
str.AppendLine("\t\tpublic object AddOutPutField(" + tableName + " _" + tableName + ", object _field)");
str.AppendLine("\t\t{");
//拼接T-sql语句和SqlParameter[]
string str_sql_1 = "\"insert into [dbo].[userInfo](";
string str_sql_2 = "\r\n\t\t\t_sql+= \" output inserted.\" + _field;\r\n\t\t\t_sql+= \" vlaues(";
string str_param = "SqlParameter[] _param = {";
int idx = 0;
foreach (DataColumn dc in data.Columns)
{
string _name = dc.ColumnName;
string _dh = ",";
//最后一个字段没有逗号
if (idx == data.Columns.Count - 1)
{
_dh = "";
}
//不是自增长字段
if (!dc.AutoIncrement)
{
str_sql_1 += "[" + _name + "]" + _dh;
str_sql_2 += "@" + _name + _dh;
str_param += "\r\n\t\t\t\t\t\t\tnew SqlParameter(\"@" + _name + "\",_userInfo." + _name + ")" + _dh;
}
idx++;
}
str_sql_1 += ")\";";
str_sql_2 += ")\";";
str_param += "\r\n\t\t\t\t\t\t};";
str.AppendLine("\t\t\tstring _sql = " + str_sql_1 + str_sql_2);
str.AppendLine("\t\t\t" + str_param);
str.AppendLine("\t\t\treturn SqlHelper.ExecuteScalar(_sql,_param);");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 根据主键修改
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void Update(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region Update");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 根据主键修改");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"_" + tableName + "\">" + tableName + "实体</param>");
str.AppendLine("\t\t/// <returns>是否成功</returns>");
str.AppendLine("\t\tpublic bool Update(" + tableName + " _" + tableName + ")");
str.AppendLine("\t\t{");
//拼接T-sql语句和SqlParameter[]
string str_sql = "\"update [dbo].[userInfo] set ";
string str_param = "SqlParameter[] _param = {";
int idx = 0;
//标识字段
string _AutoIncrement = "";
foreach (DataColumn dc in data.Columns)
{
string _name = dc.ColumnName;
string _dh = ",";
//最后一个字段没有逗号
if (idx == data.Columns.Count - 1)
{
_dh = "";
}
//不是自增长字段
if (!dc.AutoIncrement)
{
str_sql += "[" + _name + "]" + "=@" + _name + _dh;
}
else
{
_AutoIncrement = dc.ColumnName;
}
str_param += "\r\n\t\t\t\t\t\t\tnew SqlParameter(\"@" + _name + "\",_userInfo." + _name + ")" + _dh;
idx++;
}
str_sql += "\";\r\n\t\t\t_sql += \" where " + _AutoIncrement + "=@" + _AutoIncrement + "\";";
str_param += "\r\n\t\t\t\t\t\t};";
str.AppendLine("\t\t\tstring _sql = " + str_sql);
str.AppendLine("\t\t\t" + str_param);
str.AppendLine("\t\t\treturn SqlHelper.ExecuteNonQuery(_sql, _param) > 0;");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 根据主键删除
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void Delete(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region Delete");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 根据主键删除");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"id\">主键</param>");
str.AppendLine("\t\t/// <returns>是否成功</returns>");
str.AppendLine("\t\tpublic bool Delete(int id)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\tstring _sql = \"delete from [dbo].[userInfo] where [id]=@id\";");
str.AppendLine("\t\t\tSqlParameter[] _param = {");
str.AppendLine("\t\t\t\t\t\t\t\t\t\tnew SqlParameter(\"@id\",id)");
str.AppendLine("\t\t\t\t\t\t\t\t\t};");
str.AppendLine("\t\t\treturn SqlHelper.ExecuteNonQuery(_sql, _param) > 0;");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 填充DataTable
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void FullDataTable(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region FullDataTable");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataTable");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataTable</returns>");
str.AppendLine("\t\tpublic DataTable FullDataTable(string _sql)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExeccutDataTable(_sql);");
str.AppendLine("\t\t}");
str.AppendLine("");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataTable");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataTable</returns>");
str.AppendLine("\t\tpublic DataTable FullDataTable(string _sql, SqlParameter[] _para)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExeccutDataTable(_sql, _para);");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 填充DataSet
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void FullDataSet(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region FullDataSet");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataSet");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataSet</returns>");
str.AppendLine("\t\tpublic DataSet FullDataSet(string _sql)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExeccutDataSet(_sql);");
str.AppendLine("\t\t}");
str.AppendLine("");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataSet");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataSet</returns>");
str.AppendLine("\t\tpublic DataSet FullDataSet(string _sql, SqlParameter[] _para)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExeccutDataSet(_sql, _para);");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 填充DataRow
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void FullDataRow(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region FullDataRow");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataRow");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataRow</returns>");
str.AppendLine("\t\tpublic DataRow FullDataRow(string _sql)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExecuteDataRow(_sql);");
str.AppendLine("\t\t}");
str.AppendLine("");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 填充DataRow");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>DataRow</returns>");
str.AppendLine("\t\tpublic DataRow FullDataRow(string _sql, SqlParameter[] _para)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.ExecuteDataRow(_sql, _para);");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} /// <summary>
/// 返回数据记录条数
/// </summary>
/// <param name="tableName">表名称</param>
/// <param name="data">数据源</param>
/// <param name="str">字符串</param>
private static void GetRecordCount(string tableName, DataTable data, StringBuilder str)
{
str.AppendLine("");
str.AppendLine("\t\t#region GetRecordCount");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 返回数据记录条数");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <returns>记录条数</returns>");
str.AppendLine("\t\tpublic int GetRecordCount(string _sql)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.GetRecordCount(_sql);");
str.AppendLine("\t\t}");
str.AppendLine("");
str.AppendLine("\t\t/// <summary>");
str.AppendLine("\t\t/// 返回数据记录条数");
str.AppendLine("\t\t/// </summary>");
str.AppendLine("\t\t/// <param name=\"sql\">T-sql语句</param>");
str.AppendLine("\t\t/// <param name=\"_where\">过滤条件</param>");
str.AppendLine("\t\t/// <returns>记录条数</returns>");
str.AppendLine("\t\tpublic int GetRecordCount(string _sql, string _where)");
str.AppendLine("\t\t{");
str.AppendLine("\t\t\treturn SqlHelper.GetRecordCount(_sql, _where);");
str.AppendLine("\t\t}");
str.AppendLine("\t\t#endregion");
} #endregion
/// <summary>
/// 复制代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_copy_Click(object sender, EventArgs e)
{
Clipboard.Clear();
if (this.tabControl.SelectedIndex == 0)
{
if (string.IsNullOrWhiteSpace(this.txt_modelcode.Text))
{
return;
}
else
{
Clipboard.SetText(this.txt_modelcode.Text);
MessageBox.Show("成功复制Models代码。");
}
}
else
{
if (string.IsNullOrWhiteSpace(this.txt_dalcode.Text))
{
return;
}
else
{ Clipboard.SetText(this.txt_dalcode.Text);
MessageBox.Show("成功复制Dal代码。");
}
}
[置顶] 自己写代码生成器之生成Dal层代码(获取数据库所有表名称)的更多相关文章
- [置顶] 自己写sqlhelper类
自己写sqlhelper类 using System; using System.Collections.Generic; using System.Configuration; using Syst ...
- [置顶]
自己写一个简单通用的Makefile
转自:http://blog.csdn.net/u011913612/article/details/52102241 一.makefile的作用 Makefile是用于自动编译和链接的,一个工程有很 ...
- [置顶] 如何在Python IDLE中调试Python代码?
好久没有用Python了,居然忘记了怎么在Python IDLE中调试Python代码.百度了一下,然后还是写下来吧,以免以后又忘记了. 1. Set break point in the sourc ...
- 解决Popup StayOpen=true时,永远置顶的问题
Popup设置了StayOpen=true时,会置顶显示. 如弹出了Popup后,打开QQ窗口,Popup显示在QQ聊天界面之上. 怎么解决问题? 获取绑定UserControl所在的窗口,窗口层级变 ...
- Popup 解决置顶显示问题
原文:Popup 解决置顶显示问题 前言 Popup显示时会置顶显示.尤其是 Popup设置了StayOpen=true时,会一直置顶显示,问题更明显. 置顶显示问题现象: 解决方案 怎么解决问题? ...
- MySQL 上移/下移/置顶
在编写网站系统时,难免会用到上移.下移.置顶的功能,今天小编就介绍一下我的思路. 首先,需要一张数据表: CREATE TABLE `a` ( `id` ) NOT NULL AUTO_INCREME ...
- 让我们一起写出更有效的CSharp代码吧,少年们!
周末空闲,选读了一下一本很不错的C#语言使用的书,特此记载下便于对项目代码进行重构和优化时查看. Standing On Shoulders of Giants,附上思维导图,其中标记的颜色越深表示在 ...
- 用C#实现对MSSqlServer数据库的增删改查---DAL层
说明:本人完成的工作是对传感器--超声波物位计进行硬件集成,上位机通过串口接收传感器数据并将其存到数据库中:在DAL层实现对数据库的增删改查,其中包含两个数据表分别是WaterLevelSet表和Wa ...
- [置顶]
【机器学习PAI实践十一】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码
背景 最近互联网上出现一个热词就是"freestyle",源于一个比拼rap的综艺节目.在节目中需要大量考验选手的freestyle能力,freestyle指的是rapper即兴的 ...
随机推荐
- Linux开发工具之gdb(上)
三.gdb调试(上) 01.gdb:gdb是GNU debugger的缩写,是编程调试工作. 功能: 启动程序,可以按照用户自定义的要求随心所欲的运行程序: 可让被调试的程序在用户所指定的调试 ...
- Nginx反向代理,负载均衡配置
主配置文件:nginx.conf # For more information on configuration, see: # * Official English Documentation: h ...
- Centos6.3 配置yum 163源
1. 下载repo文件 下载地址:http://mirrors.163.com/.help/CentOS6-Base-163.repo 2. 备份并替换系统的repo文件[root@localh ...
- B/S系统间跨域单点登录设计思路
基于B/S系统间单点登录 此处说的单点登录的概念,即不同系统公用一个登录界面.一处系统通过登录验证,在接入的各系统均为登录状态.一般有两种情景: 1) 一级域名相同 例如:tieba.baidu.c ...
- 关于iframe调用父页面元素操作
在iframe子页面获取父页面元素 代码如下: //在iframe子页面获取父页面元素 $.('#objld', parent.document); //在父页面获取iframe子页面的元素 $(&q ...
- Web字体工具整理,网页图标字体以及使用方法整理
常用网站app logo 下载: http://www.iconfont.cn/collections/show/268?spm=a313x.7781069.0.0.nDYawz 1.Web字体免费生 ...
- javascript判断浏览器是否是隐私模式
判断浏览器是否是隐私模式,隐私模式下有写api不可用 1. try { localStorage['test'] = 'test'; //localStorage和sessionStorage都存在, ...
- jQuery键盘控制方法,以及键值(keycode)对照表
键盘控制应用范围非常广泛,比如快捷键控制页面的滚动:在填写表单时候,限制输入内容:或者是屏蔽复制.粘贴.退后等功能.这里说说用jQuery怎么来实现.个人觉得jQuery比原生态的JS好用,代码简单清 ...
- Java基础学习第一天
================每日必读==================== 写代码: 1.明确需求.我需要实现什么需求? 2.分析思路.我需要怎么实现需求? 3.确定步骤.我的每一部分思路需要使 ...
- JLRoutes--处理复杂的URL schemes-备
关键字:URL,URL schemes,Parse 代码类库:网络(Networking) GitHub链接:https://github.com/joeldev/JLRoutes JLRout ...