/
SqlHelper.cs
205 lines (185 loc) · 7.78 KB
/
SqlHelper.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.IO;
namespace Xslt.ApplicationBlocks.Data
{
public class SqlHelper
{
/// <summary>
/// Executes a query against a database - returns the result for pretty displaying of the data in xslt
/// </summary>
/// <param name="args">
/// Accepts a piece of xml that can be mapped to the Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset method.
///
/// Read about it here: http://projekt.chainbox.dk/default.asp?W258
/// </param>
/// <returns>
/// The first datatable returned by the query
///
/// <root>
/// <item>
/// <fieldname>fieldvalue</fieldname>
/// <fieldname>fieldvalue</fieldname>
/// <fieldname>fieldvalue</fieldname>
/// </item>
/// <item>
/// <fieldname>fieldvalue</fieldname>
/// <fieldname>fieldvalue</fieldname>
/// <fieldname>fieldvalue</fieldname>
/// </item>
/// </root>
///
/// </returns>
public static XPathNodeIterator ExecuteDataset(XPathNodeIterator args)
{
try
{
XPathNodeIterator retval;
SqlHelperArgs sqlargs = ParseArgs(args);
DataSet ds = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(sqlargs.ConnectionString, sqlargs.CommandType, sqlargs.CommandText, sqlargs.Params.ToArray());
XmlDocument xd = new XmlDocument();
ds.DataSetName = "root";
// this is bad - the query might not return anything - but hey this is webdevelopment.
ds.Tables[0].TableName = "item";
using (StringWriter sw = new StringWriter())
{
ds.WriteXml(sw);
xd.LoadXml(sw.ToString());
}
retval = xd.CreateNavigator().Select(".");
WriteToTrace("ExecuteDataset: Returning data from:" + sqlargs.CommandText + " without using cache");
return retval;
}
catch (Exception ex)
{
WriteToTrace(ex.ToString());
return EmptyXpathNodeIterator(ex, "Xslt.ApplicationBlocks.Data.ExecuteDataset", true);
}
}
/// <summary>
/// Executes a query against a database without results
/// </summary>
/// <param name="args">
/// Accepts a piece of xml that can be mapped to the Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset method.
///
/// Read about it here: http://projekt.chainbox.dk/default.asp?W258
/// </param>
public static void ExecuteNonQuery(XPathNodeIterator args)
{
try
{
SqlHelperArgs sqlargs = ParseArgs(args);
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(sqlargs.ConnectionString, sqlargs.CommandType, sqlargs.CommandText, sqlargs.Params.ToArray());
}
catch (Exception ex)
{
WriteToTrace(ex.ToString());
}
}
/// <summary>
/// Executes a query against a database with a single result
/// </summary>
/// <param name="args">
/// Accepts a piece of xml that can be mapped to the Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset method.
///
/// Read about it here: http://projekt.chainbox.dk/default.asp?W258
/// </param>
/// <returns>
/// A string representation of the scalar sql result
/// </returns>
public static string ExecuteScalar(XPathNodeIterator args)
{
try
{
SqlHelperArgs sqlargs = ParseArgs(args);
return Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteScalar(sqlargs.ConnectionString, sqlargs.CommandType, sqlargs.CommandText, sqlargs.Params.ToArray()).ToString();
}
catch (Exception ex)
{
WriteToTrace(ex.ToString());
}
return "Error executing sql - investigate trace";
}
private static XPathNodeIterator EmptyXpathNodeIterator(Exception ex, string caller, bool logAsError)
{
XmlDocument xd = new XmlDocument();
xd.AppendChild(xd.CreateElement("root"));
xd.FirstChild.AppendChild(xd.CreateCDataSection("An error has occured in method: " + caller + "\n" + ex.Message));
return xd.FirstChild.CreateNavigator().Select(".");
}
private static void WriteToTrace(string message)
{
System.Web.HttpContext.Current.Trace.Warn(message);
}
private static SqlHelperArgs ParseArgs(XPathNodeIterator args) {
args.MoveNext();
XmlDocument xd = new XmlDocument();
xd.LoadXml(args.Current.OuterXml);
XmlElement argsel = (XmlElement)xd.FirstChild;
SqlHelperArgs retval = new SqlHelperArgs();
retval.CommandText = argsel.SelectSingleNode("commandtext").InnerText;
if (argsel.SelectSingleNode("commandtype").InnerText.ToLower() == "storedprocedure")
{
retval.CommandType = CommandType.StoredProcedure;
}
else {
retval.CommandType = CommandType.Text;
}
foreach (XmlElement el in argsel.SelectNodes("parameter"))
{
string name = el.GetAttribute("name");
string value = el.InnerText;
retval.Params.Add(new SqlParameter(name, value));
}
// A connectionstring can either be specified in a custom app setting
// Be specified in the XSLT
// Or by default fall back to the umbraco connectionstring
XmlElement connectionstringel = (XmlElement)argsel.SelectSingleNode("connectionstring");
if (connectionstringel != null) {
if (connectionstringel.HasAttribute("appkey")) {
retval.ConnectionString = System.Configuration.ConfigurationManager.AppSettings[connectionstringel.GetAttribute("appkey")];
} else {
// you can specify the DBDSN directly in the XSLT file
retval.ConnectionString = connectionstringel.InnerText;
}
}
// If no connectionstring is specified
// this baby falls back to the umbraco connectionstring
else {
retval.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["umbracoDbDSN"];
}
return retval;
}
private class SqlHelperArgs
{
public string ConnectionString = "";
public string CommandText = "";
public CommandType CommandType = CommandType.StoredProcedure;
public List<SqlParameter> Params = new List<SqlParameter>();
public bool UseCache = false;
public int CacheExpiration = 0;
/// <summary>
/// Generates a unique key for inserting in a cachestorage
/// </summary>
public string CacheKey
{
get
{
string retval = ConnectionString + CommandText;
foreach (SqlParameter param in Params)
{
retval += param.ParameterName + param.Value.ToString();
}
return retval;
}
}
}
}
}