You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
7.9 KiB
201 lines
7.9 KiB
using System.Data;
|
|
using System.Data.SqlClient;
|
|
|
|
namespace WinFormsApp1;
|
|
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
//创建输出路径
|
|
var strPath = Environment.CurrentDirectory;
|
|
var outPath = strPath + "\\cs_file";
|
|
Directory.CreateDirectory(outPath);
|
|
|
|
//不创建的属性名
|
|
string[] ignore =
|
|
{
|
|
"ConcurrencyStamp", "CreationTime", "CreatorId", "LastModificationTime", "LastModifierId", "TenantId", "Id"
|
|
};
|
|
|
|
//数据库链接字符串
|
|
var constr = txt_constr.Text.Trim();
|
|
//查询库下所有的表
|
|
var dataTable = GetDataTable("select * from sys.tables", constr);
|
|
|
|
foreach (DataRow row in dataTable.Rows)
|
|
{
|
|
//要生成的表名
|
|
var tbname = row["name"].ToString();
|
|
//表下所有属性值遍历
|
|
var dt = GetDbTableInfo(tbname, constr);
|
|
|
|
var firstName = tbname.Substring(0, txt_removeTbFirstName.Text.Length);
|
|
if (firstName == txt_removeTbFirstName.Text) tbname = tbname.Substring(txt_removeTbFirstName.Text.Length);
|
|
var list = new List<string>();
|
|
|
|
//生成实体的字符串
|
|
var context = "";
|
|
context += "using System;\r\n";
|
|
context += "using System.Collections.Generic;\r\n";
|
|
context += "using Volo.Abp.Domain.Entities.Auditing;\r\n";
|
|
context += $"public class {tbname} : AuditedAggregateRoot<Guid>\r\n";
|
|
context += "{\r\n";
|
|
|
|
for (var i = 0; i < dt.Rows.Count; i++)
|
|
{
|
|
var Name = dt.Rows[i]["Name"].ToString();
|
|
if (ignore.Any(p => p.ToLower() == Name.ToLower())) continue;
|
|
|
|
var Type = dt.Rows[i]["Type"].ToString();
|
|
var IsNullable = dt.Rows[i]["IsNullable"].ToString();
|
|
var IsIdentity = dt.Rows[i]["IsIdentity"].ToString();
|
|
var Description = dt.Rows[i]["Description"].ToString();
|
|
var IsKey = dt.Rows[i]["IsKey"].ToString();
|
|
var type = DbTypeStr_To_CsharpType(Type);
|
|
|
|
var typeStr = type.Name == "String" ? "string" : type.Name;
|
|
|
|
context += $@"public {typeStr} {Name} {{ get; set; }}" + "\r\n";
|
|
}
|
|
|
|
context += "}\r\n";
|
|
|
|
//创建文件夹
|
|
if (tbname.IndexOf("Detail") == -1) Directory.CreateDirectory(outPath + "\\" + tbname);
|
|
|
|
//创建文件
|
|
var fileName = outPath + "\\" + tbname + ".cs";
|
|
WriteDataFile(fileName, context);
|
|
}
|
|
}
|
|
|
|
private void WriteDataFile(string filepath, string text)
|
|
{
|
|
// Note: You should handle potential IO errors in a try/catch block
|
|
using (var fs = File.Create(filepath))
|
|
{
|
|
using (var sw = new StreamWriter(fs))
|
|
{
|
|
sw.WriteLine(text);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据参数,获取数据表信息
|
|
/// </summary>
|
|
/// <param name="tabname"></param>
|
|
public DataTable GetDbTableInfo(string tabname, string constr)
|
|
{
|
|
var str = string.Format(@"select
|
|
sys.columns.column_id as [ColumnId],
|
|
sys.columns.name as [Name],
|
|
sys.types.name as [Type],
|
|
sys.columns.is_nullable [IsNullable],
|
|
[IsIdentity]=CONVERT(BIT, (select count(*) from sys.identity_columns where sys.identity_columns.object_id = sys.columns.object_id and sys.columns.column_id = sys.identity_columns.column_id)),
|
|
(select value from sys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id and sys.extended_properties.minor_id = sys.columns.column_id and name='MS_Description') as [Description],
|
|
[IsKey] =CONVERT(bit,(case when sys.columns.name in (select b.column_name
|
|
from information_schema.table_constraints a
|
|
inner join information_schema.constraint_column_usage b
|
|
on a.constraint_name = b.constraint_name
|
|
where a.constraint_type = 'PRIMARY KEY' and a.table_name = '{0}') then 1 else 0 end))
|
|
from sys.columns, sys.views, sys.types where sys.columns.object_id = sys.views.object_id and sys.columns.system_type_id=sys.types.system_type_id and sys.views.name='{0}' and sys.types.name !='sysname'
|
|
union
|
|
select
|
|
sys.columns.column_id as [ColumnId],
|
|
sys.columns.name as [Name],
|
|
sys.types.name as [Type],
|
|
sys.columns.is_nullable [IsNullable],
|
|
[IsIdentity]=CONVERT(BIT, (select count(*) from sys.identity_columns where sys.identity_columns.object_id = sys.columns.object_id and sys.columns.column_id = sys.identity_columns.column_id)),
|
|
(select value from sys.extended_properties where sys.extended_properties.major_id = sys.columns.object_id and sys.extended_properties.minor_id = sys.columns.column_id and name='MS_Description') as [Description],
|
|
[IsKey] =CONVERT(bit,(case when sys.columns.name in (select b.column_name
|
|
from information_schema.table_constraints a
|
|
inner join information_schema.constraint_column_usage b
|
|
on a.constraint_name = b.constraint_name
|
|
where a.constraint_type = 'PRIMARY KEY' and a.table_name = '{0}') then 1 else 0 end))
|
|
from sys.columns, sys.tables, sys.types where sys.columns.object_id = sys.tables.object_id and sys.columns.system_type_id=sys.types.system_type_id and sys.tables.name='{0}' and sys.types.name !='sysname'
|
|
order by sys.columns.column_id asc", tabname);
|
|
|
|
var dt = GetDataTable(str, constr);
|
|
return dt;
|
|
}
|
|
|
|
public DataTable GetDataTable(string sql, string constr)
|
|
{
|
|
var dt = new DataTable();
|
|
|
|
using (var connection = new SqlConnection(constr))
|
|
{
|
|
//定义执行SQL语句,可以为select查询,也可以为存储过程,我们要的只是返回的结果集.
|
|
|
|
//强大的SqlDataAdapter
|
|
var adapter = new SqlDataAdapter(sql, connection);
|
|
|
|
var ds = new DataSet();
|
|
//Fill 方法会执行一系列操作 connection.open command.reader 等等
|
|
//反正到最后就把 sql语句执行一遍,然后把结果集插入到 ds 里.
|
|
adapter.Fill(ds);
|
|
|
|
dt = ds.Tables[0];
|
|
}
|
|
|
|
return dt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 类型转换枚举
|
|
/// </summary>
|
|
protected Dictionary<string, Type> DbTypeDic { get; } = new()
|
|
{
|
|
{ "int", typeof(int) },
|
|
{ "text", typeof(string) },
|
|
{ "bigint", typeof(long) },
|
|
{ "binary", typeof(byte[]) },
|
|
{ "bit", typeof(bool) },
|
|
{ "char", typeof(string) },
|
|
{ "date", typeof(DateTime) },
|
|
{ "datetime", typeof(DateTime) },
|
|
{ "datetime2", typeof(DateTime) },
|
|
{ "decimal", typeof(decimal) },
|
|
{ "float", typeof(double) },
|
|
{ "image", typeof(byte[]) },
|
|
{ "money", typeof(decimal) },
|
|
{ "nchar", typeof(string) },
|
|
{ "ntext", typeof(string) },
|
|
{ "numeric", typeof(decimal) },
|
|
{ "nvarchar", typeof(string) },
|
|
{ "real", typeof(float) },
|
|
{ "smalldatetime", typeof(DateTime) },
|
|
{ "smallint", typeof(short) },
|
|
{ "smallmoney", typeof(decimal) },
|
|
{ "timestamp", typeof(DateTime) },
|
|
{ "tinyint", typeof(byte) },
|
|
{ "varbinary", typeof(byte[]) },
|
|
{ "varchar", typeof(string) },
|
|
{ "variant", typeof(object) },
|
|
{ "uniqueidentifier", typeof(Guid) }
|
|
};
|
|
|
|
/// <summary>
|
|
/// 获取字段类型
|
|
/// </summary>
|
|
/// <param name="dbTypeStr"></param>
|
|
/// <returns></returns>
|
|
public virtual Type DbTypeStr_To_CsharpType(string dbTypeStr)
|
|
{
|
|
var _dbTypeStr = dbTypeStr.ToLower();
|
|
Type type = null;
|
|
if (DbTypeDic.ContainsKey(_dbTypeStr))
|
|
type = DbTypeDic[_dbTypeStr];
|
|
else
|
|
type = typeof(string);
|
|
|
|
return type;
|
|
}
|
|
}
|