· 5 years ago · Aug 29, 2020, 08:46 AM
1using System;
2using System.Data.SqlClient;
3
4namespace AssecoSolution
5{
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 AssecoSolution ass = new AssecoSolution();
11 ass.Connect();
12 if(!ass.CheckDatabase("TestDatabase"))
13 {
14 ass.CreateDatabase("TestDatabase");
15 ass.CreateTable("TestDatabase", "TestTable");
16 ass.ChangeDatabase("TestDatabase");
17 }
18
19 ass.InsertRow("TestDatabase", "TestTable");
20 }
21 }
22
23 public class AssecoSolution
24 {
25 SqlConnection SqlConnection = null;
26 string ConnectionString = string.Empty;
27
28 public void Connect()
29 {
30 try
31 {
32 SqlConnection = CreateConnection(GetConnectionString());
33 }
34 catch (Exception)
35 {
36 throw;
37 }
38 }
39
40 public void Connect(string ConnectionString)
41 {
42 try
43 {
44 SqlConnection = CreateConnection(ConnectionString);
45 }
46 catch (Exception)
47 {
48 throw;
49 }
50 }
51
52 SqlConnection CreateConnection(string connectionString)
53 {
54 return new SqlConnection(connectionString);
55 }
56
57 string GetConnectionString()
58 {
59 //gets connection string from config file
60 string connectionString = "Server = 192.168.1.122; User ID = sa; Password = 16SunSoft88";
61 return connectionString;
62 }
63
64 public bool CheckDatabase(string database)
65 {
66 using (SqlCommand SqlCommand = new SqlCommand("SELECT COUNT(*) FROM sys.sysdatabases where name = @database", SqlConnection))
67 {
68 SqlCommand.Parameters.Add("@database", System.Data.SqlDbType.NVarChar).Value = database;
69 SqlConnection.Open();
70 bool exists = (Convert.ToInt32(SqlCommand.ExecuteScalar()) == 1);
71 SqlConnection.Close();
72
73 return exists;
74 }
75 }
76
77 public void CreateDatabase(string database)
78 {
79 using (SqlCommand SqlCommand = new SqlCommand(string.Format("CREATE DATABASE {0}", database), SqlConnection))
80 {
81 try
82 {
83 SqlConnection.Open();
84 SqlCommand.ExecuteNonQuery();
85 SqlConnection.Close();
86 }
87 catch (Exception)
88 {
89 throw;
90 }
91 }
92 }
93
94 public void ChangeDatabase(string database)
95 {
96 if (SqlConnection == null)
97 {
98 throw new Exception("Program isn't connected to any database");
99 }
100
101 if (!CheckDatabase(database))
102 {
103 CreateDatabase(database);
104 }
105
106 SqlConnection.ChangeDatabase(database);
107 }
108
109 public bool CheckTable(string database, string table)
110 {
111 using (SqlCommand SqlCommand = new SqlCommand("SELECT COUNT(*) FROM @database.tables where table_name = @table", SqlConnection))
112 {
113 SqlCommand.Parameters.Add("@database", System.Data.SqlDbType.NVarChar).Value = database;
114 SqlCommand.Parameters.Add("@table", System.Data.SqlDbType.NVarChar).Value = table;
115 SqlConnection.Open();
116 bool exists = (Convert.ToInt32(SqlCommand.ExecuteScalar()) == 1);
117 SqlConnection.Close();
118
119 return exists;
120 }
121 }
122
123 public void CreateTable(string database, string table)
124 {
125 using (SqlCommand SqlCommand = new SqlCommand(string.Format("CREATE TABLE {0}.{1} (rowID int NOT NULL AUTO_INCREMENT, PRIMARY KEY (rowID))", database, table), SqlConnection))
126 {
127 try
128 {
129 SqlConnection.Open();
130 SqlCommand.ExecuteNonQuery();
131 SqlConnection.Close();
132 }
133 catch (Exception)
134 {
135 throw;
136 }
137 }
138 }
139
140 public void InsertRow(string database, string table)
141 {
142 using (SqlCommand SqlCommand = new SqlCommand("INSERT INTO @database.@table", SqlConnection))
143 {
144 SqlCommand.Parameters.Add("@database", System.Data.SqlDbType.NVarChar).Value = database;
145 SqlCommand.Parameters.Add("@table", System.Data.SqlDbType.NVarChar).Value = table;
146 SqlConnection.Open();
147 SqlCommand.ExecuteNonQuery();
148 SqlConnection.Close();
149 }
150 }
151 }
152
153 public enum ConnectionState
154 {
155 Closed = 0,
156 Connected = 1
157 }
158}