コード例 #1
0
        public static int SelectOne(string query)
        {
            int result = 0;

            //Open connection
            if (ConnectionClass.OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, ConnectionClass.connection);
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();

                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    try
                    {
                        // MessageBox.Show(query + "   " + dataReader.GetInt32(0).ToString());
                        result = dataReader.GetInt32(0); //почему-то 0
                    }
                    catch
                    {
                        result = 0;
                    }
                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                ConnectionClass.CloseConnection();

                //return list to be displayed
                return(result);
            }
            else
            {
                return(result);
            }
        }
コード例 #2
0
        public static void QuerytoTable(string query)
        {
            try
            {
                //open connection
                if (ConnectionClass.OpenConnection() == true)
                {
                    //create command and assign the query and connection from the constructor
                    MySqlCommand cmd = new MySqlCommand(query, ConnectionClass.connection);

                    //Execute command
                    cmd.ExecuteNonQuery();

                    //close connection
                    ConnectionClass.CloseConnection();
                }
            }
            catch
            {
                MessageBox.Show("Какая-то ошибка в твоей жизни");
                return;
            }
        }
コード例 #3
0
        public static List <string[]> SelectQuery(string query, DataGridView table)
        {
            int countColumn = table.ColumnCount;

            //Open connection
            if (ConnectionClass.OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, ConnectionClass.connection);
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();
                listName.Clear();
                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    listName.Add(new string[countColumn]);
                    for (int i = 0; i < countColumn; i++)
                    {
                        listName[listName.Count - 1][i] = dataReader[i].ToString(); //get info from DB tables depending on the count of columns
                    }
                }
                RefreshInfo(table);

                //close Data Reader
                dataReader.Close();

                //close Connection
                ConnectionClass.CloseConnection();

                //return list to be displayed
                return(listName);
            }
            else
            {
                return(listName);
            }
        }