Beispiel #1
0
        public Resource query(string query, AdapterCommandType type)
        {
            //Console.WriteLine("Query: " + query);
            Resource datatable = new Resource();

            if (!connected || connection.State != ConnectionState.Open)
            {
                connect(host, db, user, password);
                if (connected)
                {
                    return(this.query(query, type));
                }
                else
                {
                    return(datatable);
                }
            }
            try
            {
                if (type == AdapterCommandType.Update || type == AdapterCommandType.Insert || type == AdapterCommandType.Delete)
                {
                    OdbcCommand odbcCommand = connection.CreateCommand();
                    odbcCommand.CommandText = query;
                    odbcCommand.Prepare();
                    odbcCommand.ExecuteNonQuery();

                    return(datatable);
                }
                else if (type == AdapterCommandType.Select)
                {
                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = query;

                    adapter.SelectCommand = command;
                    adapter.Fill(datatable);
                    return(datatable);
                }
            }
            catch (InvalidOperationException e) //Database is disconnected
            {
                Console.WriteLine("Invalid Operation Exception at Query: " + query);
                Console.WriteLine("Message: " + e.Message);
                Console.WriteLine(e.StackTrace);

                /*connect(host, db, user, password);
                 * if(connected)
                 *      return this.query(query, type);*/
                return(datatable);
            }
            catch (OdbcException e) //Database already has the value
            {
                Console.WriteLine("OdbcException at Query: " + query);
                Console.WriteLine("Message: " + e.Message);
                Console.WriteLine(e.StackTrace);
                return(datatable);
            }
            return(datatable);
        }
Beispiel #2
0
        public Resource query(string query, AdapterCommandType type)
        {
            //Console.WriteLine("Query: " + query);
            Resource datatable = new Resource();

            if (!connected || connection.State != ConnectionState.Open)
            {
                connect(host, db, user, password);
                if (connected)
                {
                    return this.query(query, type);
                }
                else
                {
                    return datatable;
                }
            }
            try
            {
                if (type == AdapterCommandType.Update || type == AdapterCommandType.Insert || type == AdapterCommandType.Delete)
                {
                    OdbcCommand odbcCommand = connection.CreateCommand();
                    odbcCommand.CommandText = query;
                    odbcCommand.Prepare();
                    odbcCommand.ExecuteNonQuery();

                    return datatable;
                }
                else if (type == AdapterCommandType.Select)
                {

                    OdbcCommand command = connection.CreateCommand();
                    command.CommandText = query;

                    adapter.SelectCommand = command;
                    adapter.Fill(datatable);
                    return datatable;
                }
            }
            catch (InvalidOperationException e) //Database is disconnected
            {
                Console.WriteLine("Invalid Operation Exception at Query: " + query);
                Console.WriteLine("Message: " + e.Message);
                Console.WriteLine(e.StackTrace);
                /*connect(host, db, user, password);
                if(connected)
                	return this.query(query, type);*/
                return datatable;
            }
            catch (OdbcException e) //Database already has the value
            {
                Console.WriteLine("OdbcException at Query: " + query);
                Console.WriteLine("Message: " + e.Message);
                Console.WriteLine(e.StackTrace);
                return datatable;
            }
            return datatable;
        }
Beispiel #3
0
        private static void SafeFill(DataTable table, AdapterCommandType commandType, string commandText)
        {
            OdbcCommand command = null;

            try
            {
                command = GetCommand(commandType, commandText);
                m_DataAdapter.Fill(table);
            }
            catch (InvalidOperationException)
            {
                if (ConnectToDB())
                {
                    //Get the new command (due to connection reset
                    command = GetCommand(commandType, commandText);
                    m_DataAdapter.Fill(table);
                }
            }
        }
