-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollationChanger.cs
110 lines (91 loc) · 3.84 KB
/
CollationChanger.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
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Globalization;
using System.Reflection;
namespace AlterCollation
{
public class CollationChanger
{
private bool canceled;
public ScriptStepCollection GenerateScript(IScriptExecuteCallback callback, string server, string userId, string password, string database, bool dropAllConstraints, string collation, FullTextLanguage language, bool setSingleUser)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = Utils.ConnectionString(server, userId, password, 2, database);
try
{
connection.Open();
ScriptStepCollection script = LoadScript(new Version(connection.ServerVersion), database, dropAllConstraints, collation, language, setSingleUser);
//now get the last script entry and replace it with
//a special component that will return out
ScriptStepGenerateScript generator = new ScriptStepGenerateScript(script[script.Count - 1]);
script[script.Count - 1] = generator;
script.Execute(connection, callback);
return generator.Script;
}
finally
{
connection.Dispose();
}
}
public bool Canceled
{
get { return canceled; }
}
public void Cancel()
{
canceled = true;
}
///look in the assembly manifest resources for the script files
///get the ones that are SQL scripts and order them
private ScriptStepCollection LoadScript(Version serverVersion, string database, bool dropAllConstraints, string collation, FullTextLanguage language, bool setSingleUser)
{
string[] resources;
Assembly thisAssembly;
ScriptStepCollection script = new ScriptStepCollection();
thisAssembly = this.GetType().Assembly;
resources = thisAssembly.GetManifestResourceNames();
int lcid = language==null ? int.MinValue : language.Lcid;
string dropAllConstraintsText = dropAllConstraints ? "1" : "0";
string setSingleUserText = setSingleUser ? "1" : "0";
object[] formatArgs = new object[] { database, collation, dropAllConstraintsText, lcid, setSingleUserText };
//first get a list or resources
ArrayList resourceNames = new ArrayList();
foreach (string resource in resources)
{
if (string.Compare(resource.Substring(resource.Length - 4), ".sql", true, CultureInfo.InvariantCulture) == 0)
{
//we want this script...
//lets see if it is specific to a version of sql server
string versionText = resource.Substring(resource.Length - 9, 5);
if (versionText == ".2000")
{
if (serverVersion.Major <= 8)
{
resourceNames.Add(resource);
}
}
else if (versionText == ".2005")
{
if (serverVersion.Major >= 9)
{
resourceNames.Add(resource);
}
}
else
{
resourceNames.Add(resource);
}
}
}
resourceNames.Sort();
foreach (string resource in resourceNames)
{
script.Add(new ScriptStepResource(resource, formatArgs));
}
return script;
}
}
}