Commit ba337e9e authored by 陈航's avatar 陈航

字段可为空的类型转换和生成类代码调整

parent 8353a772
......@@ -94,16 +94,30 @@ namespace Tools.UI
{
GenerateEntity();
}
private string GetCsharpTypeNameBySqlType(string nSqlTypeName)
private string GetCsharpTypeNameBySqlType(string nSqlTypeName, bool isnull)
{
switch (nSqlTypeName)
{
case "image": return "Image";
case "int": return "int";
case "datetime": return "DateTime?";
case "bit": return "bool";
case "varchar": return "string";
case "char": return "char";
case "image": return isnull ? "image?" : "image";
case "int": return isnull ? "int?" : "int";
case "bigint": return isnull ? "long?" : "long";
case "float": return isnull ? "float?" : "float";
case "decimal": return isnull ? "decimal?" : "decimal";
case "datetime": return isnull ? "DateTime?" : "DateTime";
case "bit": return isnull ? "bool?" : "bool";
case "char":
case "varchar":
case "nvarchar":
case "text":
case "ntext":
return "string";
case "uniqueidentifier":
return isnull ? "Guid?" : "Guid";
default: return "string";
}
}
......@@ -118,11 +132,11 @@ namespace Tools.UI
/// </summary>
private void GenerateEntity()
{
if (lsbTableInfo.SelectedItem==null) return;
if (lsbTableInfo.SelectedItem == null) return;
string strTableName = lsbTableInfo.SelectedItem.ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine($@"SELECT b.name, b.user_type_id, c.name AS TypeName, ISNULL(d.value, b.name) AS Description
sb.AppendLine($@"SELECT b.name, b.user_type_id, c.name AS TypeName, ISNULL(d.value, b.name) AS Description, b.is_nullable
FROM sys.objects a
INNER JOIN sys.columns b ON b.object_id = a.object_id
INNER JOIN sys.types c ON b.user_type_id = c.user_type_id
......@@ -137,46 +151,40 @@ ORDER BY b.column_id;
//生成前端列名
if (chkFrontendcolumn.Checked)
{
sb.Append($@"
Colums:
[
");
sb.AppendLine("Colums:");
sb.AppendLine("[");
foreach (DataRow row in dataTable.Rows)
{
sb.Append($@"
{{
label: '{row["Description"]}',
value: '{row["name"]}',
show_overflow_tooltip: true
}},");
sb.AppendLine(" {");
sb.AppendLine($" label: '{row["Description"]}',");
sb.AppendLine($" value: '{row["name"]}',");
sb.AppendLine($" show_overflow_tooltip: true");
sb.AppendLine(" },");
}
txtResult.Text = sb.ToString().Substring(0, sb.ToString().Length - 1) + @"
]";
}
else//生成实体类
{
sb.Append($@"
/// <summary>
/// {strTableName}
/// </summary>
public class {strTableName}
{{
");
sb.AppendLine(" /// <summary>");
sb.AppendLine($" /// {strTableName}");
sb.AppendLine(" /// </summary>");
sb.AppendLine($" public class {strTableName}");
sb.AppendLine(" {");
foreach (DataRow row in dataTable.Rows)
{
sb.Append($@"
/// <summary>
/// {row["Description"]}
/// </summary>
public {GetCsharpTypeNameBySqlType(row["TypeName"].ToString())} {row["name"]} {{ get; set; }}
");
sb.AppendLine($@" /// <summary>");
sb.AppendLine($@" /// {row["Description"]}");
sb.AppendLine($@" /// </summary>");
sb.AppendLine($" public {GetCsharpTypeNameBySqlType(row["TypeName"].ToString(), (bool)row["is_nullable"])} {row["name"]} {{ get; set; }}");
sb.AppendLine();
}
sb.Append($@"
}}
");
sb.AppendLine(" }");
txtResult.Text = sb.ToString();
}
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment