public static object ExecuteScalar(AdomdClient.AdomdConnection connection, string commandText)
        {
            #region Argument exceptions

            if (commandText == null)
            {
                throw new ArgumentNullException("commandText");
            }
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            #endregion

            using (var command = connection.CreateCommand())
            {
                command.CommandTimeout = DefaultTimeout;
                command.CommandType    = CommandType.Text;
                command.CommandText    = commandText;

                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return(reader.GetValue(0));
                    }
                }

                return(null);
            }
        }
        public static void ExecutePrepare(string connectionString, string commandText)
        {
            #region Argument exceptions

            if (commandText == null)
            {
                throw new ArgumentNullException("commandText");
            }
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }

            #endregion

            using (var connection = new AdomdClient.AdomdConnection(connectionString))
            {
                connection.Open();

                using (var command = connection.CreateCommand())
                {
                    command.CommandTimeout = DefaultTimeout;
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = commandText;
                    command.Prepare();
                }
            }
        }
Пример #3
0
        /*
         * Return results from query
         */
        public AdomdDataReader GetQueryResult(string sQuery)
        {
            try
            {
                string sConnString = "Data Source=" + sServer + "; Initial Catalog=" + sCatalog;
                Microsoft.AnalysisServices.AdomdClient.AdomdConnection objConn = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection(sConnString);
                objConn.Open();
                Microsoft.AnalysisServices.AdomdClient.AdomdCommand objCmd = objConn.CreateCommand();
                objCmd.CommandText = sQuery;

                //Microsoft.AnalysisServices.AdomdClient.AdomdDataReader objReader = objCmd.ExecuteReader();
                //Microsoft.AnalysisServices.AdomdClient.AdomdDataAdapter objDataAdaptor = new Microsoft.AnalysisServices.AdomdClient.AdomdDataAdapter(objCmd);

                Microsoft.AnalysisServices.AdomdClient.AdomdDataReader objDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

                /*
                 * try
                 * {
                 *  for (int i = 0; i < objDataReader.FieldCount; i++)
                 *  {
                 *      Console.Write(objDataReader.GetName(i) + "\t");
                 *  }
                 *  Console.WriteLine();
                 *  while (objDataReader.Read())
                 *  {
                 *      for (int i = 0; i < objDataReader.FieldCount; i++)
                 *      {
                 *          object value = objDataReader.GetValue(i);
                 *          string strValue = (value == null) ?
                 *          string.Empty : value.ToString();
                 *          Console.Write(strValue + "\t");
                 *      }
                 *      Console.WriteLine();
                 *  }
                 * }
                 * finally
                 * {
                 *  objDataReader.Close();
                 * }
                 */

                return(objDataReader);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            return(null);
        }
Пример #4
0
        /*
         * Return results from query
         */
        public AdomdDataReader GetQueryResult(string sQuery)
        {
            try
            {
                string sConnString = "Data Source=" + sServer + "; Initial Catalog=" + sCatalog;
                Microsoft.AnalysisServices.AdomdClient.AdomdConnection objConn = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection(sConnString);
                objConn.Open();
                Microsoft.AnalysisServices.AdomdClient.AdomdCommand objCmd = objConn.CreateCommand();
                objCmd.CommandText = sQuery;

                //Microsoft.AnalysisServices.AdomdClient.AdomdDataReader objReader = objCmd.ExecuteReader();
                //Microsoft.AnalysisServices.AdomdClient.AdomdDataAdapter objDataAdaptor = new Microsoft.AnalysisServices.AdomdClient.AdomdDataAdapter(objCmd);

                Microsoft.AnalysisServices.AdomdClient.AdomdDataReader objDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

                /*
                try
                {
                    for (int i = 0; i < objDataReader.FieldCount; i++)
                    {
                        Console.Write(objDataReader.GetName(i) + "\t");
                    }
                    Console.WriteLine();
                    while (objDataReader.Read())
                    {
                        for (int i = 0; i < objDataReader.FieldCount; i++)
                        {
                            object value = objDataReader.GetValue(i);
                            string strValue = (value == null) ?
                            string.Empty : value.ToString();
                            Console.Write(strValue + "\t");
                        }
                        Console.WriteLine();
                    }
                }
                finally
                {
                    objDataReader.Close();
                }
                */

                return objDataReader;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            return null;
        }
        public static DataTable ExecuteDataTable(AdomdClient.AdomdConnection connection, CancellationToken cancellationToken, string commandText, Guid activityID = default(Guid), int rowsLimit = Int32.MaxValue, Action beforeStart = null, Action beforeWait = null)
        {
            #region Argument exceptions

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }
            if (commandText == null)
            {
                throw new ArgumentNullException("commandText");
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException("cancellationToken");
            }

            #endregion

            var table = new DataTable();

            using (var command = connection.CreateCommand())
            {
                command.CommandTimeout = DefaultTimeout;
                command.CommandType    = CommandType.Text;
                command.CommandText    = commandText;

                if (activityID != Guid.Empty)
                {
                    command.ActivityID = activityID;
                }

                beforeStart?.Invoke();

                using (var task = Task.Factory.StartNew(() =>
                {
                    if (rowsLimit == 0)
                    {
                        command.ExecuteNonQuery();
                    }
                    else
                    {
                        CellsetToDataTable(command.ExecuteCellSet(), table, rowsLimit);
                    }

                    return(table);

                    //using (var adapter = new AdomdClient.AdomdDataAdapter(command))
                    //    adapter.Fill(table);
                    //return table;
                },
                                                        cancellationToken, TaskCreationOptions.LongRunning | TaskCreationOptions.AttachedToParent, TaskScheduler.Default))
                {
                    try
                    {
                        beforeWait?.Invoke();

                        task.Wait(cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        command.Cancel();

                        while (!task.IsCompleted)
                        {
                            Thread.Sleep(50);
                        }

                        throw;
                    }
                    finally
                    {
                        table = task.Result;
                    }
                }
            }

            return(table);
        }