-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.cs
65 lines (56 loc) · 2.13 KB
/
Database.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace AsxScraper
{
class Database
{
private Database()
{
}
private string databaseName = string.Empty;
public string DatabaseName
{
get { return databaseName; }
set { databaseName = value; }
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
private static Database _instance = null;
public static Database Instance()
{
if (_instance == null)
_instance = new Database();
return _instance;
}
public bool IsConnect()
{
if (Connection == null)
{
IDictionary<string, string> properties = new Dictionary<string, string>();
properties["server"] = (System.Configuration.ConfigurationManager.AppSettings["database_hostname"] ?? "localhost") + ":" + (System.Configuration.ConfigurationManager.AppSettings["database_port"] ?? "3306");
properties["database"] = System.Configuration.ConfigurationManager.AppSettings["database_dbname"] ?? "localhost";
properties["username"] = System.Configuration.ConfigurationManager.AppSettings["database_username"] ?? "root";
properties["password"] = System.Configuration.ConfigurationManager.AppSettings["database_password"] ?? "";
if (String.IsNullOrEmpty(databaseName))
return false;
string connstring = string.Format("Server={0}; database={1}; UID={2}; password={3}", properties["server"], properties["database"], properties["username"], properties["password"]);
connection = new MySqlConnection(connstring);
connection.Open();
}
return true;
}
public void Close()
{
connection.Close();
}
}
}