Beispiel #1
0
        /// <summary>
        /// Instantiate the class with a Halide.H3DataRowConfig class variable
        /// to specify what should be loaded, and from where. You must
        /// specify either 1) a SQL Command, or 2) a Table Name, Primary Key Name
        /// and Primary Key Value. SQL command overrides all other parameters.
        /// </summary>
        /// <example>
        /// <code>
        /// using Argentini.Halide;
        /// ...
        /// H3DataRowConfig dataCon = new H3DataRowConfig();
        /// dataCon.ConnectionName = "MyConnection";
        /// dataCon.TableName = "News";
        /// dataCon.PrimarykeyName = "News_ID";
        /// dataCon.PrimaryKeyValue = "15";
        /// H3DataRow data = new H3DataRow(dataCon);
        /// </code>
        /// </example>
        /// <param name="DataCon">Halide.H3DataRowConfig object with database settings.</param>
        public H3DataRow(H3DataRowConfig DataCon)
        {
            String SqlCmd = "";
            DataPresent = false;

            ConnectionStringName = DataCon.ConnectionName;

            // If DataCon has a SQL command passed...
            if (!String.IsNullOrEmpty(DataCon.SqlCommand))
            {
                SqlCmd = DataCon.SqlCommand;
            }

            else
            {
                // If DataCon object has table name, primary key name and value passed...
                if (!String.IsNullOrEmpty(DataCon.PrimarykeyName) && !String.IsNullOrEmpty(DataCon.PrimaryKeyValue) && !String.IsNullOrEmpty(DataCon.TableName))
                {
                    SqlCmd = "SELECT TOP 1 * FROM " + DataCon.TableName + " WHERE " + DataCon.PrimarykeyName + "='" + DataCon.PrimaryKeyValue.Replace("'", "''") + "'";
                }

                else
                {
                    // If DataCon object has table name and primary key value passed...
                    if (!String.IsNullOrEmpty(DataCon.PrimaryKeyValue) && !String.IsNullOrEmpty(DataCon.TableName))
                    {
                        using (Halide.H3Reader reader = new Halide.H3Reader("SELECT TOP 1 * FROM " + DataCon.TableName, true, ConnectionStringName))
                        {
                            reader.Read();
                            String PrimaryKey = reader.GetPrimarykeyName();
                            SqlCmd = "SELECT TOP 1 * FROM " + DataCon.TableName + " WHERE " + PrimaryKey + "='" + DataCon.PrimaryKeyValue.Replace("'", "''") + "'";
                        }
                    }

                    // If all else fails, load an empty object
                    else
                    {
                        Column = new DatabaseRow();
                    }
                }
            }

            // Instantiate the data object if valid parameters have been passed
            if (!String.IsNullOrEmpty(SqlCmd))
            {
                using (Halide.H3Reader reader = new Halide.H3Reader(SqlCmd, true, DataCon.ConnectionName))
                {
                    if (reader.HasRows)
                    {
                        DataPresent = true;
                        reader.Read();
                        Column = new DatabaseRow(reader);
                    }

                    else
                    {
                        Column = new DatabaseRow();
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a string object with an RSS 2.0 (XML) feed, generated by using the passed params. 
        /// <para>SQL must return fields in the following order:</para>
        /// <para>1. Unique ID for the record, which is appended to the "redirect" param below to create a link to the content.</para>
        /// <para>2. Title for the article.</para>
        /// <para>3. Short description of the article (up to 500 characters, or will be cropped to 500).</para>
        /// <para>4. Date of the article.</para>
        /// </summary>
        /// <example>
        /// <code>
        /// using Argentini.Halide;
        /// ...
        /// string result = H3Xml.BuildRss("SELECT TOP 10 ID, headline, content, date, content FROM News;", "MyDatabase", rssDate, "Feed Title", "http://nonsequiturs.com", "This is the pocketkaos weekly feed.", "(c)opyright 2007", "http://nonsequiturs.com/rss/redirect.aspx?ID=");
        /// </code>
        /// </example>
        /// <param name="sql">SQL SELECT statement for pulling data</param>
        /// <param name="connectionStringName">Connection string name to use for SQL access.</param>
        /// <param name="rssDate">Date of the feed itself</param>
        /// <param name="title">Title of the feed</param>
        /// <param name="link">Link to the source web site (e.g. http://nonsequiturs.com).</param>
        /// <param name="description">Description of the feed (up to 500 characters)</param>
        /// <param name="copyright">Copyright text</param>
        /// <param name="redirect">Path to use for redirection (e.g. "http://nonsequiturs.com/rss/redirect.aspx?ID="). The first column returned will be appended to the end of this string for you.
        /// Passed param is pulled from SQL and appended. This is also used as a unique GUID for the item.</param>
        /// <param name="feedUrl">Fully qualified URL to the generated feed. This is only used to generate an ATOM self-referral tag within the feed itself.</param>
        /// <returns>A string containing the XML for the RSS feed.</returns>
        public static String BuildRss(String sql, String connectionStringName, String rssDate, String title, String link, String description, String copyright, String redirect, String feedUrl)
        {
            System.DateTime thedate = new System.DateTime();
            StringWriter sw = new StringWriter();
            XmlTextWriter xml = new XmlTextWriter(sw);

            thedate = Convert.ToDateTime(rssDate);

            xml.Formatting = Formatting.Indented;
            xml.Indentation = 1;
            xml.IndentChar = (char)9;

            xml.WriteStartDocument();

            #region Write document header

            xml.WriteStartElement("rss");
            xml.WriteAttributeString("version", "2.0");
            xml.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");

            #endregion

            xml.WriteStartElement("channel");

            #region Write channel information

            xml.WriteStartElement("title"); xml.WriteString(title); xml.WriteEndElement();
            xml.WriteStartElement("link"); xml.WriteString(link); xml.WriteEndElement();
            xml.WriteStartElement("description"); xml.WriteString(description); xml.WriteEndElement();
            xml.WriteStartElement("language"); xml.WriteString("en-us"); xml.WriteEndElement();
            xml.WriteStartElement("copyright"); xml.WriteString(copyright); xml.WriteEndElement();
            xml.WriteStartElement("pubDate"); xml.WriteString(H3Temporal.DateFormat(thedate, Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
            xml.WriteStartElement("lastBuildDate"); xml.WriteString(H3Temporal.DateFormat(DateTime.Now, Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
            xml.WriteStartElement("ttl"); xml.WriteString("5"); xml.WriteEndElement();
            xml.WriteStartElement("generator"); xml.WriteString("Halide"); xml.WriteEndElement();

            #endregion

            #region Read in items from database

            Halide.H3Reader reader = new Halide.H3Reader(sql, connectionStringName);

            while (reader.Read())
            {
                xml.WriteStartElement("item");

                xml.WriteStartElement("title"); xml.WriteString(H3Text.StripHtml(H3Text.HtmlRemoveSymbols(reader[1]), true, false)); xml.WriteEndElement();
                xml.WriteStartElement("link"); xml.WriteString(redirect + reader[0]); xml.WriteEndElement();

                string desc = H3Text.StripHtml(reader[2], true, false).Replace("\r", " ").Replace("\n", "");
                String suffix = "";

                if (desc.Length > 400)
                {
                    desc = H3Text.Crop(desc, 400, Halide.H3Text.CropType.Characters, " .,:?!([");
                    suffix = "...";
                }

                while (desc.IndexOf("  ") >= 0)
                {
                    desc = desc.Replace("  ", " ");
                }

                desc = desc.Replace("&#160;", " ");
                desc = desc.Replace("&nbsp;", " ");
                desc = desc.Trim();

                if (desc.LastIndexOf("&") > (desc.Length - 8))
                {
                    desc = desc.Substring(0, desc.LastIndexOf("&"));
                }

                desc = desc.Trim() + suffix;

                xml.WriteStartElement("description"); xml.WriteString(desc); xml.WriteEndElement();
                xml.WriteStartElement("pubDate"); xml.WriteString(H3Temporal.DateFormat(Convert.ToDateTime(reader.GetString(3)), Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
                xml.WriteStartElement("guid"); xml.WriteString(redirect + reader[0]); xml.WriteEndElement();

                xml.WriteEndElement();
            }

            reader.Close();

            #endregion

            #region Write out atom-compliant self-link

            xml.WriteStartElement("atom:link");
            xml.WriteAttributeString("href", feedUrl);
            xml.WriteAttributeString("rel", "self");
            xml.WriteAttributeString("type", "application/rss+xml");
            xml.WriteEndElement();

            #endregion

            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Close();

            string[] delim = { "\r\n" };
            string[] decl = sw.ToString().Split(delim, StringSplitOptions.None);
            decl[0] = decl[0].Replace("utf-16", "ISO-8859-1");

            string feed = "";

            for (int x = 0; x < decl.Length; x++)
            {
                feed += decl[x] + "\r\n";
            }

            return (feed);
        }