Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConnectMsSql.cs
1using System;
2using System.Data.SqlClient;
3using System.Security;
4using System.Threading.Tasks;
9
11{
16 {
24 public ConnectMsSql(ScriptNode ConnectionString, int Start, int Length, Expression Expression)
25 : base(new ScriptNode[] { ConnectionString }, argumentTypes1Scalar, Start, Length, Expression)
26 {
27 }
28
38 : base(new ScriptNode[] { Host, Database }, argumentTypes2Scalar, Start, Length, Expression)
39 {
40 }
41
51 public ConnectMsSql(ScriptNode ConnectionString, ScriptNode UserName, ScriptNode Password, int Start, int Length, Expression Expression)
52 : base(new ScriptNode[] { ConnectionString, UserName, Password }, argumentTypes3Scalar, Start, Length, Expression)
53 {
54 }
55
66 public ConnectMsSql(ScriptNode Host, ScriptNode Database, ScriptNode UserName, ScriptNode Password, int Start, int Length, Expression Expression)
67 : base(new ScriptNode[] { Host, Database, UserName, Password }, argumentTypes4Scalar, Start, Length, Expression)
68 {
69 }
70
74 public override string FunctionName => nameof(ConnectMsSql);
75
79 public override string[] DefaultArgumentNames => new string[] { "Host", "Database", "UserName", "Password" };
80
85 public override bool IsAsynchronous => true;
86
94 {
95 return this.EvaluateAsync(Arguments, Variables).Result;
96 }
97
104 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
105 {
106 string ConnectionString = Arguments[0].AssociatedObjectValue?.ToString() ?? string.Empty;
107 SqlConnection Connection;
108
109 switch (Arguments.Length)
110 {
111 case 1:
112 default:
113 Connection = new SqlConnection(ConnectionString);
114 break;
115
116 case 2:
117 string Database = Arguments[1].AssociatedObjectValue?.ToString() ?? string.Empty;
118
119 ConnectionString = "Data Source=" + ConnectionString + ";Initial Catalog=" + Database + ";Integrated Security=true";
120 Connection = new SqlConnection(ConnectionString);
121 break;
122 case 3:
123 string UserName = Arguments[1].AssociatedObjectValue?.ToString() ?? string.Empty;
124 string Password = Arguments[2].AssociatedObjectValue?.ToString() ?? string.Empty;
125 SecureString Password2 = new SecureString();
126
127 foreach (char ch in Password)
128 Password2.AppendChar(ch);
129
130 Password2.MakeReadOnly();
131 Connection = new SqlConnection(ConnectionString, new SqlCredential(UserName, Password2));
132 break;
133
134 case 4:
135 Database = Arguments[1].AssociatedObjectValue?.ToString() ?? string.Empty;
136 UserName = Arguments[2].AssociatedObjectValue?.ToString() ?? string.Empty;
137 Password = Arguments[3].AssociatedObjectValue?.ToString() ?? string.Empty;
138 Password2 = new SecureString();
139
140 foreach (char ch in Password)
141 Password2.AppendChar(ch);
142
143 Password2.MakeReadOnly();
144
145 ConnectionString = "Data Source=" + ConnectionString + ";Initial Catalog=" + Database;
146 Connection = new SqlConnection(ConnectionString, new SqlCredential(UserName, Password2));
147 break;
148 }
149
150 await Connection.OpenAsync();
151
152 return new ObjectValue(new MsSqlDatabase(Connection));
153 }
154 }
155}
Creates a connection to an external MS SQL database.
Definition: ConnectMsSql.cs:16
ConnectMsSql(ScriptNode ConnectionString, ScriptNode UserName, ScriptNode Password, int Start, int Length, Expression Expression)
Creates a connection to an external MS SQL database.
Definition: ConnectMsSql.cs:51
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ConnectMsSql.cs:85
ConnectMsSql(ScriptNode Host, ScriptNode Database, ScriptNode UserName, ScriptNode Password, int Start, int Length, Expression Expression)
Creates a connection to an external MS SQL database.
Definition: ConnectMsSql.cs:66
ConnectMsSql(ScriptNode ConnectionString, int Start, int Length, Expression Expression)
Creates a connection to an external MS SQL database.
Definition: ConnectMsSql.cs:24
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: ConnectMsSql.cs:93
override string FunctionName
Name of the function
Definition: ConnectMsSql.cs:74
override string[] DefaultArgumentNames
Default Argument names
Definition: ConnectMsSql.cs:79
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
ConnectMsSql(ScriptNode Host, ScriptNode Database, int Start, int Length, Expression Expression)
Creates a connection to an external MS SQL database.
Definition: ConnectMsSql.cs:37
Manages a Microsoft SQL Server connection
Class managing a script expression.
Definition: Expression.cs:39
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
static readonly ArgumentType[] argumentTypes4Scalar
Four scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20