db_AI-4/csharp/marketconnectionNET2.0/db_AI-4_V3/db_AI-4/DataAccess.cs

113 lines
4.1 KiB
C#
Raw Normal View History

2017-07-26 18:18:37 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace db_AI_4
{
public class DataAccess
{
public SqlConnection connection { get; private set; }
//Read locations from the Database function start
public List<GetLocations> GetLocations(string LocationName)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("marketdb")))
{
var output = connection.Query<GetLocations>($"SELECT * FROM locations WHERE location_name = '{ LocationName }'").ToList();
return output;
}
}
//Read locations from the Database function end
//Registration function start
public void InsertMember(string Email_address, string Password)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("marketdb")))
{
List<GetMembers> members = new List<GetMembers>();
members.Add(new GetMembers { email_address = Email_address, password = Password });
connection.Execute("INSERT INTO dbo.members ( email_address, password)" +
" VALUES (@Email_address, @Password) ", members);
}
}
//Registration function end
// Insert Locations into Database start
2017-07-26 20:20:14 +02:00
public void InsertLocations(string StreetName, int LocationCapacity, string LocationName)
2017-07-26 18:18:37 +02:00
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("marketdb")))
{
List<GetLocations> locations = new List<GetLocations>();
locations.Add(new GetLocations { streetname = StreetName, location_capacity = LocationCapacity, location_name = LocationName });
connection.Execute("INSERT INTO dbo.locations (streetname, location_capacity, location_name)" +
2017-07-26 20:20:14 +02:00
" VALUES (@streetName, @location_capacity, @location_name)", locations);
2017-07-26 18:18:37 +02:00
}
}
// Insert Locations into Database end
2017-07-26 20:20:14 +02:00
// Insert Rents into Database start
public void InsertRent(string RentDate, string PaymentDate, int MemberID, int RentPriceID )
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("marketdb")))
{
List<GetRents> rents = new List<GetRents>();
rents.Add(new GetRents { rent_date = RentDate, payment_date = PaymentDate, member_id = MemberID, rent_price_id = RentPriceID, });
connection.Execute("INSERT INTO dbo.rents (rent_date, payment_date, member_id, rent_price_id)" +
" VALUES (@rent_date, @payment_date, @member_id, @rent_price_id )", rents);
}
}
// Insert Rents into Database end
2017-07-26 18:18:37 +02:00
//CheckLogin function start
public void CheckLogin(string Email_address, string Password)
{
SqlConnection con = new SqlConnection(Helper.CnnVal("marketdb"));
con.Open();
SqlCommand cmd = new SqlCommand($"SELECT * FROM dbo.members WHERE email_address = '{Email_address}' and password = '{Password}'",con);
SqlDataReader reader;
reader = cmd.ExecuteReader();
int count = 0;
while (reader.Read())
{
count += 1;
}
if (count == 1)
{
MessageBox.Show("It Worked");
Dashboard dashboard = new Dashboard();
dashboard.Show();
}
else if (count > 0)
{
MessageBox.Show("Already existing");
}
else
{
MessageBox.Show("username or passord not correct");
}
//CheckLogin function end
}
}
}