/
SqlServerStorage.cs
175 lines (143 loc) · 5.94 KB
/
SqlServerStorage.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
#if !NETSTANDARD
using System;
using System.Data;
namespace FileHelpers.DataLink
{
using System.Data.SqlClient;
/// <summary>
/// This is a base class that implements the <see cref="DataStorage"/> for
/// Microsoft SqlServer.
/// </summary>
[Obsolete("Datalink feature is outdated and will be rewritten, see https://www.filehelpers.net/mustread/")]
public sealed class SqlServerStorage : DatabaseStorage
{
#region " Constructors "
/// <summary>
/// Create a new instance of the SqlServerStorage based on the record
/// type provided.
/// </summary>
/// <param name="recordType">The type of the record class.</param>
public SqlServerStorage(Type recordType)
: this(recordType, string.Empty) {}
/// <summary>
/// Create a new instance of the SqlServerStorage based on the record
/// type provided.
/// </summary>
/// <param name="recordType">The type of the record class.</param>
/// <param name="connectionStr">
/// The full connection string used to connect to the SQL server.
/// </param>
public SqlServerStorage(Type recordType, string connectionStr)
: base(recordType)
{
ConnectionString = connectionStr;
}
/// <summary>
/// Create a new instance of the SqlServerStorage based on the record
/// type provided (uses windows auth)
/// </summary>
/// <param name="recordType">The type of the record class.</param>
/// <param name="server">The server name or IP of the sqlserver</param>
/// <param name="database">The database name into the server.</param>
public SqlServerStorage(Type recordType, string server, string database)
: this(recordType, server, database, string.Empty, string.Empty) {}
/// <summary>
/// Create a new instance of the SqlServerStorage based on the record
/// type provided (uses SqlServer auth)
/// </summary>
/// <param name="recordType">The type of the record class.</param>
/// <param name="server">The server name or IP of the sqlserver</param>
/// <param name="database">The database name into the server.</param>
/// <param name="user">The SQL username to login into the server.</param>
/// <param name="pass">The pass of the SQL username to login into the server.</param>
public SqlServerStorage(Type recordType, string server, string database, string user, string pass)
: this(recordType, DataBaseHelper.SqlConnectionString(server, database, user, pass))
{
mServerName = server;
mDatabaseName = database;
mUserName = user;
mUserPass = pass;
}
#endregion
#region " Create Connection and Command "
/// <summary>Create a connection object to the database</summary>
/// <returns>SQL server Connection Object.</returns>
protected override sealed IDbConnection CreateConnection()
{
string conString;
if (ConnectionString == string.Empty) {
if (mServerName == null ||
mServerName == string.Empty)
throw new BadUsageException("The ServerName can't be null or empty.");
if (mDatabaseName == null ||
mDatabaseName == string.Empty)
throw new BadUsageException("The DatabaseName can't be null or empty.");
conString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
}
else
conString = ConnectionString;
return new SqlConnection(conString);
}
#endregion
#region " ServerName "
private string mServerName = string.Empty;
/// <summary> The server name or IP of the SqlServer </summary>
public string ServerName
{
get { return mServerName; }
set
{
mServerName = value;
ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
}
}
#endregion
#region " DatabaseName "
private string mDatabaseName = string.Empty;
/// <summary> The name of the database. </summary>
public string DatabaseName
{
get { return mDatabaseName; }
set
{
mDatabaseName = value;
ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
}
}
#endregion
#region " UserName "
private string mUserName = string.Empty;
/// <summary> The user name used to logon into the SqlServer. (leave empty for WindowsAuth)</summary>
public string UserName
{
get { return mUserName; }
set
{
mUserName = value;
ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
}
}
#endregion
#region " UserPass "
private string mUserPass = string.Empty;
/// <summary> The user pass used to logon into the SqlServer. (leave empty for WindowsAuth)</summary>
public string UserPass
{
get { return mUserPass; }
set
{
mUserPass = value;
ConnectionString = DataBaseHelper.SqlConnectionString(ServerName, DatabaseName, UserName, UserPass);
}
}
#endregion
#region " ExecuteInBatch "
/// <summary>do we get groups of records or singularly</summary>
protected override bool ExecuteInBatch
{
get { return true; }
}
#endregion
}
}
#endif