Exemplo n.º 1
0
        /// <summary>
        /// Use ObjectQuery to get SqlConnection and SqlCommand.
        /// </summary>
        public static void GetSqlCommand <TEntity>(IDbContextBase context, IQueryable query, ref SqlConnection connection, ref SqlCommand command) where TEntity : class
        {
            var queryobject = GetObjectQuery <TEntity>(context, query);

            if (queryobject == null)
            {
                throw new System.ArgumentException("Paramter cannot be null", "queryobject");
            }

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

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

                // Add all the paramters used in query.
                foreach (ObjectParameter parameter in queryobject.Parameters)
                {
                    command.Parameters.AddWithValue(parameter.Name, parameter.Value);
                }
            }
        }
        /// <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);
                }
            }
        }