CNSA-212-FP/Louis'-Pharmacy_CNSA212-FP/PharmacyDataTier.cs
2024-02-19 10:47:16 -05:00

625 lines
21 KiB
C#

using System;
using System.Configuration;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.Versioning;
namespace Louis__Pharmacy_CNSA212_FP
{
public class PharmacyDataTier
{
static String connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
static SqlConnection myConn = new SqlConnection(connString);
static System.Data.SqlClient.SqlCommand cmdString = new System.Data.SqlClient.SqlCommand();
public static void UpdatePatient(
string Patient_id,
string FirstName,
string LastName,
string MiddleIntials,
int lbs,
int Height_feet,
int Height_inches,
DateTime DOB,
string Gender,
string City,
Int16 Zip,
string UsState,
string PhoneNumber)
{
try
{
myConn.Open();
cmdString.Parameters.Clear();
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "UpdatePatient";
cmdString.Parameters.Add("@Patient_id", SqlDbType.VarChar, 8).Value = Patient_id;
cmdString.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value = FirstName;
cmdString.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value = LastName;
cmdString.Parameters.Add("@MiddleIntials", SqlDbType.VarChar).Value = MiddleIntials;
cmdString.Parameters.Add("@lbs", SqlDbType.Int).Value = lbs;
cmdString.Parameters.Add("@Height_feet", SqlDbType.Int).Value = Height_feet;
cmdString.Parameters.Add("@Height_inches", SqlDbType.Int).Value = Height_inches;
cmdString.Parameters.Add("@DOB", SqlDbType.DateTime).Value = DOB;
cmdString.Parameters.Add("@Gender", SqlDbType.Char).Value = Gender;
cmdString.Parameters.Add("@City", SqlDbType.VarChar, 30).Value = City;
cmdString.Parameters.Add("@Zip", SqlDbType.SmallInt).Value = Zip;
cmdString.Parameters.Add("@UsState", SqlDbType.Char, 2).Value = UsState;
cmdString.Parameters.Add("@PhoneNumber", SqlDbType.Char,14).Value = PhoneNumber;
cmdString.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static void CreatePatient(
string Patient_id,
string FirstName,
string LastName,
string MiddleIntials,
int lbs,
int Height_feet,
int Height_inches,
DateTime DOB,
string Gender,
string City,
Int16 Zip,
string UsState,
string PhoneNumber)
{
try
{
myConn.Open();
cmdString.Parameters.Clear();
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "CreatePatient";
cmdString.Parameters.Add("@Patient_id", SqlDbType.VarChar, 8).Value = Patient_id;
cmdString.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value = FirstName;
cmdString.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value = LastName;
cmdString.Parameters.Add("@MiddleIntials", SqlDbType.VarChar).Value = MiddleIntials;
cmdString.Parameters.Add("@lbs", SqlDbType.Int).Value = lbs;
cmdString.Parameters.Add("@Height_feet", SqlDbType.Int).Value = Height_feet;
cmdString.Parameters.Add("@Height_inches", SqlDbType.Int).Value = Height_inches;
cmdString.Parameters.Add("@DOB", SqlDbType.DateTime).Value = DOB;
cmdString.Parameters.Add("@Gender", SqlDbType.Char).Value = Gender;
cmdString.Parameters.Add("@City", SqlDbType.VarChar, 30).Value = City;
cmdString.Parameters.Add("@Zip", SqlDbType.SmallInt).Value = Zip;
cmdString.Parameters.Add("@UsState", SqlDbType.Char, 2).Value = UsState;
cmdString.Parameters.Add("@PhoneNumber", SqlDbType.Char,14).Value = PhoneNumber;
cmdString.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static void DeletePatient(string Patient_id)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "DeletePatient";
// Define input parameter
cmdString.Parameters.Add("@Patient_id", SqlDbType.VarChar, 8).Value = Patient_id;
cmdString.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet PatientInfoSearch(string id, string lname, string fname)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "SearchPatients";
// Define input parameter
cmdString.Parameters.Add("@ID", SqlDbType.VarChar, 8).Value = id;
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 30).Value = lname;
cmdString.Parameters.Add("@fname", SqlDbType.VarChar, 30).Value = fname;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet PatientInfoSearch(string id)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "SearchPatients";
// Define input parameter
cmdString.Parameters.Add("@ID", SqlDbType.VarChar, 8).Value = id;
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 30).Value = "";
cmdString.Parameters.Add("@fname", SqlDbType.VarChar, 30).Value = "";
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet PrescriptionInfoSearch(string rxID, string patientID)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "PerscriptionInfoSearch";
// Define input parameter
cmdString.Parameters.Add("@rxID", SqlDbType.VarChar, 11).Value = rxID;
cmdString.Parameters.Add("@patientID", SqlDbType.VarChar, 8).Value = patientID;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet PhysicianInfoSearch(string fname,string lname, string phyID)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "PhysicianInfoSearch";
// Define input parameter
cmdString.Parameters.Add("@fname", SqlDbType.VarChar, 11).Value = fname;
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 8).Value = lname;
cmdString.Parameters.Add("@phyID", SqlDbType.VarChar, 8).Value = phyID;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet PhysicianInfoSearch(string phyID)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "PhysicianInfoSearch";
// Define input parameter
cmdString.Parameters.Add("@fname", SqlDbType.VarChar, 11).Value = "";
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 8).Value = "";
cmdString.Parameters.Add("@phyID", SqlDbType.VarChar, 8).Value = phyID;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static double GetNextPatientID()
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "GetNextPatientID";
// Define input parameter
cmdString.Parameters.Add("@TableName", SqlDbType.NVarChar, 128).Value = "PATIENT";
object result = cmdString.ExecuteScalar();
double value = 0;
try
{
value = Convert.ToDouble(result);
}
catch(Exception e)
{
MessageBox.Show("Error Getting next Patient ID","ERROR",MessageBoxButtons.OK);
}
// return dataSet
return value;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static double GetNextPhysicianID()
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "GetNextPhysicianID";
object result = cmdString.ExecuteScalar();
double value = 0;
try
{
value = Convert.ToDouble(result);
}
catch(Exception e)
{
MessageBox.Show("Error Getting next Patient ID","ERROR",MessageBoxButtons.OK);
}
// return dataSet
return value;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet RefillSearch(DateTime date, string id)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "SearchRefills";
// Define input parameter
cmdString.Parameters.Add("@date", SqlDbType.Date).Value = date;
cmdString.Parameters.Add("@ID", SqlDbType.VarChar, 10).Value = id;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static DataSet RefillSearch(string id)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "SearchRefills2";
// Define input parameter
cmdString.Parameters.Add("@ID", SqlDbType.VarChar, 10).Value = id;
//cmdString.Parameters.Add("@date", SqlDbType.Date).Value = "";
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static double GetNextRefillID()
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "GetNextRefillID";
// Define input parameter
cmdString.Parameters.Add("@TableName", SqlDbType.NVarChar, 128).Value = "Refill";
object result = cmdString.ExecuteScalar();
double value = 0;
try
{
value = Convert.ToDouble(result);
}
catch (Exception e)
{
MessageBox.Show("Error Getting next Refill ID", "ERROR", MessageBoxButtons.OK);
}
// return dataSet
return value;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static void AddRefill(string refillID, DateTime date, string patientID, string medicationID, string rxNum)
{
try
{
myConn.Open();
cmdString.Parameters.Clear();
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "AddNewRefill";
cmdString.Parameters.Add("@refillID", SqlDbType.Char, 10).Value = refillID;
cmdString.Parameters.Add("@date", SqlDbType.Date).Value = date;
cmdString.Parameters.Add("@patientID", SqlDbType.VarChar, 8).Value = patientID;
cmdString.Parameters.Add("@medicationID", SqlDbType.VarChar, 7).Value = medicationID;
cmdString.Parameters.Add("@rxNum", SqlDbType.VarChar, 11).Value = rxNum;
cmdString.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public static void UpdateRefill(string refillID, DateTime date, string patientID, string medicationID, string rxNum)
{
try
{
myConn.Open();
cmdString.Parameters.Clear();
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "UpdateRefills";
cmdString.Parameters.Add("@refillID", SqlDbType.Char, 10).Value = refillID;
cmdString.Parameters.Add("@date", SqlDbType.Date).Value = date;
cmdString.Parameters.Add("@patientID", SqlDbType.VarChar, 8).Value = patientID;
cmdString.Parameters.Add("@medicationID", SqlDbType.VarChar, 7).Value = medicationID;
cmdString.Parameters.Add("@rxNum", SqlDbType.VarChar, 11).Value = rxNum;
cmdString.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
}
}