-
Notifications
You must be signed in to change notification settings - Fork 0
/
JARVISDataSource.cs
104 lines (102 loc) · 3.35 KB
/
JARVISDataSource.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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JARVIS4
{
public class JARVISDataSource
{
public string Server { get; set; }
public string Database { get; set; }
public string UserID { get; set; }
public string Password { get; set; }
public string AuthenticationType { get; set; }
public bool TrustedConnection { get; set; }
public JARVISDataSource(
string Server,
string Database,
string UserID,
string Password)
{
this.Server = Server;
this.Database = Database;
this.UserID = UserID;
this.Password = Password;
this.AuthenticationType = "sqlauth";
}
public JARVISDataSource(
string Server,
string Database)
{
this.Server = Server;
this.Database = Database;
this.TrustedConnection = true;
this.AuthenticationType = "winauth";
}
public JARVISDataSource(string ConnectionString)
{
List<string> ConnectionStringParameters = ConnectionString.Split(';').Select(x => x.Trim()).ToList();
if(ConnectionStringParameters.Count == 2)
{
this.Server = ConnectionStringParameters[0];
this.Database = ConnectionStringParameters[1];
this.TrustedConnection = true;
this.AuthenticationType = "winauth";
}
else if(ConnectionStringParameters.Count == 4)
{
this.Server = ConnectionStringParameters[0];
this.Database = ConnectionStringParameters[1];
this.UserID = ConnectionStringParameters[2];
this.Password = ConnectionStringParameters[3];
this.AuthenticationType = "sqlauth";
}
}
public JARVISDataSource()
{
}
public string GetConnectionString()
{
string connection_string = "";
if (this.AuthenticationType == "sqlauth")
{
connection_string = String.Format(
"server={0};" +
"database={1};" +
"user id={2};" +
"password={3}",
this.Server,
this.Database,
this.UserID,
this.Password);
}
else if (this.AuthenticationType == "winauth")
{
connection_string = String.Format(
"server={0};" +
"database={1};" +
"trusted_connection=true;",
this.Server,
this.Database,
true);
}
return connection_string;
}
public SqlConnection CreateConnection()
{
SqlConnection new_connection = new SqlConnection();
try
{
new_connection = new SqlConnection();
new_connection.ConnectionString = this.GetConnectionString();
}
catch(Exception e)
{
new_connection = null;
}
return new_connection;
}
}
}