/
MicrosoftSQLServerDatabase.cs
131 lines (114 loc) · 4.39 KB
/
MicrosoftSQLServerDatabase.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
// ___________________________________________________
//
// © Hi-Integrity Systems 2010. All rights reserved.
// www.hisystems.com.au - Toby Wicks
// ___________________________________________________
//
using System.Collections;
using System;
using System.Data;
using System.Collections.Generic;
namespace DatabaseObjects
{
public class MicrosoftSQLServerDatabase : Database
{
private string pstrDataSource;
private string pstrDatabase;
/// <summary>
/// Connects to a Microsoft SQL Server database.
/// Uses SSPI security to connect to the database.
/// </summary>
/// <remarks></remarks>
public MicrosoftSQLServerDatabase(string strDataSource, string strDatabaseName)
: base(ConnectionStringRoot(strDataSource, strDatabaseName) + "Integrated Security=SSPI;", ConnectionType.SQLServer)
{
EnsureDatabaseDetailsValid(strDataSource, strDatabaseName);
pstrDatabase = strDatabaseName;
pstrDataSource = strDataSource;
}
/// <summary>
/// Connects to a Microsoft SQL Server database.
/// </summary>
/// <remarks></remarks>
public MicrosoftSQLServerDatabase(string strDataSource, string strDatabaseName, string strUserName, string strPassword)
: base(ConnectionStringRoot(strDataSource, strDatabaseName) + "UID=" + strUserName + ";pwd=" + strPassword + ";", ConnectionType.SQLServer)
{
EnsureDatabaseDetailsValid(strDataSource, strDatabaseName);
if (string.IsNullOrEmpty(strUserName))
throw new ArgumentNullException("UserName");
pstrDatabase = strDatabaseName;
pstrDataSource = strDataSource;
}
public string Source
{
get
{
return pstrDataSource;
}
}
public string Name
{
get
{
return pstrDatabase;
}
}
/// <summary>
/// If parsing fails then the FormatException message will contain useful information as to the cause.
/// </summary>
/// <exception cref="FormatException">If the connection string is in an invalid format.</exception>
public static MicrosoftSQLServerDatabase Parse(string strConnectionString)
{
var objDictionary = GetDictionaryFromConnectionString(strConnectionString);
if (objDictionary.ContainsKey("integrated security"))
return new MicrosoftSQLServerDatabase(GetDataSource(objDictionary), GetDatabase(objDictionary));
else
return new MicrosoftSQLServerDatabase(GetDataSource(objDictionary), GetDatabase(objDictionary), GetUserID(objDictionary), GetPassword(objDictionary));
}
private static string GetDataSource(IDictionary<string, string> objConnectionDictionary)
{
if (objConnectionDictionary.ContainsKey("data source"))
return objConnectionDictionary["data source"];
else
throw new FormatException("Could not find 'Data Source=' definition");
}
private static string GetDatabase(IDictionary<string, string> objConnectionDictionary)
{
if (objConnectionDictionary.ContainsKey("database"))
return objConnectionDictionary["database"];
else if (objConnectionDictionary.ContainsKey("initial catalog"))
return objConnectionDictionary["initial catalog"];
else
throw new FormatException("Could not find 'Database=' or 'Initial Catalog=' definition");
}
private static string GetUserID(IDictionary<string, string> objConnectionDictionary)
{
if (objConnectionDictionary.ContainsKey("uid"))
return objConnectionDictionary["uid"];
else if (objConnectionDictionary.ContainsKey("user id"))
return objConnectionDictionary["user id"];
else
throw new FormatException("Could not find 'UID=' or 'User ID=' definition");
}
private static string GetPassword(IDictionary<string, string> objConnectionDictionary)
{
if (objConnectionDictionary.ContainsKey("pwd"))
return objConnectionDictionary["pwd"];
else if (objConnectionDictionary.ContainsKey("password"))
return objConnectionDictionary["password"];
else
throw new FormatException("Could not find 'Password=' or 'Pwd=' definition");
}
private void EnsureDatabaseDetailsValid(string strDataSource, string strDatabaseName)
{
if (string.IsNullOrEmpty(strDataSource))
throw new ArgumentNullException("DataSource");
else if (string.IsNullOrEmpty(strDatabaseName))
throw new ArgumentNullException("DatabaseName");
}
private static string ConnectionStringRoot(string strDataSource, string strDatabaseName)
{
return "Provider=SQLOLEDB;Data Source=" + strDataSource + ";" + "Database=" + strDatabaseName + ";";
}
}
}