|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Data.SqlClient; |
| 4 | +using System.Data; |
3 | 5 | using System.Linq; |
4 | 6 | using System.Text; |
5 | 7 | using System.Threading.Tasks; |
| 8 | +using System.Runtime.CompilerServices; |
6 | 9 |
|
7 | 10 | namespace Blog_Dirty_ |
8 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// Class <c>PostsRepository</c> stores all data about posts in sql |
| 14 | + /// </summary> |
9 | 15 | public class RepositoryModel |
10 | 16 | { |
| 17 | + /// <summary> |
| 18 | + /// basic sql command |
| 19 | + /// </summary> |
| 20 | + private SqlCommand command; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Represents opening of the database |
| 24 | + /// </summary> |
| 25 | + private SqlConnection connection; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// defines where the database is |
| 29 | + /// </summary> |
| 30 | + private string databaseSource; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// executes commands as adapter |
| 34 | + /// </summary> |
| 35 | + private SqlDataAdapter adapter = new SqlDataAdapter(); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Constructor |
| 39 | + /// </summary> |
| 40 | + /// <param name="databaseSource"></param> |
| 41 | + public RepositoryModel(string databaseSource) |
| 42 | + { |
| 43 | + this.databaseSource = databaseSource; |
| 44 | + this.connection = new SqlConnection(); |
| 45 | + this.command = new SqlCommand(); |
| 46 | + this.adapter = new SqlDataAdapter(); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Destructor |
| 51 | + /// </summary> |
| 52 | + ~RepositoryModel() |
| 53 | + { |
| 54 | + closeDatabase(); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// returns connection to database |
| 59 | + /// </summary> |
| 60 | + /// <returns></returns> |
| 61 | + public SqlConnection getConnection() |
| 62 | + { |
| 63 | + return connection; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// opens/creates database |
| 68 | + /// </summary> |
| 69 | + public void createDatabase() |
| 70 | + { |
| 71 | + connection.ConnectionString = databaseSource; |
| 72 | + connection.Open(); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// closes database |
| 77 | + /// </summary> |
| 78 | + public void closeDatabase() |
| 79 | + { |
| 80 | + connection.Close(); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// assigns commands to adapter and run database through it |
| 85 | + /// </summary> |
| 86 | + /// <param name="tblName">Name of the table which will be changed</param> |
| 87 | + /// <param name="databaseCommands">strubg of sql commands that will be executed</param> |
| 88 | + /// <returns>Count of database</returns> |
| 89 | + public int executeDataAdapter(DataTable tblName, string databaseCommands) |
| 90 | + { |
| 91 | + if (connection.State == 0) |
| 92 | + { |
| 93 | + createDatabase(); |
| 94 | + } |
| 95 | + |
| 96 | + adapter.SelectCommand.CommandText = databaseCommands; |
| 97 | + adapter.SelectCommand.CommandType = CommandType.Text; |
| 98 | + SqlCommandBuilder DbCommandBuilder = new SqlCommandBuilder(adapter); |
| 99 | + |
| 100 | + string insert = DbCommandBuilder.GetInsertCommand().CommandText.ToString(); |
| 101 | + string update = DbCommandBuilder.GetUpdateCommand().CommandText.ToString(); |
| 102 | + string delete = DbCommandBuilder.GetDeleteCommand().CommandText.ToString(); |
| 103 | + |
| 104 | + closeDatabase(); |
| 105 | + return adapter.Update(tblName); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// reads data from commands that you wrote |
| 110 | + /// </summary> |
| 111 | + /// <param name="query">commands that read</param> |
| 112 | + /// <param name="tblName">Name of the table which will be read</param> |
| 113 | + public void readDataFromCommands(string query, DataTable tblName) |
| 114 | + { |
| 115 | + command.Connection = connection; |
| 116 | + command.CommandText = query; |
| 117 | + command.CommandType = CommandType.Text; |
| 118 | + |
| 119 | + adapter = new SqlDataAdapter(command); |
| 120 | + adapter.Fill(tblName); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// read data from commands without specifing table name |
| 125 | + /// </summary> |
| 126 | + /// <param name="query">commands that read</param> |
| 127 | + /// <returns></returns> |
| 128 | + private SqlDataReader readDataFromStream(string query) |
| 129 | + { |
| 130 | + SqlDataReader reader; |
| 131 | + |
| 132 | + command.Connection = connection; |
| 133 | + command.CommandText = query; |
| 134 | + command.CommandType = CommandType.Text; |
| 135 | + |
| 136 | + reader = command.ExecuteReader(); |
| 137 | + return reader; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// execute premade commands |
| 142 | + /// </summary> |
| 143 | + /// <param name="dbCommand"></param> |
| 144 | + /// <returns></returns> |
| 145 | + public int executeQuery(SqlCommand dbCommand) |
| 146 | + { |
| 147 | + dbCommand.Connection = connection; |
| 148 | + dbCommand.CommandType = CommandType.Text; |
| 149 | + |
| 150 | + return dbCommand.ExecuteNonQuery(); |
| 151 | + } |
11 | 152 | } |
12 | 153 | } |
0 commit comments