嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
c#版本SQLServer数据表转实体类(entity),支持Sql server、Mysql、Oracle、Sqlite、PostgreSql
private string GetEntityCode(TreeNode node)
{
var type = (DataBaseType)node.Tag;
StringBuilder codeString = new StringBuilder();
switch (type)
{
case DataBaseType.SQLServer:
#region SQL Server生成实体类代码
codeString.Append($@"using SqlSugar;{(string.IsNullOrWhiteSpace(this.Using_TextBox.Text) ? "" : $"{Environment.NewLine}{this.Using_TextBox.Text.Trim()}")}
namespace {this.Namespace_TextBox.Text.Trim()}
{{
/// <summary>
/// {node.Name}
/// </summary>{(string.IsNullOrWhiteSpace(this.CusAttr_TextBox.Text) ? "" : $"{Environment.NewLine} {this.CusAttr_TextBox.Text.Trim()}")}
public class {(this.TableCapsCount_NumericUpDown.Value > 0 ? node.Text.SetLengthToUpperByStart((int)this.TableCapsCount_NumericUpDown.Value) : node.Text)}{(string.IsNullOrWhiteSpace(this.BaseClass_TextBox.Text) ? "" : $" : {this.BaseClass_TextBox.Text.Trim()}")}
{{");
var tableInfo = SQLServerHelper.QueryTableInfo(node.Parent.Name, $"select * from {node.Text} where 1=2");
DataTable colsInfos = SQLServerHelper.QueryDataTable(node.Parent.Name, "SELECT OBJNAME,VALUE FROM ::FN_LISTEXTENDEDPROPERTY (NULL, 'USER', 'DBO', 'TABLE', '" node.Text "', 'COLUMN', DEFAULT)", null);
var getString = this.GetCus_TextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(getString))
{
getString = "return this._-colName-;";
}
else
{
getString = getString.Replace("属性", "-colName-");
}
var setString = this.SetCus_TextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(setString))
{
setString = "this._-colName- = -value-;";
}
else
{
setString = setString.Replace("属性", "-colName-");
}
foreach (DataRow dr in tableInfo.Rows)
{
var zhuShi = string.Empty;//列名注释
foreach (DataRow uu in colsInfos.Rows)
{
if (uu["OBJNAME"].ToString().ToUpper() == dr["ColumnName"].ToString().ToUpper())
zhuShi = uu["VALUE"].ToString();
}
if ((bool)dr["IsKey"] && !(bool)dr["IsIdentity"])
{
if (this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else if (this.SqlSugarPK_CheckBox.Checked && !this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
}
else if ((bool)dr["IsKey"] && (bool)dr["IsIdentity"])
{
if (this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else if (this.SqlSugarPK_CheckBox.Checked && !this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else if (!this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsIdentity = true)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
}
else if (!(bool)dr["IsKey"] && (bool)dr["IsIdentity"])
{
if (this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsIdentity = true)]
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {getString} }} set {{ {setString} }} }}
");
}
Type ttttt = this.GetTypeByString(dr["DataType"].ToString());
if (ttttt.IsValueType && dr["AllowDBNull"].ToString() == "True")
{
codeString.Replace("-dbType-", dr["DataType"].ToString() "?"); //替换数据类型
if (this.PropDefault_CheckBox.Checked)
{
codeString.Replace("-value-", $"value ?? default({dr["DataType"].ToString()})");
}
else
{
codeString.Replace("-value-", "value");
}
}
else
{
if (dr["DataType"].ToString() == "System.String")
{
codeString.Replace("-dbType-", dr["DataType"].ToString()); //替换数据类型
if (this.PropTrim_CheckBox.Checked)
{
codeString.Replace("-value-", "value?.Trim()");
}
else
{
codeString.Replace("-value-", "value");
}
}
else
{
codeString.Replace("-dbType-", dr["DataType"].ToString()); //替换数据类型
codeString.Replace("-value-", "value");
}
}
codeString.Replace("-colName-", this.PropCapsCount_NumericUpDown.Value > 0 ? dr["ColumnName"].ToString().SetLengthToUpperByStart((int)this.PropCapsCount_NumericUpDown.Value) : dr["ColumnName"].ToString()); //替换列名(属性名)
codeString.Replace("-zhuShi-", zhuShi);
}
codeString.Append(@" }
}");
#endregion
break;
case DataBaseType.MySQL:
var database = node.Parent.Name.Substring(node.Parent.Name.IndexOf("Database=") 9, node.Parent.Name.IndexOf(";port=") - node.Parent.Name.IndexOf("Database=") - 9);
codeString.Append($@"using SqlSugar;{(string.IsNullOrWhiteSpace(this.Using_TextBox.Text) ? "" : $"{Environment.NewLine}{this.Using_TextBox.Text.Trim()}")}
namespace {this.Namespace_TextBox.Text.Trim()}
{{
/// <summary>
/// {node.Name}
/// </summary>{(string.IsNullOrWhiteSpace(this.CusAttr_TextBox.Text) ? "" : $"{Environment.NewLine} {this.CusAttr_TextBox.Text.Trim()}")}
public class {(this.TableCapsCount_NumericUpDown.Value > 0 ? node.Text.SetLengthToUpperByStart((int)this.TableCapsCount_NumericUpDown.Value) : node.Text)}{(string.IsNullOrWhiteSpace(this.BaseClass_TextBox.Text) ? "" : $" : {this.BaseClass_TextBox.Text.Trim()}")}
{{");
var mysql_tableInfo = MySQLHelper.QueryTableInfo(node.Parent.Name, $"select * from {node.Text} where 1=2");
DataTable mysql_colsInfos = MySQLHelper.QueryDataTable(node.Parent.Name, $"select COLUMN_NAME as OBJNAME,column_comment as VALUE from INFORMATION_SCHEMA.Columns where table_name='{node.Text}' and table_schema='{database}'", null);
var mysql_getString = this.GetCus_TextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(mysql_getString))
{
mysql_getString = "return this._-colName-;";
}
else
{
mysql_getString = mysql_getString.Replace("属性", "-colName-");
}
var mysql_setString = this.SetCus_TextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(mysql_setString))
{
mysql_setString = "this._-colName- = -value-;";
}
else
{
mysql_setString = mysql_setString.Replace("属性", "-colName-");
}
foreach (DataRow dr in mysql_tableInfo.Rows)
{
var zhuShi = string.Empty;//列名注释
foreach (DataRow uu in mysql_colsInfos.Rows)
{
if (uu["OBJNAME"].ToString().ToUpper() == dr["ColumnName"].ToString().ToUpper())
zhuShi = uu["VALUE"].ToString();
}
if ((bool)dr["IsKey"] && !(bool)dr["IsAutoIncrement"])
{
if (this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = false)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else if (this.SqlSugarPK_CheckBox.Checked && !this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
}
else if ((bool)dr["IsKey"] && (bool)dr["IsAutoIncrement"])
{
if (this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else if (this.SqlSugarPK_CheckBox.Checked && !this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsPrimaryKey = true)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else if (!this.SqlSugarPK_CheckBox.Checked && this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsIdentity = true)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
}
else if (!(bool)dr["IsKey"] && (bool)dr["IsAutoIncrement"])
{
if (this.SqlSugarBZL_CheckBox.Checked)
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
[SugarColumn(IsIdentity = true)]
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
}
else
{
codeString.Append($@"
private -dbType- _-colName-;
/// <summary>
/// -zhuShi-
/// </summary>
public -dbType- -colName- {{ get {{ {mysql_getString} }} set {{ {mysql_setString} }} }}
");
}
Type ttttt = this.GetTypeByString(dr["DataType"].ToString());
if (ttttt.IsValueType && dr["AllowDBNull"].ToString() == "True")
{
codeString.Replace("-dbType-", dr["DataType"].ToString() "?"); //替换数据类型
if (this.PropDefault_CheckBox.Checked)
{
codeString.Replace("-value-", $"value ?? default({dr["DataType"].ToString()})");
}
else
{
codeString.Replace("-value-", "value");
}
}
else
{
if (dr["DataType"].ToString() == "System.String")
{
codeString.Replace("-dbType-", dr["DataType"].ToString()); //替换数据类型
if (this.PropTrim_CheckBox.Checked)
{
codeString.Replace("-value-", "value?.Trim()");
}
else
{
codeString.Replace("-value-", "value");
}
}
else
{
codeString.Replace("-dbType-", dr["DataType"].ToString()); //替换数据类型
codeString.Replace("-value-", "value");
}
}
codeString.Replace("-colName-", this.PropCapsCount_NumericUpDown.Value > 0 ? dr["ColumnName"].ToString().SetLengthToUpperByStart((int)this.PropCapsCount_NumericUpDown.Value) : dr["ColumnName"].ToString()); //替换列名(属性名)
codeString.Replace("-zhuShi-", zhuShi);
}
codeString.Append(@" }
}");
break;
case DataBaseType.Oracler:
break;
case DataBaseType.SQLite:
break;
case DataBaseType.PostgreSQL:
break;
default:
break;
}
return codeString.ToString();
}