Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David F. Hill 122 posts 242 karma points
    Sep 02, 2010 @ 01:40
    David F. Hill
    0

    dbType of PropertyType

    Hello Umbraco Colleagues,

    Can anyone tell me how to get the dbType (Integer, Ntext, Nvarchar... etc.) from a PropertyType?

    I've been trying to figure out the way to do this for a long time.

    Thanks,

    David Hill

     

  • David F. Hill 122 posts 242 karma points
    Sep 02, 2010 @ 18:43
    David F. Hill
    2

    Update:

    I developed a way to retreive the dbType (Integer, Ntext, Nvarchar... etc.) from a PropertyType.

    Here is a C# code snippet:

    protected void GetDbType()
    {
      DocumentType docType = new DocumentType(1024);
      List<PropertyType> propertyTypeList = new List<PropertyType>();
      propertyTypeList = docType.PropertyTypes;

      foreach (PropertyType propertyType in propertyTypeList)
      {
        umbraco.interfaces.IDataType dt = propertyType.DataTypeDefinition.DataType;
        string defId = dt.DataTypeDefinitionId.ToString();
        string umbracoDbDSN = System.Configuration.ConfigurationManager.AppSettings["umbracoDbDSN"].ToString();
           
        SqlConnection connection = new SqlConnection(umbracoDbDSN);
        SqlCommand command = new SqlCommand("SELECT [dbType] FROM [cmsDataType] WHERE [nodeId] = " + defId, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable table = new DataTable();
        adapter.Fill(table);
           
        string dbType = table.Rows[0]["dbType"].ToString();
      }
    }

    Hope that helps someone someday!

    Cheers,

    David Hill

  • Matt Brailsford 4125 posts 22222 karma points MVP 9x c-trib
    Mar 31, 2011 @ 16:15
    Matt Brailsford
    0

    I know it was a while ago, but I recently needed to do the same. I wrote an extension method for DataTypeDefinitions which does the trick nicely:

    public static DBTypes GetDBType(this DataTypeDefinition dtd)
    {
        var param = umbraco.BusinessLogic.Application.SqlHelper.CreateParameter("@nodeId", dtd.Id);
        var dbTypeString = umbraco.BusinessLogic.Application.SqlHelper.ExecuteScalar<string>("SELECT [dbType] FROM [cmsDataType] WHERE [nodeId] = @nodeId", param);
        return (DBTypes) Enum.Parse(typeof (DBTypes), dbTypeString);
    }

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft