-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLClass.cs
More file actions
82 lines (70 loc) · 2.58 KB
/
SQLClass.cs
File metadata and controls
82 lines (70 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.IO;
using MySql.Data.MySqlClient;
using System.Data.Common;
using System.Windows.Forms;
namespace Booking3
{
public static class SQLClass
{
public const string CONNECTION_STRING =
"SslMode=none;Server=localhost;Database=booking3;port=3306;User Id=root";
public static MySqlConnection CONN;
/// <summary>
/// Таблица гостиниц
/// </summary>
public static string HOTELS = "hotels";
/// <summary>
/// Select-запрос. Возвращает список строк
/// </summary>
public static List<string> Select(string cmdText)
{
List<string> list = new List<string>();
try
{
MySqlCommand cmd = new MySqlCommand(cmdText, CONN);
DbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
list.Add(reader.GetValue(i).ToString());
}
reader.Close();
}
catch (Exception ex)
{
if (!File.Exists(Path.GetTempPath() + "/booking.txt"))
File.Create(Path.GetTempPath() + "/booking.txt");
File.AppendAllText(Path.GetTempPath() + "/booking.txt",
"Ошибка" + Environment.NewLine +
DateTime.Now.ToString() + Environment.NewLine +
ex.Message + " " + cmdText + Environment.NewLine + Environment.NewLine);
MessageBox.Show("Ошибка");
}
return list;
}
/// <summary>
/// Insert/Update/Delete-запрос
/// </summary>
public static void Update(string cmdText)
{
try
{
MySqlCommand cmd = new MySqlCommand(cmdText, CONN);
cmd.ExecuteReader();
cmd.Dispose();
}
catch (Exception ex)
{
if (!File.Exists(Path.GetTempPath() + "/booking.txt"))
File.Create(Path.GetTempPath() + "/booking.txt");
File.AppendAllText(Path.GetTempPath() + "/booking.txt",
"Ошибка" + Environment.NewLine +
DateTime.Now.ToString() + Environment.NewLine +
ex.Message + Environment.NewLine + Environment.NewLine);
MessageBox.Show("Ошибка");
}
}
}
}