csaki

oledb with txt

May 31st, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.OleDb;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Mime;
  8. using System.Security.Policy;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using WCFServiceLibrary.UserHandling;
  12.  
  13. namespace WCFServiceLibrary
  14. {
  15.     internal static class DatabaseHandler
  16.     {
  17.         private static readonly OleDbConnection _connection;
  18.  
  19.         static DatabaseHandler()
  20.         {
  21.             Logger.Log("Setting up connection string...", LogSeverity.Information);
  22.             CreateTablesIfNotExists();
  23.             var finfo = new FileInfo("users.txt");
  24.             _connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
  25.                 + finfo.DirectoryName + ";Extended Properties='text;HDR=Yes;FMT=Delimited';");
  26.         }
  27.  
  28.         private static void CreateTablesIfNotExists()
  29.         {
  30.             if (!File.Exists("users.txt"))
  31.             {
  32.                 Logger.Log("Users table doesn't exist! I'll just create it now.", LogSeverity.Warning);
  33.                 using (var sw = File.CreateText("users.txt"))
  34.                 {
  35.                     sw.WriteLine("username;email;passwordhash");
  36.                     sw.Flush();
  37.                 }
  38.             }
  39.             if (!File.Exists("proxies.txt"))
  40.             {
  41.                 Logger.Log("Proxies table doesn't exist! I'll just create it now.", LogSeverity.Information);
  42.                 using (var sw = File.CreateText("proxies.txt"))
  43.                 {
  44.                     sw.WriteLine("proxy_ip");
  45.                     sw.Flush();
  46.                 }
  47.             }
  48.         }
  49.  
  50.         internal static List<User> GetUsers()
  51.         {
  52.             Logger.Log("Getting all the registered users.", LogSeverity.Information);
  53.             return GetUsersFromReader(ExecuteQuery("SELECT * FROM users.txt"));
  54.         }
  55.  
  56.         private static List<User> GetUsersFromReader(IDataReader dataReader)
  57.         {
  58.             Logger.Log("Reading data from users.", LogSeverity.Information);
  59.             var users = new List<User>();
  60.             while (dataReader.Read())
  61.             {
  62.                 users.Add(new User
  63.                 {
  64.                     Username = dataReader.GetString(0),
  65.                     EmailAddress = dataReader.GetString(1),
  66.                     PasswordHash = dataReader.GetString(2)
  67.                 });
  68.             }
  69.             dataReader.Close();
  70.             return users;
  71.         }
  72.  
  73.         private static IDataReader ExecuteQuery(string query)
  74.         {
  75.             Logger.Log("Executing SQL query: " + query, LogSeverity.Information);
  76.             var cmd = new OleDbCommand(query, _connection);
  77.             OpenConnection();
  78.             var result = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  79.             return result;
  80.         }
  81.  
  82.         private static void OpenConnection()
  83.         {
  84.             if (_connection.State == ConnectionState.Closed) _connection.Open();
  85.         }
  86.  
  87.         private static int ExecuteNonQuery(OleDbCommand cmd)
  88.         {
  89.             Logger.Log("Executing SQL command: " + cmd.CommandText, LogSeverity.Information);
  90.             cmd.Connection = _connection;
  91.             OpenConnection();
  92.             var result = cmd.ExecuteNonQuery();
  93.             _connection.Close();
  94.             return result;
  95.         }
  96.  
  97.         internal static RegisterResult AddUser(User user)
  98.         {
  99.             Logger.Log("Adding new user to database by the name of " + user.Username, LogSeverity.Information);
  100.             var cmd = new OleDbCommand("INSERT INTO [users.txt] VALUES (@username,@email,@password)", _connection);
  101.             cmd.Parameters.Add("@username", OleDbType.BSTR).Value = user.Username;
  102.             cmd.Parameters.Add("@email", OleDbType.BSTR).Value = user.EmailAddress;
  103.             cmd.Parameters.Add("@password", OleDbType.BSTR).Value = user.PasswordHash;
  104.  
  105.             var affectedRows = ExecuteNonQuery(cmd);
  106.             return affectedRows > 0
  107.                 ? new RegisterResult { Success = true }
  108.                 : new RegisterResult { Error = "Failed to register user!" };
  109.         }
  110.  
  111.         internal static ProxyResult AddProxy(Proxy proxy)
  112.         {
  113.             Logger.Log("Adding new proxy to the database by the IP of " + proxy.ProxyIp, LogSeverity.Information);
  114.             var cmd = new OleDbCommand("INSERT INTO [proxies.txt] VALUES (@proxy_ip, @port, @responsetime)");
  115.             cmd.Parameters.Add("@proxy_ip", OleDbType.BSTR).Value = proxy.ProxyIp;
  116.             cmd.Parameters.Add("@port", OleDbType.Integer).Value = proxy.Port;
  117.             cmd.Parameters.Add("@responsetime", OleDbType.Integer).Value = proxy.ResponseTime;
  118.  
  119.             var affectedRows = ExecuteNonQuery(cmd);
  120.             return affectedRows > 0
  121.                 ? new ProxyResult { Success = true }
  122.                 : new ProxyResult { Error = "Failed to insert proxy!" };
  123.         }
  124.  
  125.         internal static List<Proxy> GetProxies()
  126.         {
  127.             Logger.Log("Getting all the proxies.", LogSeverity.Information);
  128.             return GetProxiesFromReader(ExecuteQuery("SELECT * FROM proxies.txt ORDER BY responsetime"));
  129.         }
  130.  
  131.         // na ez az ami nem működik, mert az oledb révén a fájl használatban van.
  132.         // viszont az oledb mezei egyszerűséggel nem támogatja a delete utasítást ...
  133.         internal static void RemoveProxy(Proxy proxy)
  134.         {
  135.             var proxies = File.ReadAllLines("proxies.txt").ToList();
  136.             for (int i = 0; i < proxies.Count; i++)
  137.             {
  138.                 if (proxies[i].Contains(proxy.ProxyIp))
  139.                 {
  140.                     proxies.RemoveAt(i);
  141.                     break;
  142.                 }
  143.             }
  144.             File.WriteAllLines("proxies.txt", proxies);
  145.         }
  146.  
  147.         private static List<Proxy> GetProxiesFromReader(IDataReader dataReader)
  148.         {
  149.             Logger.Log("Reading data from proxies.", LogSeverity.Information);
  150.             var proxies = new List<Proxy>();
  151.             while (dataReader.Read())
  152.             {
  153.                 proxies.Add(new Proxy(dataReader.GetString(0), int.Parse(dataReader.GetString(1)), int.Parse(dataReader.GetString(2))));
  154.             }
  155.             return proxies;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment