Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MsSqlDatabase.cs
1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.Data.SqlClient;
5using System.Threading.Tasks;
9
11{
16 {
17 private readonly Dictionary<string, MsSqlStoredProcedure> procedures = new Dictionary<string, MsSqlStoredProcedure>();
18 private readonly MultiReadSingleWriteObject synchObject;
19 private SqlConnection connection;
20
25 public MsSqlDatabase(SqlConnection Connection)
26 {
27 this.synchObject = new MultiReadSingleWriteObject(this);
28 this.connection = Connection;
29 }
30
34 public override void Dispose()
35 {
36 this.connection?.Close();
37 this.connection?.Dispose();
38 this.connection = null;
39 }
40
46 public override async Task<IElement> ExecuteSqlStatement(string Statement)
47 {
48 using (SqlCommand Command = this.connection.CreateCommand())
49 {
50 Command.CommandType = CommandType.Text;
51 Command.CommandText = Statement;
52 SqlDataReader Reader = await Command.ExecuteReaderAsync();
53
54 return await Reader.ParseAndClose();
55 }
56 }
57
64 public override Task<IElement> GetSchema(string Name)
65 {
66 DataTable Table = this.connection.GetSchema(Name);
67 return Task.FromResult<IElement>(Table.ToMatrix());
68 }
69
75 public override async Task<ILambdaExpression> GetProcedure(string Name)
76 {
77 await this.synchObject.BeginWrite();
78 try
79 {
80 if (this.procedures.TryGetValue(Name, out MsSqlStoredProcedure Result))
81 return Result;
82
83 SqlCommand Command = this.connection.CreateCommand();
84 Command.CommandType = CommandType.StoredProcedure;
85 Command.CommandText = this.connection.Database + "." + Name;
86
87 SqlCommandBuilder.DeriveParameters(Command);
88
89 Result = new MsSqlStoredProcedure(Command);
90 this.procedures[Name] = Result;
91
92 return Result;
93 }
94 finally
95 {
96 await this.synchObject.EndWrite();
97 }
98 }
99 }
100}
Represents an object that allows single concurrent writers but multiple concurrent readers....
Abstract base class for external databases
Manages a Microsoft SQL Server connection
override async Task< ILambdaExpression > GetProcedure(string Name)
Creates a lambda expression for accessing a stored procedure.
override Task< IElement > GetSchema(string Name)
Gets a Schema table, given its collection name. For a list of collections: https://mysqlconnector....
MsSqlDatabase(SqlConnection Connection)
Manages a Microsoft SQL Server connection
override async Task< IElement > ExecuteSqlStatement(string Statement)
Executes an SQL Statement on the database.
override void Dispose()
IDisposable.Dispose
Represents a stored precedure in a MS SQL Database.
Basic interface for all types of elements.
Definition: IElement.cs:20