-
Notifications
You must be signed in to change notification settings - Fork 7
/
UserDAO.cs
193 lines (143 loc) · 5.64 KB
/
UserDAO.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace ApteanEdgeBank
{
class UserDAO
{
public static string connectionString = "Data Source=WS003LT1553PRD;Initial Catalog=ApteanEdgeBank;User=sa;Password=abc-123";
SqlConnection conn = null;
string myQuery = null;
SqlCommand cmd = null;
// SqlDataReader resultSet = null;
DataTable dt = new DataTable();
public void getConnection()
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
System.Windows.Forms.MessageBox.Show("Connection Successfull");
}
catch (SqlException e )
{
System.Windows.Forms.MessageBox.Show("Exception"+e);
}
} //returns Connection successfull message box on successfull connection
public DataTable ListCustomers()
{
try
{
getConnection();
myQuery = "select FirstName,LastName from Customer order by FirstName ";
cmd = new SqlCommand(myQuery, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("General Exception"+e);
}
return dt;
} // returns DataTable containing List of customers FirstName and LastName
public bool AddNewCustomer(string Fname, string Lname, string dateOfBirth) //first and last three are null
{
try
{
myQuery = "use ApteanEdgeBank insert into customer (FirstName,LastName,DateOfJoining,DateOfBirth,MiddleName) values(" +"'"+ Fname + "'"+","+"'" + Lname +"'"+ "," + "cast(getdate() as date)" + "," +"'"+ dateOfBirth+"'" + ")"; //insert into Customer (FirstName,LastName,DateOfJoining,DateOfBirth)
InsertData(myQuery, connectionString);
}
catch (SqlException ex)
{
System.Windows.Forms.MessageBox.Show("Sql Exception"+ex.Message);
return false;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("General Exception" + ex.Message);
return false;
}
return true;
}
public DataTable GetData(string myQuery,string connectionString) // data returned for the myQery will be returned as a DataTable
{
try
{
//create connection
SqlConnection myConnection = new SqlConnection(connectionString);
//Create the command and tell you will provide a SQL query
SqlCommand cmd = new SqlCommand(myQuery, myConnection);
//create adapter
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
//open the connection
myConnection.Open();
//fill data in command
myAdapter.Fill(dt);
//close the connection
myConnection.Close();
//return the dataTable
return dt;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return dt;
}
}
public void InsertData(string myQuery, string connectionString)
{
//create connection
try
{
SqlConnection myConnection = new SqlConnection(connectionString);
//Create the command and tell you will provide a SQL query
SqlCommand cmd = new SqlCommand(myQuery, myConnection);
//create adapter
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
//open the connection
myConnection.Open();
//fill data in command
myAdapter.Fill(dt);
//close the connection
myConnection.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
public void UpdateData(string myQuery, string connectionString)
{
try
{
SqlConnection myConnection = new SqlConnection(connectionString);
//Create the command and tell you will provide a SQL query
SqlCommand cmd = new SqlCommand(myQuery, myConnection);
//create adapter
//int rows = cmd.ExecuteNonQuery();
// SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
//open the connection
myConnection.Open();
int rows = cmd.ExecuteNonQuery();
if (rows <= 0)
{
MessageBox.Show("Update unsuccessful!");
}
//fill data in command
//myAdapter.Fill(dt);
//close the connection
myConnection.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}