Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Data.OleDb;
  8. using MySql.Data.MySqlClient;
  9.  
  10. public class SystemDatabase
  11. {
  12. #region Global Variables
  13. //GENERIC Database Attributes
  14. string databaseName;
  15. string username;
  16. string password;
  17. string connectionString;
  18. string dataSource;
  19. bool isMySQL;
  20.  
  21. //MySQL Database Attributes
  22. public MySqlConnection sqlConnection;
  23. MySqlCommand sqlCommand;
  24. MySqlDataReader sqlReader;
  25.  
  26. //MSAccess Database Attributes
  27. public OleDbConnection accessConnection;
  28. OleDbCommand accessCommand;
  29. OleDbDataReader accessReader;
  30. #endregion
  31.  
  32. //Default Database Constructor
  33. public SystemDatabase(string databaseName, string dataSource, string username, string password, bool isMySQL)
  34. {
  35. this.databaseName = databaseName;
  36. this.dataSource = dataSource;
  37. this.username = username;
  38. this.password = password;
  39. this.isMySQL = isMySQL;
  40.  
  41. if (isMySQL)
  42. {
  43. connectionString =
  44. "Database = " + databaseName + ";" +
  45. "Server = " + dataSource + ";" +
  46. "uid = " + username + ";" +
  47. "pwd = " + password;
  48. sqlConnection = new MySqlConnection(connectionString);
  49. }
  50. else
  51. {
  52. connectionString =
  53. "Provider = Microsoft.Jet.OLEDB.4.0;" +
  54. "Data Source = " + dataSource +
  55. "Jet OLEDB:Database Password = " + password;
  56. accessConnection = new OleDbConnection();
  57. }
  58. }
  59.  
  60. //Database Constructor without password
  61. public SystemDatabase(string databaseName, string dataSource, string username, bool isMySQL)
  62. {
  63. this.databaseName = databaseName;
  64. this.dataSource = dataSource;
  65. this.username = username;
  66. this.password = "";
  67. this.isMySQL = isMySQL;
  68.  
  69. if (isMySQL)
  70. {
  71. connectionString =
  72. "Database = " + databaseName + ";" +
  73. "Data Source = " + dataSource + ";" +
  74. "User Id = " + username + ";" +
  75. "Password = " + password;
  76. sqlConnection = new MySqlConnection(connectionString);
  77. }
  78. else
  79. {
  80. connectionString =
  81. "Provider = Microsoft.Jet.OLEDB.4.0;" +
  82. "Data Source = " + dataSource;
  83. accessConnection = new OleDbConnection();
  84. }
  85.  
  86. }
  87.  
  88. //Use this for testing the database connection
  89. public bool testConnection()
  90. {
  91. if (isMySQL)
  92. {
  93. try
  94. {
  95. sqlConnection.Open();
  96. sqlConnection.Close();
  97. }
  98. catch (Exception ex)
  99. {
  100. sqlConnection.Close();
  101. MessageBox.Show(ex.Message);
  102. return false;
  103. }
  104. }
  105. else
  106. {
  107. try
  108. {
  109. accessConnection.Open();
  110. accessConnection.Close();
  111. }
  112. catch (Exception ex)
  113. {
  114. accessConnection.Close();
  115. MessageBox.Show(ex.Message);
  116. return false;
  117. }
  118.  
  119. }
  120. return true;
  121. }
  122.  
  123. //Use this for executing sql commands
  124. public bool executeCommand(string sql)
  125. {
  126. if (isMySQL)
  127. {
  128. try
  129. {
  130. sqlCommand = new MySqlCommand(sql, sqlConnection);
  131.  
  132. sqlConnection.Open();
  133. sqlCommand.ExecuteNonQuery();
  134. sqlCommand.Dispose();
  135. sqlConnection.Close();
  136. return true;
  137. }
  138. catch (Exception ex)
  139. {
  140. sqlConnection.Close();
  141. MessageBox.Show(ex.Message);
  142. return false;
  143. }
  144. }
  145. else
  146. {
  147. try
  148. {
  149. accessCommand = new OleDbCommand(sql, accessConnection);
  150.  
  151. accessConnection.Open();
  152. accessCommand.ExecuteNonQuery();
  153. accessCommand.Dispose();
  154. accessConnection.Close();
  155. return true;
  156. }
  157. catch (Exception ex)
  158. {
  159. accessConnection.Close();
  160. MessageBox.Show(ex.Message);
  161. return false;
  162. }
  163. }
  164. }
  165.  
  166. //Use this to retrieve the first result of a query
  167. public object retrieveVariable(string sql, string colName)
  168. {
  169. object returnObject = null;
  170. if (isMySQL)
  171. {
  172. try
  173. {
  174. sqlCommand = new MySqlCommand(sql, sqlConnection);
  175.  
  176. sqlConnection.Open();
  177. sqlReader = sqlCommand.ExecuteReader();
  178.  
  179. if (sqlReader.Read())
  180. {
  181. returnObject = sqlReader[colName];
  182. sqlReader.Close();
  183. sqlReader.Dispose();
  184. sqlConnection.Close();
  185. sqlCommand.Dispose();
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. MessageBox.Show(ex.StackTrace);
  191. MessageBox.Show(ex.Message);
  192. }
  193. sqlReader.Close();
  194. sqlReader.Dispose();
  195. sqlConnection.Close();
  196. sqlCommand.Dispose();
  197. }
  198. else
  199. {
  200. try
  201. {
  202. accessCommand = new OleDbCommand(sql, accessConnection);
  203.  
  204. accessConnection.Open();
  205. accessReader = accessCommand.ExecuteReader();
  206.  
  207. if (accessReader.Read())
  208. {
  209. returnObject = accessReader.GetValue(0);
  210. }
  211.  
  212.  
  213. }
  214. catch (Exception ex)
  215. {
  216. MessageBox.Show(ex.Message);
  217. }
  218. accessReader.Close();
  219. accessReader.Dispose();
  220. accessConnection.Close();
  221. accessCommand.Dispose();
  222. }
  223. return returnObject;
  224. }
  225.  
  226. //Use this to retrieve specific columns in a row of query result
  227. public object[] retrieveList(string sql, string[] columns)
  228. {
  229. object varTemp;
  230. List<object> objectList = new List<object>();
  231. if (isMySQL)
  232. {
  233. try
  234. {
  235. sqlCommand = new MySqlCommand(sql, sqlConnection);
  236.  
  237. sqlConnection.Open();
  238. sqlReader = sqlCommand.ExecuteReader();
  239.  
  240. foreach (string column in columns)
  241. {
  242. sqlReader.Read();
  243. varTemp = sqlReader[column];
  244. objectList.Add(varTemp);
  245. }
  246. }
  247.  
  248. catch (Exception ex)
  249. {
  250. MessageBox.Show(ex.Message);
  251. }
  252.  
  253. sqlReader.Close();
  254. sqlConnection.Close();
  255. }
  256. else
  257. {
  258. try
  259. {
  260. accessCommand = new OleDbCommand(sql, accessConnection);
  261.  
  262. accessConnection.Open();
  263. accessReader = accessCommand.ExecuteReader();
  264.  
  265. foreach (string column in columns)
  266. {
  267. varTemp = accessReader[column];
  268. objectList.Add(varTemp);
  269. }
  270.  
  271. }
  272.  
  273. catch (Exception ex)
  274. {
  275. MessageBox.Show(ex.Message);
  276. }
  277.  
  278. accessReader.Close();
  279. accessConnection.Close();
  280. }
  281. return objectList.ToArray();
  282. }
  283.  
  284. //Use this to retrieve all rows in the first column of the query result
  285. public object[] retrieveList(string sql)
  286. {
  287. object varTemp;
  288. List<object> objectList = new List<object>();
  289. if (isMySQL)
  290. {
  291. try
  292. {
  293. sqlCommand = new MySqlCommand(sql, sqlConnection);
  294.  
  295. sqlConnection.Open();
  296. sqlReader = sqlCommand.ExecuteReader();
  297.  
  298. while (sqlReader.Read())
  299. {
  300. varTemp = sqlReader[0];
  301. objectList.Add(varTemp);
  302. }
  303. }
  304.  
  305. catch (Exception ex)
  306. {
  307. MessageBox.Show(ex.Message +"\n"+sql);
  308. }
  309.  
  310. sqlReader.Close();
  311. sqlConnection.Close();
  312. }
  313. else
  314. {
  315. try
  316. {
  317. accessCommand = new OleDbCommand(sql, accessConnection);
  318.  
  319. accessConnection.Open();
  320. accessReader = accessCommand.ExecuteReader();
  321.  
  322. while (accessReader.Read())
  323. {
  324. varTemp = accessReader[0];
  325. objectList.Add(varTemp);
  326. }
  327.  
  328. }
  329.  
  330. catch (Exception ex)
  331. {
  332. MessageBox.Show(ex.Message);
  333. }
  334.  
  335. accessReader.Close();
  336. accessConnection.Close();
  337. }
  338. return objectList.ToArray();
  339. }
  340.  
  341. //Use this to get all the values(rows and columns) of the query result
  342. public object[][] retrieveTableValues(string sql, string[] columns)
  343. {
  344. object varTemp;
  345. List<object> _objectRow = new List<object>();
  346. List<List<object>> objectColumn = new List<List<object>>();
  347. object[][] value = null;
  348. try
  349. {
  350. sqlCommand = new MySqlCommand(sql, sqlConnection);
  351.  
  352. sqlConnection.Open();
  353. sqlReader = sqlCommand.ExecuteReader();
  354.  
  355. while (sqlReader.Read())
  356. {
  357. _objectRow = new List<object>();
  358. foreach (string column in columns)
  359. {
  360. varTemp = sqlReader[column];
  361. _objectRow.Add(varTemp);
  362.  
  363. }
  364. objectColumn.Add(_objectRow);
  365. }
  366.  
  367. object[,] sample = new object[1, 2];
  368.  
  369.  
  370. value = new object[objectColumn.Count][];
  371. for (int i = 0; i < value.Length; i++)
  372. {
  373. value[i] = new object[_objectRow.Count];
  374. }
  375.  
  376. for (int i = 0; i < objectColumn.Count; i++)
  377. {
  378. _objectRow = objectColumn.ElementAt(i);
  379. for (int j = 0; j < _objectRow.Count; j++)
  380. {
  381. value[i][j] = _objectRow.ElementAt(j);
  382. }
  383. }
  384. }
  385. catch (Exception ex)
  386. {
  387. MessageBox.Show(ex.Message);
  388. }
  389. finally
  390. {
  391. sqlReader.Close();
  392. sqlConnection.Close();
  393. }
  394. return value;
  395. }
  396.  
  397. //Database table syncing
  398. public static void syncTables(MySqlConnection sqlConnection, OleDbConnection accessConnection, string sqlTable, string accessTable, byte columnCount)
  399. {
  400. #region Variable Declaration
  401.  
  402. //MySQL Variables and Objects
  403. string sql;
  404. MySqlCommand sqlCommand;
  405. MySqlDataReader sqlReader;
  406.  
  407. //MSAccess Variables and Objects
  408. string access;
  409. OleDbCommand accessCommand;
  410. OleDbDataReader accessReader;
  411.  
  412. //Generic Variables
  413. //object varTemp;
  414. bool prelimCheck = false;
  415. #endregion
  416.  
  417. #region Preliminary Checking
  418. //Check if all values in the table are equal;
  419.  
  420. sql = "SELECT * FROM " + sqlTable;
  421. access = "SELECT * FROM " + accessTable;
  422. try
  423. {
  424. //Initialize Commands
  425. sqlCommand = new MySqlCommand(sql, sqlConnection);
  426. accessCommand = new OleDbCommand(access, accessConnection);
  427.  
  428. //Open Connection
  429. sqlConnection.Open();
  430. accessConnection.Open();
  431.  
  432. //Initialize Readers
  433. sqlReader = sqlCommand.ExecuteReader();
  434. accessReader = accessCommand.ExecuteReader();
  435.  
  436.  
  437. //Check values;
  438. while (sqlReader.Read()) //loop through all rows
  439. {
  440. if (accessReader.Read()) //returns true if sql rows = access rows
  441. {
  442. for (int i = 0; i < columnCount; i++) //if true, the loop through each column
  443. {
  444. if (sqlReader[i] == accessReader[i]) //if current row in both databases are equal
  445. {
  446. prelimCheck = true;
  447. }
  448. else
  449. {
  450. prelimCheck = false;
  451. }
  452. }
  453. }
  454. else
  455. {
  456. prelimCheck = false;
  457. }
  458. }
  459.  
  460. if (accessReader.Read()) //if sql has no more rows and access has more
  461. {
  462. prelimCheck = false;
  463. }
  464. }
  465. catch (Exception ex)
  466. {
  467. MessageBox.Show(ex.Message);
  468. }
  469. sqlConnection.Close();
  470. accessConnection.Close();
  471. #endregion
  472. }
  473.  
  474. //Extra Feature: One Way Encryption
  475. public static string encryptOneWay(string thing)
  476. {
  477. string that = null;
  478.  
  479.  
  480. return that;
  481. }
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement