Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.SQLite;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using static Globals.Score;
  10. using System.Configuration;
  11.  
  12.  
  13. namespace DataLayer
  14. {
  15. public class DataAccess
  16. {
  17.  
  18. private DbDataAdapter dgAdapter;
  19. private DbDataAdapter dsAdapter;
  20. private DbDataAdapter dpAdapter;
  21.  
  22. private DbDataAdapter gdgAdapter;
  23. private DbDataAdapter gdsAdapter;
  24.  
  25. private DbConnection connection;
  26. private DbConnection global;
  27.  
  28. public DataSet DsGameScores { get; set; }
  29.  
  30. public void Load()
  31. {
  32.  
  33. DbSqliteConnection("System.Data.SQLite", GetConnectionStringByProvider("System.Data.SQLite"));
  34. connection.Open();
  35.  
  36. DsGameScores = new DataSet();
  37.  
  38. DbCommand cmdGames = connection.CreateCommand();
  39. cmdGames.CommandText = "select * from games";
  40. cmdGames.CommandType = CommandType.Text;
  41. dgAdapter.SelectCommand = cmdGames;
  42. dgAdapter.Fill(DsGameScores, "games");
  43.  
  44. DbCommand cmdPlayers = connection.CreateCommand();
  45. cmdPlayers.CommandText = "select * from players";
  46. cmdPlayers.CommandType = CommandType.Text;
  47. dpAdapter.SelectCommand = cmdPlayers;
  48. dpAdapter.Fill(DsGameScores, "players");
  49.  
  50. DbCommand cmdScores = connection.CreateCommand();
  51. cmdScores.CommandText = "select * from scores order by score desc";
  52. cmdScores.CommandType = CommandType.Text;
  53. dsAdapter.SelectCommand = cmdScores;
  54. dsAdapter.Fill(DsGameScores, "scores");
  55.  
  56. connection.Close();
  57. }
  58.  
  59. public void globalAccess()
  60. {
  61. DbSqliteConnection("MySql.Data.MySqlClient", GetConnectionStringByProvider("MySql.Data.MySqlClient"));
  62. global.Open();
  63.  
  64. DbCommand cmdGames = global.CreateCommand();
  65. cmdGames.CommandText = "select * from games";
  66. cmdGames.CommandType = CommandType.Text;
  67. gdgAdapter.SelectCommand = cmdGames;
  68. gdgAdapter.Fill(DsGameScores, "games");
  69.  
  70. DbCommand cmdScores = global.CreateCommand();
  71. cmdScores.CommandText = "select * from scores order by score desc";
  72. cmdScores.CommandType = CommandType.Text;
  73. gdsAdapter.SelectCommand = cmdScores;
  74. gdsAdapter.Fill(DsGameScores, "scores");
  75.  
  76. global.Close();
  77. }
  78.  
  79. public DataAccess()
  80. {
  81. globalAccess();
  82. }
  83.  
  84. static string GetConnectionStringByProvider(string providerName)
  85. {
  86.  
  87. string returnValue = null;
  88.  
  89. ConnectionStringSettingsCollection settings = ConfigurationManager.ConnectionStrings;
  90.  
  91. if (settings != null)
  92. {
  93. foreach (ConnectionStringSettings cs in settings)
  94. {
  95. if (cs.ProviderName.ToLower() == providerName.ToLower())
  96. {
  97. returnValue = cs.ConnectionString;
  98. break;
  99. }
  100. }
  101. }
  102. return returnValue;
  103. }
  104.  
  105. private void DbSqliteConnection(string providerName, string connectionString)
  106. {
  107. if (connectionString != null)
  108. {
  109. if (providerName == "System.Data.SQLite")
  110. {
  111. try
  112. {
  113. DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
  114. connection = factory.CreateConnection();
  115. connection.ConnectionString = connectionString;
  116. this.dpAdapter = factory.CreateDataAdapter();
  117. this.dgAdapter = factory.CreateDataAdapter();
  118. this.dsAdapter = factory.CreateDataAdapter();
  119. }
  120. catch (Exception ex)
  121. {
  122. if (connection != null)
  123. {
  124. connection = null;
  125. }
  126. Console.WriteLine(ex.Message);
  127. }
  128. }
  129.  
  130. if (providerName == "Mysql.Data.MySqlClient")
  131. {
  132. try
  133. {
  134. DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
  135. global = factory.CreateConnection();
  136. global.ConnectionString = connectionString;
  137. gdgAdapter = factory.CreateDataAdapter();
  138. gdsAdapter = factory.CreateDataAdapter();
  139.  
  140. }
  141. catch (Exception ex)
  142. {
  143. if (global != null)
  144. {
  145. global = null;
  146. }
  147. Console.WriteLine(ex.Message);
  148. }
  149. }
  150.  
  151. }
  152.  
  153. }
  154.  
  155. public void Update(DataSet ds)
  156. {
  157.  
  158. }
  159.  
  160. public List<ScoreItem> GetTop10Scores(string gameName)
  161. {
  162. return null;
  163. }
  164.  
  165. public bool KnownPlayer(string name)
  166. {
  167. try {
  168. bool known = false;
  169.  
  170. foreach (DataRow row in DsGameScores.Tables["players"].Rows)
  171. {
  172. if ((row["name"]).ToString().ToLower() == name.ToLower()) known = true;
  173. }
  174. return known;
  175. }
  176.  
  177. finally
  178. {
  179. if (connection != null)
  180. {
  181. connection.Close();
  182. }
  183. }
  184. }
  185.  
  186. public bool KnownGame(string game)
  187. {
  188. try
  189. {
  190. bool known = false;
  191.  
  192. foreach (DataRow row in DsGameScores.Tables["games"].Rows)
  193. {
  194. if ((row["name"]).ToString().ToLower() == game.ToLower()) known = true;
  195. }
  196. return known;
  197. }
  198. finally
  199. {
  200. if (connection != null)
  201. {
  202. connection.Close();
  203. }
  204. }
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement