Ejemplo n.º 1
0
        private static List<DatabaseUtility.Schema.Parameter> GetParameters(Microsoft.SqlServer.Management.Smo.StoredProcedureParameterCollection storedProcedureParameters)
        {
            var parameters = new List<DatabaseUtility.Schema.Parameter>();
            foreach (Microsoft.SqlServer.Management.Smo.StoredProcedureParameter parameter in storedProcedureParameters)
            {
                var paramSchema = new DatabaseUtility.Schema.Parameter();
                paramSchema.Name = parameter.Name;
                paramSchema.Nullable = !(String.IsNullOrWhiteSpace(parameter.DefaultValue));
                paramSchema.Type = DatabaseTypeMapping.SmoMapping[parameter.DataType.SqlDataType];
                paramSchema.Size = parameter.DataType.MaximumLength;
                paramSchema.Direction = parameter.IsOutputParameter
                                            ? DatabaseUtility.Schema.ParameterDirection.Output
                                            : DatabaseUtility.Schema.ParameterDirection.Input;

                parameters.Add(paramSchema);
            }
            return parameters;
        }
        private void Materialize(DatabaseSchema result, System.Data.SqlClient.SqlDataReader reader)
        {
            var sprocName = reader["StoredProcedureName"].ToString();
            var fieldName = reader["Name"].ToString();
            var isParameterStr = reader["IsParameter"].ToString();
            var resultSetName = reader["ResultSetName"].ToString();
            var direction = reader["ParameterDirection"].ToString();
            var dbType = reader["DatabaseType"].ToString();
            var ordinal = reader["Ordinal"].ToString();
            var nullable = reader["Nullable"].ToString();
            var size = reader["SIZE"].ToString();

            var sproc = (from sp in result.StoredProcedures where sp.Name == sprocName  select sp).SingleOrDefault();
            if (sproc == null)
            {
                sproc = new StoredProcedure() {Name = sprocName};
                result.StoredProcedures.Add(sproc);
            }

            Boolean isParameter = true;
            if(Boolean.TryParse(isParameterStr,out isParameter ) && isParameter)
            {
                var param = new DatabaseUtility.Schema.Parameter();
                param.Name = fieldName;
                param.Direction =
                    (DatabaseUtility.Schema.ParameterDirection)
                    Enum.Parse(typeof (DatabaseUtility.Schema.ParameterDirection), direction);
                param.Type =
                    (DatabaseUtility.Schema.DatabaseType)
                    Enum.Parse(typeof (DatabaseUtility.Schema.DatabaseType), dbType);
                param.Ordinal = Convert.ToInt32(ordinal);
                param.Nullable = Convert.ToBoolean(nullable);
                param.Size = Convert.ToInt32(size);

                sproc.Parameters.Add(param);
            }
            else if (Boolean.TryParse(isParameterStr, out isParameter) && !isParameter)
            {
                var resultSet = (from rs in sproc.ResultSets
                                 where rs.Name == resultSetName
                                 select rs).SingleOrDefault();
                if (resultSet == null)
                {
                    resultSet = new ResultSet() { Name = resultSetName };
                    sproc.ResultSets.Add(resultSet);
                }

                var resultSetColumn = new DatabaseUtility.Schema.DatabaseField();
                resultSetColumn.Name = fieldName;
                resultSetColumn.Type =
                    (DatabaseUtility.Schema.DatabaseType)
                    Enum.Parse(typeof(DatabaseUtility.Schema.DatabaseType), dbType);
                resultSetColumn.Ordinal = Convert.ToInt32(ordinal);
                resultSetColumn.Nullable = Convert.ToBoolean(nullable);
                resultSetColumn.Size = Convert.ToInt32(size);

                resultSet.Columns.Add(resultSetColumn);
            }
        }