-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsurance.cs
More file actions
226 lines (202 loc) · 10.1 KB
/
Insurance.cs
File metadata and controls
226 lines (202 loc) · 10.1 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace ConsoleApp4
{
public class Insurance
{
// Creates the "Customer" table in the SQL Server database.
public void Create_Table_Customer()
{
// Connection string to connect to the SQL Server database.
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
// Using statement to open a connection to the database.
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// String containing the SQL query to create the "Customer" table.
string conString = "CREATE TABLE Customer(\r\n CustomerID varchar(50) PRIMARY KEY,\r\n CustomerName varchar(50) NOT NULL,\r\n email varchar(50) UNIQUE NOT NULL,\r\n password varchar(30) NOT NULL,\r\n Address varchar(100) UNIQUE NOT NULL,\r\n Contact varchar(50) UNIQUE NOT NULL,\r\n Nominee varchar(50) NOT NULL,\r\n Relationship varchar(50) NOT NULL\r\n);";
// Using statement to create a new SQL Command with the SQL query and the connection.
using (SqlCommand cmd = new SqlCommand(conString, connection))
{
// Executing the SQL query
cmd.ExecuteNonQuery();
}
}
Console.WriteLine("Customer Table Created Successfully");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
// Creating Policy Table
public void Create_Table_Policy()
{
// Connection string to connect to the SQL Server database
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
// Creating a SqlConnection object using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Opening the connection to the database
connection.Open();
// SQL query to create the Policy table
string conString = "CREATE TABLE Policy(\r\n CustomerID varchar(50) ,\r\n policy_Id varchar(50) ,\r\n policy_type varchar(50) ,\r\n start_dates varchar(50) ,\r\n sum_assured varchar(50) ,\r\n premium varchar(50) ,\r\n paying_term varchar(50) ,\r\n title varchar(50) ,\r\n nextdue varchar(50) ,\r\n FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)\r\n);";
// Creating a SqlCommand object using the SQL query and the SqlConnection object
using (SqlCommand cmd = new SqlCommand(conString, connection))
{
// Executing the SQL query
cmd.ExecuteNonQuery();
}
}
// Writing a success message if the Policy table was created successfully
Console.WriteLine("Policy Table Created Successfully");
}
catch (Exception e)
{
// Writing the error message if an exception occurred
Console.WriteLine(e.Message);
}
}
// Adding Customer details to Customer table
public int AddCustomer(string c_id, string c_na, string e_Ma, string p_a, string c_ad, string n_um, string n_o, string r_e)
{
int rows = 0;
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string conString = "INSERT INTO Customer VALUES (@c_id, @c_na, @e_Ma, @p_a, @c_ad, @n_um, @n_o, @r_e)";
using (SqlCommand cmd = new SqlCommand(conString, connection))
{
cmd.Parameters.AddWithValue("@c_id", c_id);
cmd.Parameters.AddWithValue("@c_na", c_na);
cmd.Parameters.AddWithValue("@e_Ma", e_Ma);
cmd.Parameters.AddWithValue("@p_a", p_a);
cmd.Parameters.AddWithValue("@c_ad", c_ad);
cmd.Parameters.AddWithValue("@n_um", n_um);
cmd.Parameters.AddWithValue("@n_o", n_o);
cmd.Parameters.AddWithValue("@r_e", r_e);
rows = cmd.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
// Console.WriteLine(e.Message);
}
return rows;
}
// Adding Policy details to Policy table
public int AddPolicy(string c_id, string p_nu, string p_ty, string d, string s_um, string p_um, string p_t, string t, string nt)
{
int rows = 0;
try
{
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
string conString = string.Format("INSERT INTO Policy VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')", c_id, p_nu, p_ty, d, s_um, p_um, p_t, t, nt);
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(conString, connection);
connection.Open();
rows = cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return rows;
}
// Reading all customer & policy details
public List<Customer> fetchCustomer()
{
List<Customer> list = new List<Customer>();
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT Customer.CustomerID, Customer.CustomerName, Policy.Policy_type, Policy.title, Policy.Premium, Policy.Sum_assured, \r\nCustomer.Nominee, Customer.email, Customer.Contact, nextdue FROM Policy FULL OUTER JOIN Customer ON Customer.CustomerID = Policy.CustomerID;";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Customer c = new Customer(Convert.ToInt32(reader[0]), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), Convert.ToInt32(reader[0]), reader[9].ToString());
list.Add(c);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return list;
}
// Reading specific customer & policy details
public List<Customer> fetchCustomer(string cust_id)
{
List<Customer> list = new List<Customer>();
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT Customer.CustomerID, Customer.CustomerName, Policy.Policy_type, Policy.title, Policy.Premium, Policy.Sum_assured, \r\nCustomer.Nominee, Customer.email, Customer.Contact, nextdue FROM Policy FULL OUTER JOIN Customer ON Customer.CustomerID = Policy.CustomerID WHERE Customer.CustomerID = '" + cust_id + "';";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Customer c = new Customer(Convert.ToInt32(reader[0]), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), Convert.ToInt32(reader[0]), reader[9].ToString());
list.Add(c);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return list;
}
// Reading Specific customer id
public string fetchCustomerid(string id)
{
string i_d = null;
string connectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT CustomerID FROM Customer where CustomerID = '" + id + "'";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
i_d = Convert.ToString(reader[0].ToString());
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return i_d;
}
}
}