Copied to clipboard

Flag this post as spam?

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


  • Mark 2 posts 22 karma points
    Sep 09, 2012 @ 13:47
    Mark
    0

    Query the database from cshtml

    Hello,

    Sorry for asking a question that has probably been answered but I have searched everywhere with no luck. All I want to do is query the database which is stored in APP_DATA as Umbraco. The table that I have added is called courses. The below code fails here:

    var db = Database.Open("Umbraco");

    I dont know how to get to the logs to find a detailed description for this.

    So, my questions are:

    1, How can I query the database?

    2, How do I grab value from a form that has been posted?

    Many thanks in advance.

    Mark

    //My code so far

    @using System.Linq
    @using WebMatrix.Data;
        
        @{
           var ermsg ="";
         
       

       try
                {
                   var db = Database.Open("Umbraco");
                    ermsg="There is no error in code";
                }
                catch
                {
                    ermsg="Error in code! ";
                }
        }

    <html> 
    <head> 
    <title>Displaying Data </title> 
    </head> 
    <body> 
    <h1>keyStages</h1> 
    <div id="grid"> 
    @ermsg

    </div> 
    </body> 
    </html>

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 10, 2012 @ 00:52
    Jeroen Breuer
    0

    Umbraco has a helper which you can use:

    protected static ISqlHelper SqlHelper
    {
          get { return umbraco.BusinessLogic.Application.SqlHelper; }
    }

    You can than do things like this in Razor (copied from one of my packages):

    //This sql get's all the newest versions of the documents which store the DAMP xml and contain the id of the current media item.
                    string sql = string.Format(@"
                    select cpd.contentNodeId as nodeId, cpd.dataNText as xmlValue, cpt.Alias as propertyAlias from cmsDocument cd
                        inner join cmsPropertyData cpd ON cd.nodeId = cpd.contentNodeId
                        inner join cmsPropertyType cpt ON cpd.propertytypeid = cpt.id
                    where cd.newest = 1
                    and cd.versionId = cpd.versionId
                    and cpd.dataNtext like '<DAMP fullMedia="""">%'
                    and cpd.dataNtext like '%{0}%'", media.Id);
    
                    List<DAMP_UpdateDocument> updateDocuments = new List<DAMP_UpdateDocument>();
    
                    using (IRecordsReader dr = SqlHelper.ExecuteReader(sql))
                    {
                        while (dr.Read())
                        {
                            try
                            {
                                //Get the documents which need to be updated.
                                updateDocuments.AddRange(DAMP_Helper.GetUpdateDocuments(dr.GetString("xmlValue"), dr.GetInt("nodeId"), dr.GetString("propertyAlias"), media.Id));
                            }
                            catch (Exception ex)
                            {
                                //If the xml can't be converted log it and go to the next row.
                                DAMP_Helper.LogException(ex);
                            }
                        }
                    }

    Jeroen

  • Mark 2 posts 22 karma points
    Sep 10, 2012 @ 09:48
    Mark
    0

    Hi Jeroen,

    That is a very detailed anwser and looks like it could work for me. I will try this later!

    Thanks for your time!

     

Please Sign in or register to post replies

Write your reply to:

Draft