Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
grgreeef committed Dec 29, 2018
0 parents commit 8e5efbf
Show file tree
Hide file tree
Showing 24 changed files with 4,638 additions and 0 deletions.
510 changes: 510 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions PhoneBook.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneBook", "PhoneBook\PhoneBook.csproj", "{71F1CFEC-1E1F-4AE8-8BD4-F652ABD0492C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{71F1CFEC-1E1F-4AE8-8BD4-F652ABD0492C}.Debug|x86.ActiveCfg = Debug|x86
{71F1CFEC-1E1F-4AE8-8BD4-F652ABD0492C}.Debug|x86.Build.0 = Debug|x86
{71F1CFEC-1E1F-4AE8-8BD4-F652ABD0492C}.Release|x86.ActiveCfg = Release|x86
{71F1CFEC-1E1F-4AE8-8BD4-F652ABD0492C}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file added PhoneBook.suo
Binary file not shown.
15 changes: 15 additions & 0 deletions PhoneBook/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="searchBarToolStripMenuItem.Checked" value="true"/>
<add key="copyOnClickToolStripMenuItem.Checked" value="false"/>
<add key="disablePromptToolStripMenuItem.Checked" value="true"/>
</appSettings>
<connectionStrings>
<add name="PhoneBook.Properties.Settings.ContactsDBConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\accdb\ContactsDB.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
1,605 changes: 1,605 additions & 0 deletions PhoneBook/ContactsDBDataSet.Designer.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions PhoneBook/ContactsDBDataSet.xsc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

127 changes: 127 additions & 0 deletions PhoneBook/ContactsDBDataSet.xsd

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions PhoneBook/ContactsDBDataSet.xss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

135 changes: 135 additions & 0 deletions PhoneBook/DBMngProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace PhoneBook
{

// This class is a static container class for the interface needs in order to connect with the database
public static class DBMngProxy
{

/* Contacts Database string
*private static OleDbConnection contactsDBConn, used to connect
*ACCDB 2013 connection string
*/
private static OleDbConnection contactsDBConn = new OleDbConnection();

// Retrieve the path of the database file name, using the ConnectionString
private static string GetDatabasePath()
{
// to null reference or out of bounds
if (ConnectionString != null && ConnectionString != string.Empty)
{
return ConnectionString.Substring(ConnectionString.IndexOf("=", ConnectionString.IndexOf("Source=")) + 1);
}
return string.Empty;
}

// public interface for checking about the database existing
public static bool DatabaseFileExists()
{
return File.Exists(GetDatabasePath());
}

// CennctionString get and set
public static string ConnectionString
{
get
{
return DBMngProxy.contactsDBConn.ConnectionString;
}
set
{
DBMngProxy.contactsDBConn.ConnectionString = value;
}
}

// Remove contact by id and return how many rows were affected
public static int RemoveContact(int id)
{
return ExecuteNonQuery(String.Format("DELETE FROM ContactsTable Where id={0}", id));
}

// Remove all the contacts from ContactsTable
public static int RemoveAllContacts()
{
return ExecuteNonQuery("DELETE FROM ContactsTable");
}

// Insert new contact into the database with the details from the declaration, the method returns the number of affected rows
public static int InsertNewContact(string firstName, string lastName, string phoneNumber, string gender)
{
return ExecuteNonQuery(String.Format("INSERT INTO ContactsTable (firstName, lastName, phoneNumber, gender) VALUES ('{0}','{1}','{2}','{3}')",
firstName, lastName, phoneNumber, gender));

}

// Update existing contact to the details from the declaration, the method returns the number of affected rows
public static int UpdateContact(string firstName, string lastName, string phoneNumber, string gender, int id)
{
return ExecuteNonQuery(string.Format("UPDATE ContactsTable SET firstName='{0}', lastName='{1}', phoneNumber='{2}', gender='{3}' WHERE id={4}",
firstName, lastName, phoneNumber, gender, id));
}

/* The method is checking whether an existing contact have this phone number already, the purpose of the method is to
* avoid duplications of phone numbers
* the method return the id if found the same phone number or -1 in other case
*/
public static int PhoneNumberExists(string phoneNumber)
{
int id = -1; // default state is not found
OleDbCommand oleDbCmd;
try
{
contactsDBConn.Open(); // start connection
oleDbCmd = new OleDbCommand("SELECT * FROM ContactsTable WHERE phoneNumber='" + phoneNumber + "'", contactsDBConn); // sql query
if (oleDbCmd != null)
{
OleDbDataReader dr = oleDbCmd.ExecuteReader();
if (dr != null)
{
dr.Read(); // moving to first row if available
if (dr.HasRows)
{
id = Convert.ToInt32(dr["id"]);
}
}
}
contactsDBConn.Close(); //connection close
return id;
}
catch (Exception e)
{
// if something wen wrong Exception will be thrown
contactsDBConn.Close();
throw new Exception("An error occured", e);
}
}

// This is help method for executing non query sql commands, "sqlCmd" is the requaierd command, the method returns the number of affected rows
private static int ExecuteNonQuery(string sqlCmd)
{
OleDbCommand oleDbCmd;
try
{
int affectedRows; // prepare int for affected rows
contactsDBConn.Open(); // start connection
oleDbCmd = new OleDbCommand(sqlCmd, contactsDBConn);
affectedRows = oleDbCmd.ExecuteNonQuery(); // execute the sql command
contactsDBConn.Close(); // connection close
return affectedRows;
}
catch (Exception e)
{
// if something wen wrong Exception will be thrown
throw new InvalidOperationException("Data could not be read, try again later", e);
}
}
}
}
Loading

0 comments on commit 8e5efbf

Please sign in to comment.