Beispiel #4
0
        private static OdbcCommand GetCommand(AdapterCommandType commandType, string commandText)
        {
            OdbcCommand odbcCommand = m_MySQLConnection.CreateCommand();

            odbcCommand.CommandText = commandText;

            switch (commandType)
            {
            case AdapterCommandType.Select:
            {
                m_DataAdapter.SelectCommand = odbcCommand;
                break;
            }

            case AdapterCommandType.Insert:
            {
                m_DataAdapter.InsertCommand = odbcCommand;
                break;
            }

            case AdapterCommandType.Update:
            {
                m_DataAdapter.UpdateCommand = odbcCommand;
                break;
            }

            case AdapterCommandType.Delete:
            {
                m_DataAdapter.DeleteCommand = odbcCommand;
                break;
            }

            default:
                break;
            }

            return(odbcCommand);
        }
Beispiel #5
0
        private static bool SafeExecuteNonQuery(AdapterCommandType commandType, string commandText, IEntity from, double endTime, int seed)
        {
            OdbcCommand command;
            int         mobileHash = 0;

            if (from != null)
            {
                mobileHash = from.Serial.GetHashCode();
            }

            try
            {
                command = GetCommand(commandType, commandText);
                return(command.ExecuteNonQuery() != 0);
            }
            catch (InvalidOperationException)
            {
                if (ConnectToDB())
                {
                    //Get the new command (due to connection reset
                    command = GetCommand(commandType, commandText);
                    try
                    {
                        return(command.ExecuteNonQuery() != 0);
                    }
                    catch (OdbcException) //Database already has the value
                    {
                        commandText = "UPDATE " + c_TableName + " SET seed = " + seed + ", endTime = " + (int)endTime + ", answer = NULL WHERE mobileHash = " + mobileHash;
                        command     = GetCommand(commandType, commandText);

                        return(command.ExecuteNonQuery() != 0);
                    }
                }

                return(false);
            }
        }
        private static OdbcCommand GetCommand(AdapterCommandType commandType, string commandText)
        {
            OdbcCommand odbcCommand = m_MySQLConnection.CreateCommand();
            odbcCommand.CommandText = commandText;

            switch (commandType)
            {
                case AdapterCommandType.Select:
                    {
                        m_DataAdapter.SelectCommand = odbcCommand;
                        break;
                    }
                case AdapterCommandType.Insert:
                    {
                        m_DataAdapter.InsertCommand = odbcCommand;
                        break;
                    }
                case AdapterCommandType.Update:
                    {
                        m_DataAdapter.UpdateCommand = odbcCommand;
                        break;
                    }
                case AdapterCommandType.Delete:
                    {
                        m_DataAdapter.DeleteCommand = odbcCommand;
                        break;
                    }
                default:
                    break;
            }

            return odbcCommand;
        }
 private static void SafeFill(DataTable table, AdapterCommandType commandType, string commandText)
 {
     OdbcCommand command = null;
     try
     {
         command = GetCommand(commandType, commandText);
         m_DataAdapter.Fill(table);
     }
     catch (InvalidOperationException)
     {
         if (ConnectToDB())
         {
             //Get the new command (due to connection reset
             command = GetCommand(commandType, commandText);
             m_DataAdapter.Fill(table);
         }
     }
 }
        private static bool SafeExecuteNonQuery(AdapterCommandType commandType, string commandText, IEntity from, double endTime, int seed)
        {
            OdbcCommand command;
            int mobileHash = 0;
            
            if (from != null)
                mobileHash = from.Serial.GetHashCode();

            try
            {
                command = GetCommand(commandType, commandText);
                return command.ExecuteNonQuery() != 0;
            }
            catch (InvalidOperationException)
            {
                if (ConnectToDB())
                {
                    //Get the new command (due to connection reset
                    command = GetCommand(commandType, commandText);
                    try
                    {
                        return command.ExecuteNonQuery() != 0;
                    }
                    catch (OdbcException) //Database already has the value
                    {
                        commandText = "UPDATE " + c_TableName + " SET seed = " + seed + ", endTime = " + (int)endTime + ", answer = NULL WHERE mobileHash = " + mobileHash;
                        command = GetCommand(commandType, commandText);

                        return command.ExecuteNonQuery() != 0;
                    }
                }

                return false;
            }
        }