-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextensions.cs
More file actions
154 lines (122 loc) · 4.27 KB
/
extensions.cs
File metadata and controls
154 lines (122 loc) · 4.27 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
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace zenComparer
{
public static class Extensions
{
public static Regex regex = new Regex("[\\s]", // \\s [\n\r]
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
/// <summary>
/// convert to hashtable
/// </summary>
public static Hashtable _convertDataTableToHashTable(DataTable dtIn, string keyField, string valueField)
{
Hashtable htOut = new Hashtable();
foreach (DataRow drIn in dtIn.Rows)
{
if (!htOut.ContainsKey(drIn[keyField].ToString().ToLower()))
htOut.Add(drIn[keyField].ToString().ToLower(), drIn[valueField].ToString());
}
return htOut;
}
/// <summary>
/// convert to hashtable
/// </summary>
public static Hashtable _convertDataTableToHashTable(DataTable dtIn, int columnIndexKey, int columnIndexValue)
{
Hashtable htOut = new Hashtable();
foreach (DataRow drIn in dtIn.Rows)
{
if (!htOut.ContainsKey(drIn[columnIndexKey].ToString().ToLower()))
htOut.Add(drIn[columnIndexKey].ToString().ToLower(), drIn[columnIndexValue].ToString());
}
return htOut;
}
/// <summary>
/// eliminuje whitespace bardzo wazne chodzi o entery etc
/// </summary>
/// <param name="IN"></param>
/// <returns></returns>
public static string _cleanstring(string input)
{
return regex.Replace(input, "");
}
//dirty check
public static string _cleanstringWithoutComments(string input)
{
input = input.Replace("WITH EXEC AS CALLER", "");
input = input.Replace("[", "");
input = input.Replace("]", "");
var blockComments = @"/\*(.*?)\*/";
var lineComments = @"--(.*?)\r?\n";
var strings = @"'((\\[^\n]|[^""\n])*)'";
// var verbatimStrings = @"@(""[^""]*"")+";
string retvalue = Regex.Replace(input,
blockComments + "|" + lineComments + "|" + strings,"",
RegexOptions.Singleline);
return zenComparer.Extensions._cleanstring(retvalue);
}
public static string _getSeparatedString(string IN, int position)
{
char[] delim = { '|' };
string[] splitted = IN.Split(delim);
string temp = "";
try
{
temp = splitted[position];
}
catch (System.Exception)
{
temp = "";
}
return temp;
}
public static System.Data.DataTable GetDataTable(string sql, string dsn)
{
DataTable dt = new DataTable();
SqlConnection cnn = new SqlConnection(dsn);
cnn.Open();
SqlCommand mycommand = new SqlCommand(sql, cnn);
mycommand.CommandText = sql;
mycommand.CommandTimeout = 90;
SqlDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
return dt;
}
public static string MakeSafeFilename(string filename, char replaceChar)
{
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
filename = filename.Replace(c, replaceChar);
}
return filename;
}
public static string GetValue(string sql, string dsn)
{
string temp;
try
{
//DataTable dt = new DataTable();
SqlConnection cnn = new SqlConnection(dsn);
cnn.Open();
SqlCommand mycommand = new SqlCommand(sql, cnn);
mycommand.CommandText = sql;
temp = mycommand.ExecuteScalar().ToString();
cnn.Close();
}
catch (System.Exception ex)
{
temp = "ERROR " + ex.Message.ToString();
}
return temp;
}
}
}