예제 #1
0
        public NewMessageNotifier(DbContext context, IQueryable query)
        {
            this.iquery = query;

            // Get the ObjectQuery directly or convert the DbQuery to ObjectQuery.
            oquery = QueryExtension.GetObjectQuery <T>(context, iquery);

            QueryExtension.GetSqlCommand(oquery, ref connection, ref command);

            RegisterSqlDependency();
        }
        /// <summary>
        /// Use ObjectQuery to get SqlConnection and SqlCommand.
        /// </summary>
        public static void GetSqlCommand(ObjectQuery query, ref SqlConnection connection, ref SqlCommand command)
        {
            if (query == null)
            {
                throw new System.ArgumentException("Paramter cannot be null", "query");
            }

            if (connection == null)
            {
                connection = new SqlConnection(QueryExtension.GetConnectionString(query));
            }

            if (command == null)
            {
                command = new SqlCommand(QueryExtension.GetSqlString(query), connection);

                // Add all the paramters used in query.
                foreach (ObjectParameter parameter in query.Parameters)
                {
                    command.Parameters.AddWithValue(parameter.Name, parameter.Value);
                }
            }
        }