Share via


How to: Create the SqlParameterDefinition Class

The SQL Query Filter uses the SqlParameterDefinition class to get, set, serialize, or deserialize an SQL statement. Both the client and the server use this class to store, display, run, or read the SQL statement that defines an SQL Query Filter. Depending on the functionality of your filter, you may need to implement a similar class. For more information about filters, see Filters Overview.

Prerequisites

Before you compile the code example, ensure you have met the following prerequisites:

Example

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace SqlQueryParameterProvider
{
    [Serializable]
    public class SqlParameterDefinition
    {
        private static XmlSerializer xmlSerializer = null;
        private string _expression;

        // Clear the filter definition. 
        public SqlParameterDefinition()
        {
            _expression = string.Empty;
        }

        // Get or set the filter definition.
        public string Expression
        {
            get { return _expression; }
            set { _expression = value; }
        }

        // Deserialize the XML string.
        public static SqlParameterDefinition Deserialize(string xml)
        {
            if (!string.IsNullOrEmpty(xml))
            {
                if (xmlSerializer == null)
                    xmlSerializer = new XmlSerializer(typeof(SqlParameterDefinition));

                SqlParameterDefinition data;
                using (TextReader reader = new StringReader(xml))
                {
                    data = (SqlParameterDefinition)xmlSerializer.Deserialize(reader);
                    reader.Close();
                }
                return data;
            }
            return null;

        }

        // Serialize the filter definition.
        public static string Serialize(SqlParameterDefinition data)
        {
            if (data != null)
            {
                if (xmlSerializer == null)
                    xmlSerializer = new XmlSerializer(typeof(SqlParameterDefinition));

                StringBuilder xml = new StringBuilder();
                using (StringWriter writer = new StringWriter(xml))
                {
                    xmlSerializer.Serialize(writer, data);
                    writer.Close();
                }
                return xml.ToString();
            }
            return null;
        }
    }

}

See Also

Other Resources

Dashboard Filters