-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConnection.cs
More file actions
253 lines (225 loc) · 8.54 KB
/
Connection.cs
File metadata and controls
253 lines (225 loc) · 8.54 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TempleDev
{
public class DBConnect
{
String SqlConnectString = "";
SqlConnection myConnectionSql;
SqlCommand objCmd;
SqlDataReader objDataReader;
DataSet ds;
public DBConnect()
{
myConnectionSql = new SqlConnection(SqlConnectString);
}
public DataSet GetDataSet(String SqlSelect)
{
// Input parameter is a SELECT SQL statement. Return is the Dataset
// Note: The Dataset is also stored as a class variable for use
// in the GetField function
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SqlSelect, myConnectionSql);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
ds = myDataSet;
return myDataSet;
}
public DataSet GetDataSet(String SqlSelect, out int theRecordCount)
{
// Input parameter is a SELECT SQL statement.
// Output parameter is the number of rows in the returned dataset.
// Return is a Dataset
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SqlSelect, myConnectionSql);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
ds = myDataSet;
theRecordCount = ds.Tables[0].Rows.Count;
return myDataSet;
}
public DataSet GetDataSet(String SqlSelect, out int theRecordCount, out String theErrorMessage)
{
// Input parameter is a SELECT SQL statement.
// Output parameter (1) is the number of rows in the returned dataset.
// Output parameter (2) is the error message when an exception occurs.
// Return is a Dataset
try
{
SqlDataAdapter myDataAdapter = new SqlDataAdapter(SqlSelect, myConnectionSql);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
ds = myDataSet;
theRecordCount = ds.Tables[0].Rows.Count;
theErrorMessage = "";
return myDataSet;
}
catch (Exception ex)
{
theRecordCount = 0;
theErrorMessage = ex.Message;
return null;
}
}
//public SqlDataReader GetDataReader(String SqlSelect)
//{
// // Input parameter is a SELECT SQL statement.
// objCmd = new SqlCommand(SqlSelect, myConnectionSql);
// return objCmd.ExecuteReader();
//}
public int DoUpdate(String SqlManipulate)
{
// Input parameter is a SQL manipulate statement (INSERT, UPDATE, DELETE).
// Returns the number of rows affected by the update.
// Returns -1 when an exsception occurs.
objCmd = new SqlCommand(SqlManipulate, myConnectionSql);
try
{
myConnectionSql.Open();
int ret = objCmd.ExecuteNonQuery();
myConnectionSql.Close();
return ret;
}
catch (Exception ex)
{
return -1;
}
}
public void DoUpdateWithDSCmdOjb(DataSet passedds, string DBTableDestination)
{
// Parameters in are the dataset being passed and the table you are inserting into.
// The dataset MUST have the SAME COLUMN NAMES as the SQL table to which it is being inserted into.
try
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(myConnectionSql))
{
bulkCopy.DestinationTableName = DBTableDestination;
bulkCopy.WriteToServer(passedds.Tables[0]);
myConnectionSql.Close();
}
}
catch (Exception ex)
{
myConnectionSql.Close();
}
}
public int DoUpdateUsingCmdObj(SqlCommand theCommandObject)
{
// Input parameter is a Command object containing a SQL manipulate statement (Insert, Update, Delete).
// Returns the number of rows affected by the update.
// Returns -1 when an exsception occurs.
// This method is used for passing parameters to a Stored Procedure
try
{
theCommandObject.Connection = myConnectionSql;
theCommandObject.Connection.Open();
int ret = theCommandObject.ExecuteNonQuery();
theCommandObject.Connection.Close();
return ret;
}
catch (Exception ex)
{
return -1;
}
}
public DataSet GetDataSetUsingCmdObj(SqlCommand theCommand)
{
// This method is used for Stored Procedures (SELECT statement only) with Parameters
theCommand.Connection = myConnectionSql;
SqlDataAdapter myDataAdapter = new SqlDataAdapter(theCommand);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
ds = myDataSet;
return myDataSet;
}
public DataRow GetRow(DataSet theDataSet, int theRow)
{
DataTable objTable = ds.Tables[0];
DataRow objRow = objTable.Rows[theRow];
return objRow;
}
public Array GetRows(String theCondition)
{
// Input parameters are (1) a DataSet and (2) the zero based row of the
// table in the DataSet to be returned. Returns a row.
DataRow[] objRow;
DataTable objTable = ds.Tables[0];
objRow = objTable.Select(theCondition);
return objRow;
}
public Object GetField(String theFieldName, int theRow)
{
// Input parameterss are (1) a Field (Column) name as a string and
// (2) the zero based row of the table in the DataSet
// from which the field is to be extracted. Returns the value
// in the field as a variant type.
// This function assumes that one of the getDataSet functions
// had been called, thus producing a ds at the class level.
DataTable objTable = ds.Tables[0];
DataRow objRow = objTable.Rows[theRow];
return objRow[theFieldName];
}
public void CommitDataSet(DataSet theDataSet)
{
// Input parameter is a DataSet. This function is used to Commit
// the Dataset to the Data Source when updating a disconnected ds.
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.Update(theDataSet);
}
public Object ExecuteScalarFunction(SqlCommand theCommand)
{
// Input parameter is a Command object containing a Select statement
// that returns a single scalar value. Returns the single scalar value.
// Returns scalar value as a Variant Type.
theCommand.Connection = myConnectionSql;
return theCommand.ExecuteScalar();
}
public SqlConnection GetConnection()
{
// NOTE: .NET has implemented its Stored User Defined Functions only
// with the Managed Provider for SQL Server,
// not the OLEDB provider.
return myConnectionSql;
}
public void CloseConnection()
{
try
{
myConnectionSql.Close();
}
catch (Exception ex)
{
// Catch exception created when trying to close a closed connection.
}
}
public void ResetConnection()
{
try
{
myConnectionSql.Close();
myConnectionSql.Open();
}
catch (Exception ex)
{
// Catch exception created when trying to close a closed connection.
}
}
// The Deconstructor
~DBConnect()
{
// Close any open connections to the database before the objects of this class
// are garbage collected.
try
{
myConnectionSql.Close();
}
catch (Exception ex)
{
// Catch exception created when trying to close a closed connection.
}
}
} // end class
} // end namespace