Skip to content

Commit 9b58994

Browse files
authored
Add files via upload
1 parent f346b5f commit 9b58994

File tree

12 files changed

+17276
-0
lines changed

12 files changed

+17276
-0
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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 <only.master.red@gmail.com>
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 C_Sharf_Classes.Classes
42+
{
43+
class Database : Config
44+
{
45+
// instantiate public variable
46+
Public_variables public_vars = new Public_variables();
47+
48+
// connection string
49+
private static string constring = "server=localhost;uid=" + dbuser + ";pwd='" + dbpassword + "';database=" + dbname;
50+
public MySqlConnection con = new MySqlConnection(constring);
51+
MySqlCommand cmd;
52+
MySqlDataReader reader;
53+
MySqlDataAdapter da;
54+
DataTable dt;
55+
56+
// Check Database Connection
57+
public Database()
58+
{
59+
con = new MySqlConnection(constring);
60+
try
61+
{
62+
if(con.State == ConnectionState.Closed)
63+
con.Open();
64+
}
65+
catch (Exception ex)
66+
{
67+
Application.Exit();
68+
MessageBox.Show(ex.Message);
69+
}
70+
}
71+
72+
// select or search = return a row
73+
public MySqlDataReader select(string qry)
74+
{
75+
76+
con = new MySqlConnection(constring);
77+
con.Open();
78+
79+
cmd = new MySqlCommand(qry, con);
80+
reader = cmd.ExecuteReader();
81+
return reader;
82+
83+
}
84+
85+
// create / update / delete
86+
public void cud(string qry, string msg = "")
87+
{
88+
89+
con = new MySqlConnection(constring);
90+
con.Open();
91+
92+
cmd = new MySqlCommand(qry, con);
93+
if (cmd.ExecuteNonQuery() > 0)
94+
{
95+
if (msg == "")
96+
MessageBox.Show("Transaction Complete!");
97+
else
98+
MessageBox.Show(msg);
99+
}
100+
else
101+
{
102+
MessageBox.Show("Sorry! There's something wrong.");
103+
}
104+
105+
}
106+
107+
// Save
108+
public void save(string table, string[] column, string[] bind)
109+
{
110+
111+
con = new MySqlConnection(constring);
112+
con.Open();
113+
114+
string fields = "";
115+
foreach (string col in column)
116+
{
117+
fields = fields + col + ',';
118+
}
119+
120+
string value = "";
121+
foreach (string val in bind)
122+
{
123+
value = value + "'" + val + "',";
124+
}
125+
126+
var qry = "INSERT INTO " + table + "(" + fields.TrimEnd(',') + ") VALUES(" + value.TrimEnd(',') + ")";
127+
128+
cmd = new MySqlCommand(qry, con);
129+
if (cmd.ExecuteNonQuery() > 0)
130+
{
131+
MessageBox.Show("Successfully saved.");
132+
}
133+
else
134+
{
135+
MessageBox.Show("Sorry! There's something wrong.");
136+
}
137+
}
138+
139+
// datasource / select rows / for DataGridView
140+
public void table(string qry, DataGridView dgv, string[] header = null)
141+
{
142+
143+
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
144+
dgv.MultiSelect = false;
145+
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
146+
147+
con = new MySqlConnection(constring);
148+
con.Open();
149+
150+
da = new MySqlDataAdapter(qry, con);
151+
dt = new DataTable();
152+
da.Fill(dt);
153+
dgv.DataSource = dt;
154+
155+
if (header != null)
156+
{
157+
foreach (DataGridViewColumn dgvcolumn in dgv.Columns)
158+
{
159+
dgvcolumn.HeaderText = header[dgvcolumn.Index];
160+
}
161+
}
162+
163+
}
164+
165+
// select & add to combo box
166+
public void list(string qry, ComboBox comboBox)
167+
{
168+
con = new MySqlConnection(constring);
169+
con.Open();
170+
171+
cmd = new MySqlCommand(qry, con);
172+
reader = cmd.ExecuteReader();
173+
174+
comboBox.Items.Clear();
175+
while (reader.Read())
176+
{
177+
comboBox.Items.Add(reader[0].ToString());
178+
comboBox.AutoCompleteCustomSource.Add(reader[0].ToString());
179+
}
180+
181+
}
182+
183+
// check data if exist
184+
public bool exist(string qry)
185+
{
186+
187+
con = new MySqlConnection(constring);
188+
con.Open();
189+
190+
cmd = new MySqlCommand(qry, con);
191+
reader = cmd.ExecuteReader();
192+
if (reader.Read())
193+
return true;
194+
else
195+
return false;
196+
197+
}
198+
199+
//MAx ID
200+
public void maxid(String column, String tbl)
201+
{
202+
con = new MySqlConnection(constring);
203+
con.Open();
204+
205+
var cmd = new MySqlCommand("SELECT MAX(" + column + ") from " + tbl + "", con);
206+
207+
var reader = cmd.ExecuteReader();
208+
if (reader.Read())
209+
{
210+
try
211+
{
212+
public_vars.maxid = reader.GetInt32(0) + 1;
213+
}
214+
catch (Exception)
215+
{
216+
public_vars.maxid = 1;
217+
}
218+
}
219+
}
220+
221+
222+
}
223+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 <only.master.red@gmail.com>
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+
38+
namespace C_Sharf_Classes.Classes
39+
{
40+
class Date_time
41+
{
42+
//instantiate public variables
43+
Public_variables public_vars = new Public_variables();
44+
45+
public void date_format(string format)
46+
{
47+
public_vars.default_date_format = format;
48+
}
49+
50+
public string reFormat(string date)
51+
{
52+
DateTime dt;
53+
var isValidDate = DateTime.TryParse(date, out dt);
54+
if (isValidDate)
55+
return dt.ToString(public_vars.default_date_format);
56+
else
57+
return $"{date} is not a valid date string";
58+
}
59+
60+
public string toReadableDate(string date)
61+
{
62+
DateTime dt;
63+
var isValidDate = DateTime.TryParse(date, out dt);
64+
if (isValidDate)
65+
return dt.ToString("MMMM dd, yyyy");
66+
else
67+
return $"{date} is not a valid date string";
68+
}
69+
70+
public string toTimeStamp(string date)
71+
{
72+
DateTime dt;
73+
var isValidDate = DateTime.TryParse(date, out dt);
74+
if (isValidDate)
75+
return dt.Ticks.ToString();
76+
else
77+
return $"{date} is not a valid date string";
78+
}
79+
80+
public string getDate(string date)
81+
{
82+
DateTime dt;
83+
var isValidDate = DateTime.TryParse(date, out dt);
84+
if (isValidDate)
85+
return dt.Day.ToString();
86+
else
87+
return $"{date} is not a valid date string";
88+
}
89+
90+
public string getMonth(string date)
91+
{
92+
DateTime dt;
93+
var isValidDate = DateTime.TryParse(date, out dt);
94+
if (isValidDate)
95+
return dt.Month.ToString();
96+
else
97+
return $"{date} is not a valid date string";
98+
}
99+
100+
public string getYear(string date)
101+
{
102+
DateTime dt;
103+
var isValidDate = DateTime.TryParse(date, out dt);
104+
if (isValidDate)
105+
return dt.Year.ToString();
106+
else
107+
return $"{date} is not a valid date string";
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)