在代码中写sql代码中,经常会用到 字段名,表名。如果每次写字符串将会难以维护,我的习惯是定义一份表的字段名常量。
通常也会定义一份对应该表的一个数据结构,在处理数据时便于使用。
今天整了一份自动生成字段名表名常量以及根据表生成对应数据结构的工具。
思路比较简单:
第一步 获取数据库中的所有表名称
第二步 根据表名称获取表的字段名,字段类型,注释.
第三步 根据获得的表名,字段名,字段类型,注释 生成代码
接下来上代码,需要的自己研究下。
我这里的MySql 类是我自己包装的方便调用的 Sql语句的类
需要的到这里拿
MrWu.dll
MySql.Data
private List<string> GetAllTableNames(MySQL mysql, string dbName)
{
List<String> result = new List<string>();
string sql = string.Format("select table_name from information_schema.tables where table_schema='{0}'", dbName);
DataSet ds = new DataSet();
mysql.ExecuteSql(dbName, sql, null, ds);
if (BaseFun.DataSetIsNullOrEmpty(ds))
return null;
DataTable dt = ds.Tables[0];
if (BaseFun.DataTableIsNullOrEmpty(dt))
return null;
int len = dt.Rows.Count;
for (int i = 0; i < len; i++)
{
result.Add(dt.Rows[i]["table_name"].ToString());
}
return result;
}
private void GenerateCode(MySQL mysql, string dbName, string tablename, string spaceName)
{
string sql = string.Format("select column_name,data_type,column_comment from information_schema.`columns` where table_name='{0}'", tablename);
DataSet fieldDs = new DataSet();
mysql.ExecuteSql(dbName, sql, null, fieldDs);
if (BaseFun.DataSetIsNullOrEmpty(fieldDs))
return;
DataTable dt = fieldDs.Tables[0];
if (BaseFun.DataTableIsNullOrEmpty(dt))
return;
GenerateCode(tablename, dt, spaceName);
}
private void GenerateCode(string tableName, DataTable dt, string spaceName)
{
string entityPath = @"code\Entity";
string fieldNamePath = @"code\Field";
if (!Directory.Exists(entityPath))
Directory.CreateDirectory(entityPath);
if (!Directory.Exists(fieldNamePath))
Directory.CreateDirectory(fieldNamePath);
string modelFieldNameCode =
@"public static class #tableNameField
{
#ReplaceCode
}";
string modelFiledCode =
@"using System;
public static class #tableName
{
#ReplaceCode
}";
string fieldNameCode = string.Empty;
string fieldCode = string.Empty;
fieldNameCode += string.Format(" public const string table_Name = \"{0}\"", tableName) + ";\r\n";
int len = dt.Rows.Count;
for (int i = 0; i < len; i++)
{
string comment = dt.Rows[i]["column_comment"].ToString();
if (!string.IsNullOrEmpty(comment))
{
comment = string.Format(
@" /// <summary>
/// {0}
/// </summary>
", comment);
}
fieldNameCode += comment;
fieldNameCode += string.Format(
" public const string {0}=\"{0}\"", dt.Rows[i]["column_name"].ToString()
) + ";\r\n";
fieldCode += comment;
fieldCode +=
" public " + GetType(dt.Rows[i]["data_type"].ToString()) + " " + dt.Rows[i]["column_name"] + " { get; set; }\r\n";
}
modelFieldNameCode = modelFieldNameCode.Replace("#tableName", tableName).Replace("#ReplaceCode", fieldNameCode);
modelFiledCode = modelFiledCode.Replace("#tableName", tableName).Replace("#ReplaceCode", fieldCode);
if (!string.IsNullOrEmpty(spaceName))
{
modelFieldNameCode = "namespace " + spaceName + "{\r\n" + modelFieldNameCode + "\r\n}";
modelFiledCode = "namespace " + spaceName + "{\r\n" + modelFiledCode + "\r\n}";
}
File.WriteAllText(fieldNamePath + "\\" + tableName + ".cs", modelFieldNameCode);
File.WriteAllText(entityPath + "\\" + tableName + ".cs", modelFiledCode);
}
public string GetType(string typeName)
{
switch (typeName.ToLower())
{
case "datetime":
case "date":
case "timestamp":
return "DateTime";
case "bigint":
return "long";
case "smallint":
return "short";
case "int":
case "mediumint":
return "int";
case "longtext":
case "text":
case "char":
case "varchar":
case "enum":
case "mediumtext":
case "set":
case "tinytext":
return "string";
case "time":
return "TimeSpan";
case "decimal":
return "decimal";
case "binary":
case "blob":
case "longblob":
case "mediumblob":
case "tinyblob":
case "varbinary":
return "byte[]";
case "bit":
return "bool";
case "double":
case "float":
return "double";
case "tinyint":
return "sbyte";
case "geometry":
return "System.Data.Entity.Spatial.DbGeometry";
default:
return string.Empty;
}
}