-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbAccess.cs
201 lines (179 loc) · 5.27 KB
/
DbAccess.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
namespace HRM
{
public class DbAccess
{
public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
public static SqlCommand cmd; public static SqlDataReader reader;
public static SqlConnection connnection = new SqlConnection(connectionString);
public static bool recordExist(string sSQL)
{
SqlCommand cm = new SqlCommand();
SqlConnection cn = new SqlConnection(connectionString);
try
{
cn.Open();
cm.Connection = cn;
cm.CommandText = sSQL;
SqlDataReader reader;
reader = cm.ExecuteReader();
if (reader.Read())
{ return true; }
else { return false; }
}
catch (System.Exception e)
{
MessageBox.Show("" + e.ToString());
return false;
}
finally
{
cn.Close();
}
}
public static Boolean ExecuteToDB(string sSQL)
{
Boolean Result = false;
SqlCommand cm = new SqlCommand();
SqlConnection cn = new SqlConnection(connectionString);
try
{
cn.Open();
cm.Connection = cn;
cm.CommandText = sSQL;
Result = cm.ExecuteNonQuery() > 0 ? true : false;
}
catch (System.Exception e)
{
MessageBox.Show("" + e.ToString());
return false;
}
finally
{
cn.Close();
}
return Result;
}
public static SqlDataReader GetFromDB(string sSQL)
{
try
{
connnection.Open();
cmd = new SqlCommand(sSQL, connnection);
reader = cmd.ExecuteReader();
}
catch (System.Exception e)
{
MessageBox.Show("" + e.ToString());
return reader;
}
return reader;
}
public static int NumberOfRecords(string sSQL)
{
SqlDataReader reader = null; int count = 0;
SqlCommand cm = new SqlCommand();
SqlConnection cn = new SqlConnection(connectionString);
try
{
cn.Open();
cm.Connection = cn;
cm.CommandText = sSQL;
reader = cm.ExecuteReader();
if (reader.HasRows)
{
if (reader.Read())
{
count = Convert.ToInt32(reader["Count"]);
}
else
{
count = 0;
}
}
else
{
count = 0;
}
}
catch (System.Exception e)
{
MessageBox.Show("" + e.ToString());
return 0;
}
finally
{
cn.Close();
}
return count;
}
public static bool IsDate(string sdate)
{
DateTime dt;
bool isDate = true;
try
{
dt = DateTime.Parse(sdate);
}
catch
{
isDate = false;
}
return isDate;
}
public static bool CheckDouble(string Sstring)
{
bool NumberCheck = false;
double CheckDouble = 0;
try
{
NumberCheck = double.TryParse(Sstring, out CheckDouble);
}
catch
{
NumberCheck = false;
}
return NumberCheck;
}
public int MaxRecord(string sSQL)
{
string connectionStr = DbAccess.connectionString;
SqlCommand cm = new SqlCommand();
SqlConnection cn = new SqlConnection(connectionStr);
int mr = 0;
try
{
cn.Open();
cm.Connection = cn;
cm.CommandText = sSQL;
SqlDataReader reader;
reader = cm.ExecuteReader();
while (reader.Read())
{
mr = Convert.ToInt32(reader[0].ToString());
}
//SqlDataAdapter ADAP = new SqlDataAdapter(sSQL, cn);
//DataSet DS = new DataSet();
//ADAP.Fill(DS, sTable);
//mr = Convert.ToInt32(DS.Tables[sTable].Rows[0].ItemArray.GetValue(0).ToString());
}
catch (System.Exception e)
{
MessageBox.Show("" + e.ToString());
}
finally
{
cn.Close();
}
return mr;
}
}
}