Recent Changes

edit

Recent Changes

One thing I noticed recently when working with Subsonic 3.0 and the T4 templates was that there was no way to detect if a stored procedure has nullable parameters. After a bit of research I found this msdn article (? .?)\s{1}as\s{1}"; RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture; MatchCollection matches = Regex.Matches(procedureText, regex, options); if (matches.Count > 0 && matches[0].Groups["Params"] != null) { Match match = matches[0]; arguments = match.Groups["Params"].ToString(); argumentArray = Regex.Split(arguments, ","); foreach (string sArgument in argumentArray) { MatchCollection paramMatches = Regex.Matches(sArgument, @"(? @\S)", options); MatchCollection nullMatches = Regex.Matches(sArgument, @"(? =\s*)", options); if (paramMatches.Count > 0 && paramMatches[0].Groups["ParamName"] != null) { bool isNullDefault = (nullMatches.Count > 0 && nullMatches[0].Groups["NullString"] != null); parameterNullibility.Add(paramMatches[0].Groups["ParamName"].ToString()); } } } return parameterNullibility; } b. Change SPPArams List GetSPParams(string spName) { var result = new List (); string[] restrictions = new string[3] { null, null, spName }; string procedureText; procedureText = GetStoredProcedureText(spName); List nullableParams = this.GetNullableParams(procedureText); using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); var sprocs = conn.GetSchema("ProcedureParameters", restrictions); using (SqlCommand cmd = new SqlCommand(spName, conn)) { cmd.CommandType = CommandType.StoredProcedure; SqlCommandBuilder.DeriveParameters(cmd); } conn.Close(); foreach (DataRow row in sprocs.Select("", "ORDINALPOSITION")) { SPParam p = new SPParam(); p.SysType = GetSysType(row["DATATYPE"].ToString()); p.DbType = GetDbType(row["DATATYPE"].ToString()).ToString(); p.Name = row["PARAMETERNAME"].ToString().Replace("@", ""); p.ParameterMode = GetParamDirection(row["PARAMETERMODE"].ToString()).ToString(); p.CleanName = CleanUp(p.Name); if (nullableParams.Contains(row["PARAMETERNAME"].ToString())) p.Nullable = true; result.Add(p); } } return result; } 2. Inside Setting.ttinclude a. Change SSParam class as follows public class SPParam{ public string Name; public string CleanName; public string SysType; public string DbType; public string ParameterMode; public bool Nullable; } b. Change ArgList property as follows. public string ArgList{ get{ StringBuilder sb=new StringBuilder(); int loopCount=1; foreach(var par in Parameters){ if (par.Nullable) { if ( par.SysType.ToString() != "string" && par.SysType.ToString() != "byte[]") { sb.AppendFormat("{0}{1} {2}", par.SysType, "?", par.CleanName); } else { sb.AppendFormat("{0} {1}", par.SysType, par.CleanName); } } else { sb.AppendFormat("{0} {1}", par.SysType, par.CleanName); } if(loopCount