Advertisement
Guest User

Untitled

a guest
May 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using System;
  2. using System.Data.SQLite;
  3. using System.IO;
  4.  
  5. namespace Sqlite
  6. {
  7. public class Program
  8. {
  9. private static string dir = Path.Combine(Environment
  10. .GetFolderPath(Environment.SpecialFolder.ApplicationData), "Peppemess");
  11. private static string databaseName = Path.Combine(dir, "PeppemessDB.sqlite");
  12.  
  13. static void Main(string[] args)
  14. {
  15. CreateDatabase();
  16. CreateQuote("Quote", "Author");
  17. CreatePhonenumber("012345");
  18. }
  19.  
  20. private static void CreateQuote(string quote, string author)
  21. {
  22. using (var dbConnection = new SQLiteConnection("Data Source=" + databaseName))
  23. {
  24. dbConnection.Open();
  25.  
  26. var sqlCommand = "INSERT INTO Quotes(Quote, Author) VALUES('" + quote + "', '" + author + "')";
  27.  
  28. using (SQLiteCommand command = new SQLiteCommand(sqlCommand, dbConnection))
  29. {
  30. command.ExecuteNonQuery();
  31. dbConnection.Close();
  32. }
  33. }
  34. }
  35. private static void CreatePhonenumber(string phoneNumber)
  36. {
  37. using (var dbConnection = new SQLiteConnection("Data Source=" + databaseName))
  38. {
  39. dbConnection.Open();
  40.  
  41. var sqlCommand = "INSERT INTO Phonenumbers(Phonenumber, DateCreated, IsDeleted) VALUES('" + phoneNumber + "', '" + DateTime.Now + "', false)";
  42.  
  43. using (SQLiteCommand command = new SQLiteCommand(sqlCommand, dbConnection))
  44. {
  45. command.ExecuteNonQuery();
  46. dbConnection.Close();
  47. }
  48. }
  49. }
  50.  
  51.  
  52. private static void CreateDatabase()
  53. {
  54. // Create system result database
  55. if (!Directory.Exists(dir))
  56. Directory.CreateDirectory(dir);
  57. SQLiteConnection.CreateFile(databaseName);
  58.  
  59. // Connect to system result database
  60. using (var dbConnection = new SQLiteConnection("Data Source=" + databaseName))
  61. {
  62. dbConnection.Open();
  63.  
  64. using (var transaction = dbConnection.BeginTransaction())
  65. {
  66. // Create table
  67. //Spara i en tabell: telefonnummer, utskickstid, namn, sign-up-tid, isdeleted
  68. var sqlCommand = "CREATE TABLE Phonenumbers (Phonenumber NVARCHAR(450) PRIMARY KEY, " +
  69. "DateCreated DATETIME, " +
  70. "TimeToSend DATETIME, " +
  71. "Name NVARCHAR(450)," +
  72. "IsDeleted bit)";
  73.  
  74. using (SQLiteCommand command = new SQLiteCommand(sqlCommand, dbConnection))
  75. {
  76. command.ExecuteNonQuery();
  77. }
  78.  
  79. sqlCommand = "CREATE TABLE Quotes (ID INTEGER PRIMARY KEY, Quote nvarchar(140), Author nvarchar(20), SendTime DATETIME)";
  80.  
  81. using (SQLiteCommand command = new SQLiteCommand(sqlCommand, dbConnection))
  82. {
  83. command.ExecuteNonQuery();
  84. }
  85.  
  86. transaction.Commit();
  87. }
  88.  
  89. dbConnection.Close();
  90. }
  91. }
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement