Commit ba337e9e authored by 陈航's avatar 陈航

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

parent 8353a772
...@@ -94,16 +94,30 @@ namespace Tools.UI ...@@ -94,16 +94,30 @@ namespace Tools.UI
{ {
GenerateEntity(); GenerateEntity();
} }
private string GetCsharpTypeNameBySqlType(string nSqlTypeName) private string GetCsharpTypeNameBySqlType(string nSqlTypeName, bool isnull)
{ {
switch (nSqlTypeName) switch (nSqlTypeName)
{ {
case "image": return "Image"; case "image": return isnull ? "image?" : "image";
case "int": return "int";
case "datetime": return "DateTime?"; case "int": return isnull ? "int?" : "int";
case "bit": return "bool"; case "bigint": return isnull ? "long?" : "long";
case "varchar": return "string"; case "float": return isnull ? "float?" : "float";
case "char": return "char"; 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"; default: return "string";
} }
} }
...@@ -118,11 +132,11 @@ namespace Tools.UI ...@@ -118,11 +132,11 @@ namespace Tools.UI
/// </summary> /// </summary>
private void GenerateEntity() private void GenerateEntity()
{ {
if (lsbTableInfo.SelectedItem==null) return; if (lsbTableInfo.SelectedItem == null) return;
string strTableName = lsbTableInfo.SelectedItem.ToString(); string strTableName = lsbTableInfo.SelectedItem.ToString();
StringBuilder sb = new StringBuilder(); 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 FROM sys.objects a
INNER JOIN sys.columns b ON b.object_id = a.object_id 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 INNER JOIN sys.types c ON b.user_type_id = c.user_type_id
...@@ -137,46 +151,40 @@ ORDER BY b.column_id; ...@@ -137,46 +151,40 @@ ORDER BY b.column_id;
//生成前端列名 //生成前端列名
if (chkFrontendcolumn.Checked) if (chkFrontendcolumn.Checked)
{ {
sb.Append($@" sb.AppendLine("Colums:");
Colums: sb.AppendLine("[");
[
");
foreach (DataRow row in dataTable.Rows) foreach (DataRow row in dataTable.Rows)
{ {
sb.Append($@" sb.AppendLine(" {");
{{ sb.AppendLine($" label: '{row["Description"]}',");
label: '{row["Description"]}', sb.AppendLine($" value: '{row["name"]}',");
value: '{row["name"]}', sb.AppendLine($" show_overflow_tooltip: true");
show_overflow_tooltip: true sb.AppendLine(" },");
}},");
} }
txtResult.Text = sb.ToString().Substring(0, sb.ToString().Length - 1) + @" txtResult.Text = sb.ToString().Substring(0, sb.ToString().Length - 1) + @"
]"; ]";
} }
else//生成实体类 else//生成实体类
{ {
sb.Append($@" sb.AppendLine(" /// <summary>");
/// <summary> sb.AppendLine($" /// {strTableName}");
/// {strTableName} sb.AppendLine(" /// </summary>");
/// </summary> sb.AppendLine($" public class {strTableName}");
public class {strTableName} sb.AppendLine(" {");
{{
");
foreach (DataRow row in dataTable.Rows) foreach (DataRow row in dataTable.Rows)
{ {
sb.Append($@" sb.AppendLine($@" /// <summary>");
/// <summary> sb.AppendLine($@" /// {row["Description"]}");
/// {row["Description"]} sb.AppendLine($@" /// </summary>");
/// </summary>
public {GetCsharpTypeNameBySqlType(row["TypeName"].ToString())} {row["name"]} {{ get; set; }}
");
}
sb.Append($@" sb.AppendLine($" public {GetCsharpTypeNameBySqlType(row["TypeName"].ToString(), (bool)row["is_nullable"])} {row["name"]} {{ get; set; }}");
}} sb.AppendLine();
"); }
sb.AppendLine(" }");
txtResult.Text = sb.ToString(); 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