locked
How to retrieve data from MySQL database in windows mobile development (C#) RRS feed

  • Question

  • Hi everyone.

    I'm trying to develop a Windows Mobile reporting application for university students, which basically should authenticate the users via a MySQL database, and then display certain information to each of them.

    To do so, the main challenge I'm facing is actually retrieving data from MySQL or in the other words to use it as data source. I've already downloaded and installed MySQL .net connector which allows me to connect and view my remote database, however, to add it as a data source I still receive errors (not supported).

    As a result, I created a class to control the database connectivity for the entire project which looks like this:

     

    internal static class DataBaseCommunicator

        {

            // The MySQL ConnectionString of the whole application

            static string conStr = "Server=x.x.x.x;Port=3306;Database=DB;Uid=Ali;Pwd=****";

            internal static MySqlConnection objMySqlConnection;

            static DataBaseCommunicator()

            {

                objMySqlConnection = new MySqlConnection(conStr);

            }

            public static bool OpenSqlConnection()

            {

                try

                {

                    if (objMySqlConnection.State != System.Data.ConnectionState.Open)

                        objMySqlConnection.Open();

                    return (true);

                }

                catch (MySqlException ex)

                {

                    return (false);

                }

            }

            public static bool CloseSqlConnection()

            {

                try

                {

                   

                    if (objMySqlConnection.State != System.Data.ConnectionState.Closed)

                        objMySqlConnection.Close();

                    return (true);

                }

                catch (MySqlException ex)

                {

                    return (false);

                }

            }

            public static int ExecuteNonQuary(string commandText)

            {

                int result = 0;

                MySqlCommand cmd = new MySqlCommand();

                cmd.CommandText = commandText;

                cmd.CommandType = CommandType.Text;

                cmd.Connection = objMySqlConnection;

                try

                {

                    OpenSqlConnection();

                    result = cmd.ExecuteNonQuery();

                }

                catch (MySqlException ex)

                {

                }

                finally

                {

                    CloseSqlConnection();

                }

                return result;

            }

            public static DataSet ExecuteDataSet(string commandText)

            {

                DataSet ds = new DataSet();

                try

                {

                    MySqlDataAdapter da = new MySqlDataAdapter();

                    MySqlCommand cmd = new MySqlCommand();

                    cmd.CommandText = commandText;

                    cmd.CommandType = CommandType.Text;

                    cmd.Connection = objMySqlConnection;

                    da.SelectCommand = cmd;

                    da.Fill(ds);

                }

                catch (MySqlException ex)

                {

     

                }

                return ds;

            }

            public static MySqlDataReader ExecuteReader(string commandText)

            {

                MySqlDataReader objReader = null;

                try

                {

                    MySqlCommand cmd = new MySqlCommand();

                    cmd.CommandText = commandText;

                    cmd.CommandType = CommandType.Text;

                    cmd.Connection = objMySqlConnection;

                    objReader = cmd.ExecuteReader();

                }

                catch (MySqlException)

                { }

                return objReader;

            }

            public static object ExecuteScaler(string commandText)

            {

                object result = null;

                try

                {

                    MySqlCommand cmd = new MySqlCommand();

                    cmd.Connection = objMySqlConnection;

                    cmd.CommandText = commandText;

                    cmd.CommandType = CommandType.Text;

                    OpenSqlConnection();

                    result = (object)cmd.ExecuteScalar();

                }

                catch (MySqlException)

                {

                }

                finally

                {

                    CloseSqlConnection();

                }

                return result;

            }

            public static bool CheckConnectionToServer()

            {

                bool result = false;

                try

                {

                    objMySqlConnection.Open();

                    objMySqlConnection.Close();

                    result = true;

                }

                catch (MySqlException ex)

                { 

                }

                return result;

            }

    Well, the code is compiling very well, but in the run time it stops on the following lines:

    objMySqlConnection.Open();

    objMySqlConnection.Close();

     

    I’ve tried to use the same code and login on windows applications and it’s working fine, but my main project should be running on windows mobile!

     

    Is there any of you, who could kindly let me know if having a solution for this issue I'm facing? Regardless of my codes, I’ll be more than happy if you could share some guidelines/codes on how to use mysql database to authenticate and then show reports in windows mobile.

     

    Many thanx

     

    Friday, March 19, 2010 9:28 AM

Answers

  • Hi Ali V. Tehrani,

    Since MySqlConnection is 3rd party componenet,we don't know whether Compact framework support it. Please contact software vendor whether it support.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Monday, March 22, 2010 9:20 AM