-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.cs
148 lines (130 loc) · 3.94 KB
/
db.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Net.Configuration;
using System.Configuration;
namespace IBC.Database
{
public class db : IDisposable
{
private readonly SqlConnection _sqlConn = new SqlConnection();
public SqlDataReader Reader;
private readonly SqlCommand _cmd = new SqlCommand();
private bool _disposed = false;
private int _rowsUpdated;
/// <summary>
/// Database Constructor
/// </summary>
/// <param name="conn">Takes in the database configuration name from web.config</param>
public db(string conn)
{
_sqlConn.ConnectionString = ConfigurationManager.ConnectionStrings[conn].ConnectionString;
_sqlConn.Open();
_cmd.Connection = _sqlConn;
}
~db()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (this._disposed) return;
if (disposing)
{
// Dispose managed resources.
_sqlConn.Close();
_sqlConn.Dispose();
_cmd.Dispose();
}
_disposed = true;
}
public void SetSqlStoredProcedure()
{
_cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
public void SetSqlText()
{
_cmd.CommandType = System.Data.CommandType.Text;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public SqlException ExecuteSql(string sqlStr)
{
try
{
_cmd.CommandText = sqlStr;
_rowsUpdated = _cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
CloseConnection();
return ex;
}
return null;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public SqlException ExecuteSqlReader(string sqlStr)
{
try
{
_cmd.CommandText = sqlStr;
Reader = _cmd.ExecuteReader();
}
catch (SqlException ex)
{
CloseConnection();
return ex;
}
return null;
}
public void ParameterIdentity(string parameter)
{
var param = new SqlParameter
{
ParameterName = parameter,
Direction = ParameterDirection.Output,
SqlDbType = SqlDbType.Int
};
_cmd.Parameters.Add(param);
}
public object ParameterValue(string parameter)
{
return _cmd.Parameters[parameter].Value;
}
public void ParameterAdd(string parameter, object value)
{
_cmd.Parameters.AddWithValue(parameter, value);
}
public void ParameterEdit(string parameter, object value)
{
_cmd.Parameters[parameter].Value = value;
}
public void ParameterClear(string parameter)
{
_cmd.Parameters.RemoveAt(parameter);
}
public void ParameterClearAll()
{
_cmd.Parameters.Clear();
}
public string ConnectionStatus()
{
return _sqlConn.State.ToString();
}
public void CloseConnection()
{
_sqlConn.Close();
}
public int getRowsUpdated()
{
return _rowsUpdated;
}
}
}