public int Generate(string nameSpace, string sa, string password) { string SQL = @" USE [{0}] SELECT name, OBJECT_DEFINITION(OBJECT_ID) AS sp FROM sys.procedures WHERE is_ms_shipped <> 1 ORDER BY name "; SqlCmd cmd = new SqlCmd(databaseName.Provider, string.Format(SQL, databaseName.Name)); // cmd.ChangeConnection(sa, password); DataTable dt = cmd.FillDataTable(); foreach (DataRow row in dt.Rows) { SpProc proc = new SpProc(databaseName, (string)row[SP_NAME], row[SP_DEFINITION].IsNull <string>("")); string sourceCode = proc.Proc(nameSpace, databaseName.Name, sa, password); WriteFile(proc.SpName, sourceCode, nameSpace, proc.IsSpChanged(nameSpace, databaseName.Name)); } return(dt.Rows.Count); }
public string Proc(string nameSpace, string className, string sa, string password) { string SQL = @" USE [{0}] DECLARE @objid INT SELECT @objid = object_id FROM sys.all_objects WHERE object_id = OBJECT_ID('{1}') SELECT name, 'type' = TYPE_NAME(user_type_id), max_length, precision, scale, is_output FROM sys.all_parameters WHERE object_id = @objid ORDER BY parameter_id "; SqlCmd cmd = new SqlCmd(databaseName.Provider, string.Format(SQL, databaseName.Name, spName)); // cmd.ChangeConnection(sa,password); DataTable dt = cmd.FillDataTable(); DPCollection <SpParamDpo> parameters = new DPCollection <SpParamDpo>(dt); string comment = string.Format("//Machine Generated Code by {0} at {1}", ActiveAccount.Account.UserName, DateTime.Today); string usingString = @"{0} using System; using System.Data; using Sys.Data; "; comment = string.Format(usingString, comment); string clss = @"{0} namespace {1} {{ public partial class {2} {{ {3} }} }} "; string func = @" private static object ___{0}(int __xtype{6}{1}) {{ SqlCmd cmd = new SqlCmd(""{2}..[{0}]""); {3} object __result = null; if(__xtype == 1) cmd.ExecuteNonQuery(); else if(__xtype == 2) __result = cmd.FillDataTable(); else if(__xtype == 3) __result = cmd.FillDataSet(); {4} return __result; }} public static void {0}({1}) {{ ___{0}(1{5}); }} public static DataTable dt_{0}({1}) {{ return (DataTable)___{0}(2{5}); }} public static DataSet ds_{0}({1}) {{ return (DataSet)___{0}(3{5}); }} public const string {7} = @""{8}""; "; string signuture1 = ""; string code1 = ""; string code2 = ""; string signuture2 = ""; string tab = " "; foreach (SpParamDpo param in parameters) { SpParam p = new SpParam(param); if (signuture1 != "") { signuture1 += ", "; } if (signuture2 != "") { signuture2 += ", "; } signuture1 += p.signuture1(); signuture2 += p.signuture2(); code1 += tab + p.param1(); code2 += tab + p.param2(); } string method = string.Format(func, spName, signuture1, databaseName.Name, code1, code2, signuture2 == "" ? "" : ", " + signuture2, signuture1 == "" ? "" : ", ", this.spDefVariable, escapeSpDef() ); return(string.Format(clss, comment, nameSpace, className, method)); }