Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Connection.h"
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <Windows.h>
- using namespace std;
- #import "C:\Program Files\Common Files\System\ado\msado15.dll" \
- rename("EOF", "EndOfFile")
- ADODB::_ConnectionPtr ConnDB = NULL;
- ADODB::_RecordsetPtr EmpRec = NULL;
- Connection::Connection()
- {
- }
- Connection::~Connection()
- {
- }
- //Function to display error in case database fails
- void Connection::error(_com_error e, char* displayErr)
- {
- sprintf_s(displayErr, 800, " Error Details: \n");
- sprintf_s(displayErr, 800, "%s Code Message: %s\n", displayErr, (char *)e.ErrorMessage());
- sprintf_s(displayErr, 800, "%s Error Source: %s\n", displayErr, (char *)e.Source());
- sprintf_s(displayErr, 800, "%s Error Description: %s\n", displayErr, (char *)e.Description());
- return;
- }
- void Connection::connect(char choice)
- {
- HRESULT hr;
- int count;
- char option;
- int option2;
- char ErrStr[800];
- char QueryStr[300];
- char NewQueryStr[400];
- string col1;
- string col2;
- string col3;
- string col4;
- string col5;
- string col6;
- string col7;
- string col8;
- string col9;
- string col10;
- string col11;
- _variant_t vtValue;
- int id;
- char Id[5];
- char FName[16];
- char LName[16];
- string add;
- char Address[26];
- string city;
- char City[21];
- char State[3];
- char Zip[6];
- char Assoc[11];
- char Email[26];
- char Phone[16];
- int Year;
- ::CoInitialize(NULL);
- try
- {
- hr = ConnDB.CreateInstance(__uuidof(ADODB::Connection));
- ConnDB->CursorLocation = ADODB::adUseClient;
- ConnDB->Open("Provider = SQLOLEDB; Server=cstnt.tstc.edu; Database = ITSE1392", "1392Stu", "1392Pass", NULL);
- //If statement to check if the choice equals 6 or exit on menu
- if (choice != '6')
- {
- cout << "1. Continue \n";
- cout << "2. Return to Main \n";
- cout << "Please make a selection from the menu: ";
- cin >> option;
- //
- if (option == '1')
- {
- system("cls");
- switch (choice)
- {
- case '1':
- cout << "1. Search by Employee ID \n";
- cout << "2. Search by Member Year \n";
- cout << "3. Search by Association \n";
- cout << "4. Return \n";
- cout << "Please make a selection from the menu: ";
- cin >> option2;
- if (option2 == '1')
- {
- //Search function with parameter ID
- Search("Id");
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- }
- else if (option2 == '2')
- {
- //Search function with parameter ID
- Search("Year");
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- }
- else if (option2 == '3')
- {
- //Search function with parameter ID
- Search("Assoc");
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- }
- else if (option2 == '4')
- {
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- break;
- }
- break;
- case '2':
- try
- {
- //Gets the maxID
- id = maxId();
- //cout << "DB Connected: " << endl;
- bool valid = false;
- //Gets the User inputs
- cout << "Add New Employee: \n\n";
- cout << "First Name: ";
- cin >> FName;
- cout << "Last Name: ";
- cin >> LName;
- cout << "Address: ";
- cin.ignore();
- getline(cin, add);
- strncpy_s(Address, add.c_str(), sizeof(Address));
- Address[sizeof(Address) - 1] = 0;
- cout << "City: ";
- getline(cin, city);
- strncpy_s(City, city.c_str(), sizeof(City));
- City[sizeof(City) - 1] = 0;
- cout << "State: ";
- cin >> State;
- cout << "Zip: ";
- cin >> Zip;
- cout << "Association: ";
- cin >> Assoc;
- cout << "Email: ";
- cin >> Email;
- cout << "Phone(###-###-####): ";
- cin >> Phone;
- cout << "Member Year: ";
- cin >> Year;
- //while (!valid)
- //{
- // valid = true; //Assume the cin will be an integer.
- // cout << "Please enter the Member Year: " << endl;
- // cin >> Year;
- // if (cin.fail()) //cin.fail() checks to see if the value in the cin
- // //stream is the correct type, if not it returns true,
- // //false otherwise.
- // {
- // cin.clear(); //This corrects the stream.
- // cin.ignore(); //This skips the left over stream data.
- // //cout << "Please enter an number only." << endl;
- // valid = false; //The cin was not an integer so try again.
- // }
- //}
- cin.ignore();
- //Insert Query
- sprintf_s(NewQueryStr, 400, "INSERT INTO Employee VALUES(%i, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %i);", id, FName, LName, Address, City, State, Zip, Assoc, Email, Phone, Year);
- //Executes the query
- EmpRec = ConnDB->Execute(NewQueryStr, NULL, 1);
- }
- catch (exception ex)
- {
- cout << endl;
- cout << "Incorrect data entered, Press any key to return to the menu... " << endl;
- cin.get();
- system("cls");
- }
- cout << "Employee Added!!!\n\n";
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- break;
- case '3':
- cout << "Update Employee: \n\n";
- cout << "Enter the id of the employee to update: ";
- cin >> id;
- cin.ignore();
- cout << endl;
- cout << "1. First Name " << endl;
- cout << "2. Last Name " << endl;
- cout << "3. Address " << endl;
- cout << "4. City " << endl;
- cout << "5. State " << endl;
- cout << "6. Zip " << endl;
- cout << "7. Association " << endl;
- cout << "8. Email " << endl;
- cout << "9. Phone(###-###-####) " << endl;
- cout << "10. Member Year " << endl;
- cout << "11. ALL " << endl;
- cout << "12. Return to Main " << endl;
- cin >> option2;
- if (option2 == '1')
- {
- }
- else if (option2 == '2')
- {
- }
- else if (option2 == '3')
- {
- }
- else if (option2 == '4')
- {
- }
- else if (option2 == '5')
- {
- }
- else if (option2 == '6')
- {
- }
- else if (option2 == '7')
- {
- }
- else if (option2 == '8')
- {
- }
- else if (option2 == '9')
- {
- }
- else if (option2 == '10')
- {
- }
- else if (option2 == '11')
- {
- }
- else if (option2 == 12)
- {
- cin.ignore();
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- }
- //cin.ignore();
- ////Insert Query
- //sprintf_s(NewQueryStr, 400, "INSERT INTO Employee VALUES(%i, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %i);", id, FName, LName, Address, City, State, Zip, Assoc, Email, Phone, Year);
- ////Executes the query
- //EmpRec = ConnDB->Execute(NewQueryStr, NULL, 1);
- break;
- case '4':
- //cout << "DB Connected: " << endl;
- cout << "Delete Employee: \n\n";
- cout << "Enter the id of the employee to delete: ";
- cin >> id;
- cin.ignore();
- //Delete Query
- sprintf_s(NewQueryStr, 400, "DELETE FROM Employee WHERE EmployeeID = %i", id);
- //Executes the query
- EmpRec = ConnDB->Execute(NewQueryStr, NULL, 1);
- cout << "Employee Deleted!!!\n\n";
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- break;
- case '5':
- cin.ignore();
- //cout << " DB Connected:" << endl;
- //Search funtion with parameter All
- Search("All");
- //Return to the menu
- cout << endl;
- cout << " Press any key to return to the menu..." << endl;
- cin.get();
- system("cls");
- break;
- }
- }
- else if (option == '2')
- {
- //if option 2 was pressed
- cin.get();
- system("cls");
- }
- else
- {
- //if an invalid option was pressed
- cout << "Invalid token, returning to the main menu...";
- cin.ignore();
- cin.get();
- system("cls");
- }
- }
- else //if option 6 is chosen
- {
- //Exits the program
- exit;
- }
- }
- catch (const _com_error &e)
- {
- system("cls");
- system("\a");
- cout << " DB Connection Error: " << endl << endl;
- error(e, ErrStr);
- printf(ErrStr);
- cout << endl;
- }
- CoUninitialize();
- }
- int Connection::maxId()
- {
- _variant_t vtValue;
- string col1;
- int scope;
- char QueryStr[200];
- sprintf_s(QueryStr, 200, "SELECT MAX(EmployeeID) as MaxCount FROM Employee");
- EmpRec = ConnDB->Execute(QueryStr, NULL, 1);
- vtValue = EmpRec->Fields->GetItem("MaxCount")->GetValue();
- col1 = string((char*)_bstr_t(vtValue));
- scope = int(atoi(col1.c_str()));
- //Returns the max id + 1
- return scope + 1;
- }
- void Connection::Search(string type)
- {
- char QueryStr[300];
- string col1;
- string col2;
- string col3;
- string col4;
- string col5;
- string col6;
- string col7;
- _variant_t vtValue;
- if (type == "All")
- {
- //Select statement
- sprintf_s(QueryStr, 200, "SELECT EmployeeID, FirstName, LastName, MemberYear, Association FROM Employee");
- EmpRec = ConnDB->Execute(QueryStr, NULL, 1);
- cout << endl << left << " " << setw(6) << "ID" << setw(14) << "First Name" << setw(16) << "Last Name" << setw(8) << "Year" << setw(8) << "Association" << endl;
- cout << "-------------------------------------------------------------\n";
- //Reads the first record
- if (!(EmpRec->EndOfFile))
- {
- //Moves to the first record
- EmpRec->MoveFirst();
- //Reads while it is NOT the end of file/table
- while (!(EmpRec->EndOfFile))
- {
- vtValue = EmpRec->Fields->GetItem("EmployeeID")->GetValue();
- col1 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("FirstName")->GetValue();
- col2 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("LastName")->GetValue();
- col3 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("MemberYear")->GetValue();
- col4 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("Association")->GetValue();
- col5 = string((char*)_bstr_t(vtValue));
- cout << endl << left << " " << setw(6) << col1 << setw(14) << col2 << setw(16) << col3 << setw(8) << col4 << setw(8) << col5 << endl;
- //Moves to the next record
- EmpRec->MoveNext();
- }
- }
- else
- {
- cout << "\n No Records Found!!!!!" << endl;
- }
- }
- else if (type == "Id")
- {
- //variable to hold search data
- int id;
- cout << "Please enter the EmployeeID: ";
- cin >> id;
- cin.ignore();
- //Select statement
- sprintf_s(QueryStr, 200, "SELECT EmployeeID, FirstName, LastName, Address, City, State, ZipCode FROM Employee WHERE EmployeeID = %i",id);
- EmpRec = ConnDB->Execute(QueryStr, NULL, 1);
- cout << endl << left << " " << setw(6) << "ID" << setw(14) << "First Name" << setw(16) << "Last Name" << setw(26) << "Address" << setw(8) << "City" << setw(8) << "State" << setw(8) << "Zip" << endl;
- cout << "-------------------------------------------------------------------------------------\n";
- //Reads the first record
- if (!(EmpRec->EndOfFile))
- {
- //Moves to the first record
- EmpRec->MoveFirst();
- //Reads while it is NOT the end of file/table
- while (!(EmpRec->EndOfFile))
- {
- vtValue = EmpRec->Fields->GetItem("EmployeeID")->GetValue();
- col1 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("FirstName")->GetValue();
- col2 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("LastName")->GetValue();
- col3 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("Address")->GetValue();
- col4 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("City")->GetValue();
- col5 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("State")->GetValue();
- col6 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("ZipCode")->GetValue();
- col7 = string((char*)_bstr_t(vtValue));
- cout << endl << left << " " << setw(6) << col1 << setw(14) << col2 << setw(16) << col3 << setw(26) << col4 << setw(8) << col5 << setw(8) << col6 << setw(8) << col7 << endl;
- //Moves to the next record
- EmpRec->MoveNext();
- }
- }
- else
- {
- cout << "\n No Records Found!!!!!" << endl;
- }
- }
- else if (type == "Year")
- {
- //variable to hold search data
- int year;
- cout << "Please enter the Member Year: ";
- cin >> year;
- cin.ignore();
- //Select statement
- sprintf_s(QueryStr, 200, "SELECT EmployeeID, FirstName, LastName, MemberYear, Association FROM Employee WHERE MemberYear = %i", year);
- EmpRec = ConnDB->Execute(QueryStr, NULL, 1);
- cout << endl << left << " " << setw(6) << "ID" << setw(14) << "First Name" << setw(16) << "Last Name" << setw(26) << "Address" << setw(8) << "City" << setw(8) << "State" << setw(8) << "Zip" << endl;
- cout << "-------------------------------------------------------------------------------------\n";
- //Reads the first record
- if (!(EmpRec->EndOfFile))
- {
- //Moves to the first record
- EmpRec->MoveFirst();
- //Reads while it is NOT the end of file/table
- while (!(EmpRec->EndOfFile))
- {
- vtValue = EmpRec->Fields->GetItem("EmployeeID")->GetValue();
- col1 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("FirstName")->GetValue();
- col2 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("LastName")->GetValue();
- col3 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("Address")->GetValue();
- col4 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("City")->GetValue();
- col5 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("State")->GetValue();
- col6 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("ZipCode")->GetValue();
- col7 = string((char*)_bstr_t(vtValue));
- cout << endl << left << " " << setw(6) << col1 << setw(14) << col2 << setw(16) << col3 << setw(26) << col4 << setw(8) << col5 << setw(8) << col6 << setw(8) << col7 << endl;
- //Moves to the next record
- EmpRec->MoveNext();
- }
- }
- else
- {
- cout << "\n No Records Found!!!!!" << endl;
- }
- }
- else if (type == "Assoc")
- {
- //variable to hold search data
- char ass[11];
- cout << "Please enter the Association: ";
- cin >> ass;
- cin.ignore();
- //Select statement
- sprintf_s(QueryStr, 200, "SELECT EmployeeID, FirstName, LastName, MemberYear, Association FROM Employee WHERE Association = '%s'", ass);
- EmpRec = ConnDB->Execute(QueryStr, NULL, 1);
- cout << endl << left << " " << setw(6) << "ID" << setw(14) << "First Name" << setw(16) << "Last Name" << setw(26) << "Address" << setw(8) << "City" << setw(8) << "State" << setw(8) << "Zip" << endl;
- cout << "-------------------------------------------------------------------------------------\n";
- //Reads the first record
- if (!(EmpRec->EndOfFile))
- {
- //Moves to the first record
- EmpRec->MoveFirst();
- //Reads while it is NOT the end of file/table
- while (!(EmpRec->EndOfFile))
- {
- vtValue = EmpRec->Fields->GetItem("EmployeeID")->GetValue();
- col1 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("FirstName")->GetValue();
- col2 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("LastName")->GetValue();
- col3 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("Address")->GetValue();
- col4 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("City")->GetValue();
- col5 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("State")->GetValue();
- col6 = string((char*)_bstr_t(vtValue));
- vtValue = EmpRec->Fields->GetItem("ZipCode")->GetValue();
- col7 = string((char*)_bstr_t(vtValue));
- cout << endl << left << " " << setw(6) << col1 << setw(14) << col2 << setw(16) << col3 << setw(26) << col4 << setw(8) << col5 << setw(8) << col6 << setw(8) << col7 << endl;
- //Moves to the next record
- EmpRec->MoveNext();
- }
- }
- else
- {
- cout << "\n No Records Found!!!!!" << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement