Language: C#
T4 template to dump basic database schema in a static class...
1: <#@ template language="C#v3.5" hostspecific="True" debug="True" #> 2: <#@ output extension="cs" #> 3: <#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #> 4: <#@ assembly name="Microsoft.SqlServer.Smo" #> 5: <#@ assembly name="System.Data" #> 6: <#@ import namespace="Microsoft.SqlServer.Management.Smo" #> 7: <#@ import namespace="Microsoft.SqlServer.Management.Common" #> 8: <#@ import namespace="System.Data.SqlClient" #> 9: <#@ import namespace="System.Collections.Specialized" #> 10: <# 11: var ConnectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True;"; 12: SqlConnection cn = new SqlConnection(ConnectionString); 13: var DatabaseName = cn.Database; 14: Server server = new Server(new ServerConnection(cn)); 15: Database database = server.Databases[cn.Database]; 16: database.Refresh(); 17: #> 18: using System; 19: using System.Data; 20: namespace dbSchema{ 21: public static class Tables 22: { 23: <# 24: foreach (Table table in database.Tables) 25: { 26: #> 27: <# if (!table.IsSystemObject) { 28: #> 29: public static class <#=table.Name.Replace(" ","_") #> { 30: public static string Name = "<#=table.Name#>"; 31: public static class Columns { 32: <# 33: for (int i = 0; i < table.Columns.Count; i++) 34: { 35: #> public static Column <#=table.Columns[i].Name #>= new Column("<#=table.Columns[i].Name #>",<#=table.Columns[i].Nullable?"true":"false"#>,"<#=table.Columns[i].DataType#>",<#=table.Columns[i].DataType.ToString().Contains("char")?table.Columns[i].DataType.MaximumLength.ToString():"null" #>); 36: <# 37: } 38: #> 39: } 40: } 41: <# } 42: } 43: #> 44: } 45: 46: public class Column { 47: public Column(string columnName, bool isNullable, string dbType, int? maxlength) 48: { 49: IsNullable = isNullable; 50: SqlDbType = (SqlDbType)Enum.Parse(typeof(SqlDbType), dbType, true); 51: ColumnName = columnName; 52: MaxLength = maxlength; 53: } 54: 55: public bool IsNullable { get; set; } 56: 57: public SqlDbType SqlDbType { get; set; } 58: 59: public int? MaxLength { get; set; } 60: 61: public string ColumnName { get; set; } 62: 63: public override string ToString() 64: { 65: return ColumnName; 66: } 67: } 68: } 69: 70: <# 71: #>
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

