public void GenerateClass(string sTemplate, DBTableInfo p_Table, string p_OutputDir) { Template template = this.engine.GetTemplate(sTemplate, "GB2312"); string className = p_Table.GetClassName(); DBFieldInfo[] fields = p_Table.GetFields(); DBRelatedTableInfo[] dbRelatedTableInfo = p_Table.GetDbRelatedTableInfo(); for (int i = 0; i < fields.Length; i++) { fields[i].EnableValidationAttributes = this._EnableValidationAttributes; } Debug.WriteLine(string.Concat(new object[] { "Table: ", p_Table, " -> ", className })); VelocityContext velocityContext = new VelocityContext(); velocityContext.Put("namespace", this._NameSpace); velocityContext.Put("developer", Environment.UserName); velocityContext.Put("Partial", this._MakePartial ? "partial" : ""); velocityContext.Put("PropChange", this._PropChange); velocityContext.Put("table", p_Table); velocityContext.Put("fields", fields); velocityContext.Put("related", dbRelatedTableInfo); velocityContext.Put("date", DateTime.Now.ToString("yyyy-MM-dd")); string fileName = this.GetFileName(sTemplate, velocityContext); if (this.CanWriteThisFile(p_OutputDir, className, fileName)) { StreamWriter streamWriter = new StreamWriter(p_OutputDir + fileName, false, Encoding.UTF8); try { StringWriter stringWriter = new StringWriter(); template.Merge(velocityContext, stringWriter); streamWriter.WriteLine(stringWriter.GetStringBuilder().ToString()); } finally { streamWriter.Close(); } } }
public static DBTableInfo[] GetTables(BackgroundWorker bw) { DbProviderFactory instance = SqlClientFactory.Instance; IDbConnection dbConnection = null; dbConnection = instance.CreateConnection(); IDbCommand dbCommand = null; IDataReader dataReader = null; List <DBTableInfo> list = new List <DBTableInfo>(); if (bw != null) { bw.ReportProgress(0, "Connecting ..."); } dbConnection.ConnectionString = ConfigOut.ConnectString; dbConnection.Open(); try { dbCommand = dbConnection.CreateCommand(); dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES;"; dataReader = dbCommand.ExecuteReader(); while (dataReader.Read()) { string @string = dataReader.GetString(0); if ([email protected]("dtproperties")) { if ([email protected]("sys")) { DBTableInfo dbTableInfo = new DBTableInfo(@string); dbTableInfo.GetFields(); list.Add(dbTableInfo); } } } dataReader.Close(); if (bw != null) { bw.ReportProgress(50, "Tables collected"); } foreach (DBTableInfo current in list) { current.CollectFields(dbConnection); } if (bw != null) { bw.ReportProgress(100, "Fields collected"); } } finally { if (dataReader != null) { dataReader.Close(); } if (dbCommand != null) { dbCommand.Dispose(); } dbConnection.Close(); } return(list.ToArray()); }