|
| 1 | +/** |
| 2 | + * __________________________________________________________________ |
| 3 | + * |
| 4 | + * C-Sharf Custom Classes |
| 5 | + * __________________________________________________________________ |
| 6 | + * |
| 7 | + * MIT License |
| 8 | + * |
| 9 | + * Copyright (c) 2020 Wilfred V. Pine |
| 10 | + * |
| 11 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | + * of this software and associated documentation files (the "Software"), to deal |
| 13 | + * in the Software without restriction, including without limitation the rights |
| 14 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | + * copies of the Software, and to permit persons to whom the Software is |
| 16 | + * furnished to do so, subject to the following conditions: |
| 17 | + * |
| 18 | + * The above copyright notice and this permission notice shall be included in |
| 19 | + * all copies or substantial portions of the Software. |
| 20 | + * |
| 21 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | + * THE SOFTWARE. |
| 28 | + * |
| 29 | + * @package C-Sharf Custom Classes |
| 30 | + * @author Wilfred V. Pine <[email protected]> |
| 31 | + * @copyright Copyright 2020 (https://red.confired.com) |
| 32 | + * @link https://confired.com |
| 33 | + * @license https://opensource.org/licenses/MIT MIT License |
| 34 | + */ |
| 35 | + |
| 36 | +using System; |
| 37 | +using System.Data; |
| 38 | +using System.Windows.Forms; |
| 39 | +using MySql.Data.MySqlClient; |
| 40 | + |
| 41 | +namespace Classes |
| 42 | +{ |
| 43 | + class Database |
| 44 | + { |
| 45 | + private static string constring; |
| 46 | + /* MySql Classes |
| 47 | + * |
| 48 | + */ |
| 49 | + public MySqlConnection con; |
| 50 | + MySqlCommand cmd; |
| 51 | + MySqlDataReader reader; |
| 52 | + MySqlDataAdapter da; |
| 53 | + DataTable dt; |
| 54 | + |
| 55 | + /* Check Database Connection |
| 56 | + * |
| 57 | + */ |
| 58 | + public Database(string host, string user, string password, string database) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + constring = "server="+ host+";uid=" + user + ";pwd='" + password + "';database=" + database; |
| 63 | + con = new MySqlConnection(constring); |
| 64 | + if (con.State == ConnectionState.Closed) |
| 65 | + { |
| 66 | + con.Open(); |
| 67 | + } |
| 68 | + } |
| 69 | + catch(MySqlException e) |
| 70 | + { |
| 71 | + MessageBox.Show(e.Message.ToString()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /* Select or search = return a row |
| 76 | + * |
| 77 | + */ |
| 78 | + public MySqlDataReader select(string qry) |
| 79 | + { |
| 80 | + con = new MySqlConnection(constring); |
| 81 | + con.Open(); |
| 82 | + |
| 83 | + cmd = new MySqlCommand(qry, con); |
| 84 | + reader = cmd.ExecuteReader(); |
| 85 | + return reader; |
| 86 | + } |
| 87 | + |
| 88 | + /* create / update / delete |
| 89 | + * |
| 90 | + */ |
| 91 | + public void cud(string qry, string msg = "Transaction Completed!") |
| 92 | + { |
| 93 | + con = new MySqlConnection(constring); |
| 94 | + con.Open(); |
| 95 | + |
| 96 | + cmd = new MySqlCommand(qry, con); |
| 97 | + if (cmd.ExecuteNonQuery() > 0) |
| 98 | + { |
| 99 | + MessageBox.Show(msg); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + MessageBox.Show("Sorry! There's something wrong."); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /* Save |
| 108 | + * |
| 109 | + */ |
| 110 | + public void save(string table, string[] column, string[] bind, string msg = "Successfully saved.") |
| 111 | + { |
| 112 | + |
| 113 | + con = new MySqlConnection(constring); |
| 114 | + con.Open(); |
| 115 | + |
| 116 | + string fields = ""; |
| 117 | + foreach (string col in column) |
| 118 | + { |
| 119 | + fields = fields + col + ','; |
| 120 | + } |
| 121 | + |
| 122 | + string value = ""; |
| 123 | + foreach (string val in bind) |
| 124 | + { |
| 125 | + value = value + "'" + val + "',"; |
| 126 | + } |
| 127 | + |
| 128 | + var qry = "INSERT INTO " + table + "(" + fields.TrimEnd(',') + ") VALUES(" + value.TrimEnd(',') + ")"; |
| 129 | + |
| 130 | + cmd = new MySqlCommand(qry, con); |
| 131 | + if (cmd.ExecuteNonQuery() > 0) |
| 132 | + { |
| 133 | + MessageBox.Show(msg); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + MessageBox.Show("Sorry! There's something wrong."); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /* Datasource / Select Multiple Rows / for DataGridView |
| 142 | + * |
| 143 | + */ |
| 144 | + public void table(string qry, DataGridView dgv, string[] header = null) |
| 145 | + { |
| 146 | + /*DataGridView Properties*/ |
| 147 | + dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; |
| 148 | + dgv.MultiSelect = false; |
| 149 | + dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; |
| 150 | + dgv.EditMode = DataGridViewEditMode.EditProgrammatically; |
| 151 | + |
| 152 | + /*Database*/ |
| 153 | + con = new MySqlConnection(constring); |
| 154 | + con.Open(); |
| 155 | + |
| 156 | + da = new MySqlDataAdapter(qry, con); |
| 157 | + dt = new DataTable(); |
| 158 | + da.Fill(dt); |
| 159 | + dgv.DataSource = dt; |
| 160 | + |
| 161 | + /*Custom Header*/ |
| 162 | + if (header != null) |
| 163 | + { |
| 164 | + foreach (DataGridViewColumn dgvcolumn in dgv.Columns) |
| 165 | + { |
| 166 | + dgvcolumn.HeaderText = header[dgvcolumn.Index]; |
| 167 | + } |
| 168 | + } |
| 169 | + dgv.ClearSelection(); |
| 170 | + } |
| 171 | + |
| 172 | + /* Select & Display to ComboBox |
| 173 | + * |
| 174 | + */ |
| 175 | + public void list(string qry, ComboBox comboBox) |
| 176 | + { |
| 177 | + /*ComboBox Properties*/ |
| 178 | + comboBox.Items.Clear(); |
| 179 | + comboBox.DropDownStyle = ComboBoxStyle.DropDownList; |
| 180 | + |
| 181 | + con = new MySqlConnection(constring); |
| 182 | + con.Open(); |
| 183 | + |
| 184 | + cmd = new MySqlCommand(qry, con); |
| 185 | + reader = cmd.ExecuteReader(); |
| 186 | + |
| 187 | + while (reader.Read()) |
| 188 | + { |
| 189 | + comboBox.Items.Add(reader[0].ToString()); |
| 190 | + comboBox.AutoCompleteCustomSource.Add(reader[0].ToString()); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /* Check data if exist |
| 195 | + * return bool |
| 196 | + */ |
| 197 | + public bool exist(string qry) |
| 198 | + { |
| 199 | + con = new MySqlConnection(constring); |
| 200 | + con.Open(); |
| 201 | + |
| 202 | + cmd = new MySqlCommand(qry, con); |
| 203 | + reader = cmd.ExecuteReader(); |
| 204 | + if (reader.Read()) |
| 205 | + return true; |
| 206 | + else |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + /* Select the Maximum Inserted Id |
| 211 | + * return int (id) |
| 212 | + */ |
| 213 | + public int maxid(String column, String tbl) |
| 214 | + { |
| 215 | + con = new MySqlConnection(constring); |
| 216 | + con.Open(); |
| 217 | + |
| 218 | + var cmd = new MySqlCommand("SELECT MAX(" + column + ") from " + tbl + "", con); |
| 219 | + |
| 220 | + var reader = cmd.ExecuteReader(); |
| 221 | + if (reader.Read()) |
| 222 | + { |
| 223 | + try |
| 224 | + { |
| 225 | + return reader.GetInt32(0) + 1; |
| 226 | + } |
| 227 | + catch (Exception) |
| 228 | + { |
| 229 | + return 1; |
| 230 | + } |
| 231 | + } |
| 232 | + return 1; |
| 233 | + } |
| 234 | + |
| 235 | + } |
| 236 | +} |
0 commit comments