-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIWrapper.cs
More file actions
60 lines (52 loc) · 2.35 KB
/
APIWrapper.cs
File metadata and controls
60 lines (52 loc) · 2.35 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Altiris.Database;
using Altiris.NS.ContextManagement;
namespace Symantec.CWoC.APIWrappers {
class DatabaseAPI {
public static DataTable GetTable(string sqlStatement) {
DataTable t = new DataTable();
try {
using (DatabaseContext context = DatabaseContext.GetContext()) {
SqlCommand cmdAllResources = context.CreateCommand() as SqlCommand;
cmdAllResources.CommandText = sqlStatement;
using (SqlDataReader r = cmdAllResources.ExecuteReader()) {
t.Load(r);
}
}
return t;
} catch (Exception e) {
Console.WriteLine("Error: {0}\nException message = {1}\nStack trace = {2}.", e.Message, e.InnerException, e.StackTrace);
throw new Exception("Failed to execute SQL command...");
}
}
public static int ExecuteNonQuery(string sqlStatement) {
try {
using (DatabaseContext context = DatabaseContext.GetContext()) {
SqlCommand sql_cmd = context.CreateCommand() as SqlCommand;
sql_cmd.CommandText = sqlStatement;
return sql_cmd.ExecuteNonQuery();
}
} catch (Exception e) {
Console.WriteLine("Error: {0}\nException message = {1}\nStack trace = {2}.", e.Message, e.InnerException, e.StackTrace);
throw new Exception("Failed to execute non query SQL command...");
}
}
public static int ExecuteScalar(string sqlStatement) {
try {
using (DatabaseContext context = DatabaseContext.GetContext()) {
SqlCommand cmd = context.CreateCommand() as SqlCommand;
cmd.CommandText = sqlStatement;
Object result = cmd.ExecuteScalar();
return Convert.ToInt32(result);
}
} catch (Exception e) {
Console.WriteLine("Error: {0}\nException message = {1}\nStack trace = {2}.", e.Message, e.InnerException, e.StackTrace);
throw new Exception("Failed to execute scalar SQL command...");
}
}
}
}