/// <summary> /// Saves <see cref="Historian"/> information to database. /// </summary> /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param> /// <param name="historian">Infomration about <see cref="Historian"/>.</param> /// <returns>String, for display use, indicating success.</returns> public static string Save(AdoDataConnection database, Historian historian) { bool createdConnection = false; string query; try { createdConnection = CreateConnection(ref database); if (historian.ID == 0) { query = database.ParameterizedQueryString("INSERT INTO Historian (NodeID, Acronym, Name, AssemblyName, TypeName, ConnectionString, IsLocal, " + "MeasurementReportingInterval, Description, LoadOrder, Enabled, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) VALUES ({0}, {1}, {2}, {3}, {4}, " + "{5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14})", "nodeID", "acronym", "name", "assemblyName", "typeName", "connectionString", "isLocal", "measurementReportingInterval", "description", "loadOrder", "enabled", "updatedBy", "updatedOn", "createdBy", "createdOn"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, (historian.NodeID != Guid.Empty) ? database.Guid(historian.NodeID) : database.CurrentNodeID(), historian.Acronym.Replace(" ", "").ToUpper(), historian.Name.ToNotNull(), historian.AssemblyName.ToNotNull(), historian.TypeName.ToNotNull(), historian.ConnectionString.ToNotNull(), database.Bool(historian.IsLocal), historian.MeasurementReportingInterval, historian.Description.ToNotNull(), historian.LoadOrder, database.Bool(historian.Enabled), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow); } else { query = database.ParameterizedQueryString("UPDATE Historian SET NodeID = {0}, Acronym = {1}, Name = {2}, AssemblyName = {3}, TypeName = {4}, " + "ConnectionString = {5}, IsLocal = {6}, MeasurementReportingInterval = {7}, Description = {8}, LoadOrder = {9}, Enabled = {10}, " + "UpdatedBy = {11}, UpdatedOn = {12} WHERE ID = {13}", "nodeID", "acronym", "name", "assemblyName", "typeName", "connectionString", "isLocal", "measurementReportingInterval", "description", "loadOrder", "enabled", "updatedBy", "updatedOn", "id"); database.Connection.ExecuteNonQuery(query, DefaultTimeout, (historian.NodeID != Guid.Empty) ? database.Guid(historian.NodeID) : database.CurrentNodeID(), historian.Acronym.Replace(" ", "").ToUpper(), historian.Name.ToNotNull(), historian.AssemblyName.ToNotNull(), historian.TypeName.ToNotNull(), historian.ConnectionString.ToNotNull(), database.Bool(historian.IsLocal), historian.MeasurementReportingInterval, historian.Description.ToNotNull(), historian.LoadOrder, database.Bool(historian.Enabled), CommonFunctions.CurrentUser, database.UtcNow, historian.ID); } return "Historian information saved successfully"; } finally { if (createdConnection && database != null) database.Dispose(); } }
/// <summary> /// Retrieves a <see cref="Historian"/> information from the database. /// </summary> /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param> /// <param name="whereClause">Filter expression to filter data.</param> /// <returns><see cref="Historian"/> information.</returns> public static Historian GetHistorian(AdoDataConnection database, string whereClause) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); DataTable historianTable = database.Connection.RetrieveData(database.AdapterType, "SELECT * FROM HistorianDetail " + whereClause); if (historianTable.Rows.Count == 0) return null; DataRow row = historianTable.Rows[0]; Historian historian = new Historian { NodeID = database.Guid(row, "NodeID"), ID = row.ConvertField<int>("ID"), Acronym = row.Field<string>("Acronym"), Name = row.Field<string>("Name"), AssemblyName = row.Field<string>("AssemblyName"), TypeName = row.Field<string>("TypeName"), ConnectionString = row.Field<string>("ConnectionString"), IsLocal = Convert.ToBoolean(row.Field<object>("IsLocal")), Description = row.Field<string>("Description"), LoadOrder = row.ConvertField<int>("LoadOrder"), Enabled = Convert.ToBoolean(row.Field<object>("Enabled")), MeasurementReportingInterval = row.ConvertField<int>("MeasurementReportingInterval"), m_nodeName = row.Field<string>("NodeName") }; return historian; } finally { if (createdConnection && database != null) database.Dispose(); } }
/// <summary> /// Loads <see cref="Historian"/> information as an <see cref="ObservableCollection{T}"/> style list. /// </summary> /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param> /// <param name="keys">Keys to load, if any.</param> /// <returns>Collection of <see cref="Historian"/>.</returns> public static ObservableCollection<Historian> Load(AdoDataConnection database, IList<int> keys) { bool createdConnection = false; try { createdConnection = CreateConnection(ref database); string query; string commaSeparatedKeys; Historian[] historianList = null; DataTable historianTable; int id; if ((object)keys != null && keys.Count > 0) { commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2); query = database.ParameterizedQueryString(string.Format("SELECT NodeID, ID, Acronym, Name, AssemblyName, TypeName, " + "ConnectionString, IsLocal, Description, LoadOrder, Enabled, MeasurementReportingInterval, NodeName" + " FROM HistorianDetail WHERE NodeID = {{0}} AND ID IN ({0})", commaSeparatedKeys), "nodeID"); historianTable = database.Connection.RetrieveData(database.AdapterType, query, DefaultTimeout, database.CurrentNodeID()); historianList = new Historian[historianTable.Rows.Count]; foreach (DataRow row in historianTable.Rows) { id = row.ConvertField<int>("ID"); historianList[keys.IndexOf(id)] = new Historian() { NodeID = database.Guid(row, "NodeID"), ID = id, Acronym = row.Field<string>("Acronym"), Name = row.Field<string>("Name"), AssemblyName = row.Field<string>("AssemblyName"), TypeName = row.Field<string>("TypeName"), ConnectionString = row.Field<string>("ConnectionString"), IsLocal = Convert.ToBoolean(row.Field<object>("IsLocal")), Description = row.Field<string>("Description"), LoadOrder = row.ConvertField<int>("LoadOrder"), Enabled = Convert.ToBoolean(row.Field<object>("Enabled")), MeasurementReportingInterval = row.ConvertField<int>("MeasurementReportingInterval"), m_nodeName = row.Field<string>("NodeName") }; } } return new ObservableCollection<Historian>(historianList ?? new Historian[0]); } finally { if (createdConnection && database != null) database.Dispose(); } }