This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsDataAccess.cs
151 lines (131 loc) · 5.32 KB
/
clsDataAccess.cs
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
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
/// <summary>
/// Class used to access the database.
/// </summary>
public class clsDataAccess
{
/// <summary>
/// Connection string to the database.
/// Holds Path to the file
/// </summary>
private string sConnectionString;
/// <summary>
/// Constructor that sets the connection string to the database
/// </summary>
public clsDataAccess()
{
sConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data source= " + Directory.GetCurrentDirectory() + "\\Invoice.accdb";
}
/// <summary>
/// This method takes an SQL statment that is passed in and executes it. The resulting values
/// are returned in a DataSet. The number of rows returned from the query will be put into
/// the reference parameter iRetVal.
/// </summary>
/// <param name="sSQL">The SQL statement to be executed.</param>
/// <param name="iRetVal">Reference parameter that returns the number of selected rows.</param>
/// <returns>Returns a DataSet that contains the data from the SQL statement.</returns>
public DataSet ExecuteSQLStatement(string sSQL, ref int iRetVal)
{
try
{
//Create a new DataSet
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(sConnectionString))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter())
{
//Open the connection to the database
conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object
adapter.SelectCommand = new OleDbCommand(sSQL, conn);
adapter.SelectCommand.CommandTimeout = 0;
//Fill up the DataSet with data
adapter.Fill(ds);
}
}
//Set the number of values returned
iRetVal = ds.Tables[0].Rows.Count;
//return the DataSet
return ds;
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// This method takes an SQL statment that is passed in and executes it. The resulting single
/// value is returned.
/// </summary>
/// <param name="sSQL">The SQL statement to be executed.</param>
/// <returns>Returns a string from the scalar SQL statement.</returns>
public string ExecuteScalarSQL(string sSQL)
{
try
{
//Holds the return value
object obj;
using (OleDbConnection conn = new OleDbConnection(sConnectionString))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter())
{
//Open the connection to the database
conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object
adapter.SelectCommand = new OleDbCommand(sSQL, conn);
adapter.SelectCommand.CommandTimeout = 0;
//Execute the scalar SQL statement
obj = adapter.SelectCommand.ExecuteScalar();
}
}
//See if the object is null
if (obj == null)
{
//Return a blank
return "";
}
else
{
//Return the value
return obj.ToString();
}
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
/// <summary>
/// This method takes an SQL statment that is a non query and executes it.
/// </summary>
/// <param name="sSQL">The SQL statement to be executed.</param>
/// <returns>Returns the number of rows affected by the SQL statement.</returns>
public int ExecuteNonQuery(string sSQL)
{
try
{
//Number of rows affected
int iNumRows;
using (OleDbConnection conn = new OleDbConnection(sConnectionString))
{
//Open the connection to the database
conn.Open();
//Add the information for the SelectCommand using the SQL statement and the connection object
OleDbCommand cmd = new OleDbCommand(sSQL, conn);
cmd.CommandTimeout = 0;
//Execute the non query SQL statement
iNumRows = cmd.ExecuteNonQuery();
}
//return the number of rows affected
return iNumRows;
}
catch (Exception ex)
{
throw new Exception(MethodInfo.GetCurrentMethod().DeclaringType.Name + "." + MethodInfo.GetCurrentMethod().Name + " -> " + ex.Message);
}
}
}