private void SetResult() { if (LB_SP.SelectedItem != null && CB_TYPE.SelectedItem != null) { string spName = LB_SP.SelectedItem.ToString(); string rtnType = CB_TYPE.SelectedItem.ToString(); //Void,ReturnValue,List<T> bool IsConnect = IsConnection.Checked; bool IsMethod = CK_Method.Checked; StringBuilder builder = new StringBuilder(200); if (!String.IsNullOrWhiteSpace(spName)) { List <SPInfo> spinfos = new List <SPInfo>(); using (var cmd = new SqlCommand(SPInfo.CreateSPInfoQuery(spName), main.SqlConn)) { spinfos = cmd.ExecuteList <SPInfo>(); } using (var cmd = new SqlCommand("sp_depends", main.SqlConn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.AddParameterInput("@objname", SqlDbType.NVarChar, spName, 776); this.depends = cmd.ExecuteTable(); if (this.depends != null && this.depends.Rows.Count > 0) { this.ReturnType = Convert.ToString(this.depends.Rows[0].ItemArray[0]); } } if (string.IsNullOrWhiteSpace(this.ReturnType)) { this.ReturnType = "object"; } if (IsMethod) { string NameOfSP = string.Empty; if (spName.IndexOf("_") > -1) { NameOfSP = spName.Substring(spName.IndexOf("_")).Replace("_", ""); } else { NameOfSP = spName; } if (rtnType.Equals("ReturnValue", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine($"public ReturnValue {NameOfSP}({this.ReturnType} TargetModel)"); } else if (rtnType.Equals("List<T>", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine($"public List<T> {NameOfSP}(object TargetModel)"); } else { builder.AppendLine($"public void {NameOfSP}(object TargetModel)"); } builder.AppendLine("{"); builder.AppendLine("var result = new ReturnValue();"); builder.AppendLine(""); } if (IsConnect) { builder.AppendLine("using (var SqlConn = new SqlConnection(\"\"))"); } builder.AppendLine($"using (var cmd = new SqlCommand(\"{spName}\", SqlConn))"); builder.AppendLine("{"); builder.AppendLine("cmd.CommandType = System.Data.CommandType.StoredProcedure;"); if (spinfos != null && spinfos.Count > 0) { foreach (SPInfo info in spinfos) { if (info.is_output) { if ((rtnType.Equals("ReturnValue", StringComparison.OrdinalIgnoreCase) && (info.name.Equals("@Code", StringComparison.OrdinalIgnoreCase) || info.name.Equals("@Value", StringComparison.OrdinalIgnoreCase) || info.name.Equals("@Msg", StringComparison.OrdinalIgnoreCase))) == false) { builder.AppendLine($"cmd.AddParameterOutput(\"{info.name}\", System.Data.SqlDbType.{info.SqlType.ToString()}, {info.max_length});"); } } else { builder.Append($"cmd.AddParameterInput(\"{info.name}\", System.Data.SqlDbType.{info.SqlType.ToString()}, TargetModel.{info.name.Replace("@", "")}, "); switch (info.SqlType) { case SqlDbType.NVarChar: case SqlDbType.NChar: builder.AppendLine($" {info.max_length / 2});"); break; default: builder.AppendLine($" {info.max_length});"); break; } } } } if (rtnType.Equals("ReturnValue", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine("result = cmd.ExecuteReturnValue();"); } else if (rtnType.Equals("List<T>", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine("result = cmd.ExecuteList<T>();"); } else { builder.AppendLine("cmd.ExecuteNonQuery();"); } builder.AppendLine("}"); if (IsMethod) { if (rtnType.Equals("ReturnValue", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine($"return result;"); } else if (rtnType.Equals("List<T>", StringComparison.OrdinalIgnoreCase)) { builder.AppendLine($"return result;"); } else { } builder.AppendLine("}"); } this.Query = builder.ToString(); } } }
private void SetResult() { if (LB_SP.Items != null && LB_SP.Items.Count > 0) { string Prefix = TB_Prefix.Text; bool IsEntity = CK_Entity.Checked; bool IsRoute = CK_Route.Checked; string spName = string.Empty; string tmp = string.Empty; string rtnType = TB_Return.Text; int chk = 0; List <SPInfo> spinfos = new List <SPInfo>(); StringBuilder builder = new StringBuilder(200); StringBuilder paramData = new StringBuilder(200); for (int i = 0; i < LB_SP.Items.Count; i++) { if (LB_SP.GetItemChecked(i)) { paramData.Clear(); spName = LB_SP.Items[i].ToString(); if (IsRoute) { builder.AppendLine($"[Route(\"{Prefix.Replace("_","/")}{spName.Replace("ESP_", "").Replace("_", "/")}\")]"); } builder.Append($"public JsonResult {Prefix}{spName.Replace("ESP_", "")}("); if (IsEntity) { using (var cmd = new SqlCommand("sp_depends", main.SqlConn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.AddParameterInput("@objname", SqlDbType.NVarChar, spName, 776); this.depends = cmd.ExecuteTable(); } if (this.depends != null && this.depends.Rows.Count > 0) { tmp = Convert.ToString(this.depends.Rows[0].ItemArray[0]).Replace("dbo.", ""); if (!string.IsNullOrWhiteSpace(tmp)) { builder.Append($"{tmp} {tmp.ToLower()}"); paramData.Append($"{tmp.ToLower()}"); if (string.IsNullOrWhiteSpace(rtnType)) { rtnType = tmp; } } } } else { using (var cmd = new SqlCommand(SPInfo.CreateSPInfoQuery(spName), main.SqlConn)) { spinfos = cmd.ExecuteList <SPInfo>(); } if (spinfos != null && spinfos.Count > 0) { chk = 0; foreach (var sp in spinfos.Where(x => x.is_output == false)) { if (chk > 0) { builder.Append(","); paramData.Append(","); } builder.Append($"{sp.BindType} {sp.name.Replace("@","")}"); paramData.Append($"{sp.name.Replace("@", "")}"); chk++; } } } builder.AppendLine(")"); builder.AppendLine("{"); if (string.IsNullOrWhiteSpace(rtnType)) { rtnType = "ReturnValue"; } builder.AppendTabLine(1, $"var result = new {rtnType}();"); builder.AppendLine(""); builder.AppendTab(1, $"result = this.Repository.{spName.Replace("ESP_", "").Replace("_", "")}("); builder.Append(paramData.ToString()); builder.AppendLine(");"); builder.AppendLine(""); builder.AppendTabLine(1, "return Json(result);"); builder.AppendLine("}"); } builder.AppendLine(""); } this.Query = builder.ToString(); } else { MessageBox.Show("대상이 지정되지 않았습니다."); } }