Added SQL Interface to NAV Object table with support for serialization. Tested this.

This commit is contained in:
Torben Nehmer 2016-11-20 21:34:21 +01:00
parent f816138188
commit e2eb504261
11 changed files with 608 additions and 1 deletions

View File

@ -54,6 +54,8 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@ -4,6 +4,12 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using System.Runtime.Serialization;
using System.Xml;
using System.Data.Linq;
using NavScm;
using NavScm.NavInterface;
using System.IO;
namespace NavScm.TestHost
{
@ -17,6 +23,47 @@ namespace NavScm.TestHost
log.Info("Starting up...");
var context = new NavSQLDataContext("Data Source=tbrt-sql-erp-01;Initial Catalog=\"TERRABIT 2015 DEV\";Integrated Security=True");
var NavSqlObjects = context.NavObject;
log.InfoFormat("{0} total entries in NavSqlObjects", NavSqlObjects.Count());
var query = from sql in NavSqlObjects
where sql.Modified == 1
select sql;
int count = query.Count();
log.InfoFormat("{0} modified objects detected, reading them...", count);
var foundObjects = new Dictionary<string, NavObject>(count);
int i = 1;
foreach (NavObject o in query)
{
log.DebugFormat("Row {6}/{7}: Type {0}, ID {1}, Name {2}, Modified {3} {4}, Version {5}",
o.Type, o.ID, o.Name, o.Date.ToShortDateString(), o.Time.ToShortTimeString(), o.Version_List, i++, count);
foundObjects.Add(String.Format("{0}.{1}", o.Type, o.ID), o);
}
log.InfoFormat("Collection has {0} entries, writing to cache.xml", foundObjects.Count);
FileStream writer = new FileStream("cache.xml", FileMode.Create);
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, NavObject>));
serializer.WriteObject(writer, foundObjects);
writer.Close();
log.Info("Reloading these entries");
FileStream reader = new FileStream("cache.xml", FileMode.Open);
XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateTextReader(reader, new XmlDictionaryReaderQuotas());
var loadedObjects = (Dictionary<string, NavObject>)serializer.ReadObject(xmlReader, true);
log.Debug("Dumping sample object 1.82004");
NavObject o2 = loadedObjects["1.82004"];
log.DebugFormat("Type {0}, ID {1}, Name {2}, Modified {3} {4}, Version {5}",
o2.Type, o2.ID, o2.Name, o2.Date.ToShortDateString(), o2.Time.ToShortTimeString(), o2.Version_List);
log.Info("Shutting down...");

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="TERRABIT_2015_DEVEntities" connectionString="metadata=res://*/NavScm.NavInterface.SQL.Model1.csdl|res://*/NavScm.NavInterface.SQL.Model1.ssdl|res://*/NavScm.NavInterface.SQL.Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=tbrt-sql-erp-01;initial catalog=&quot;TERRABIT 2015 DEV&quot;;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>

View File

@ -37,7 +37,11 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -46,13 +50,41 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NavInterface\NavSQL.designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>NavSQL.dbml</DependentUpon>
</Compile>
<Compile Include="NavInterface\NavObject.cs" />
<Compile Include="NavInterface\NavSQLDataContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="NavInterface\NavSQL.dbml">
<Generator>MSLinqToSQLGenerator</Generator>
<LastGenOutput>NavSQL.designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="NavScm.NavInterface\" />
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<None Include="NavInterface\NavSQL.dbml.layout">
<DependentUpon>NavSQL.dbml</DependentUpon>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -0,0 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NavScm.NavInterface
{
partial class NavObject
{
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?><Database Name="TERRABIT 2015 DEV" EntityNamespace="NavScm.NavInterface" ContextNamespace="NavScm.NavInterface" Class="NavSQLDataContext" Serialization="Unidirectional" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
<Table Name="dbo.Object" Member="NavObject">
<Type Name="NavObject">
<Column Name="timestamp" Type="System.Data.Linq.Binary" DbType="rowversion NOT NULL" IsReadOnly="true" CanBeNull="false" IsVersion="true" />
<Column Name="Type" Type="System.Int32" DbType="Int NOT NULL" IsReadOnly="true" IsPrimaryKey="true" CanBeNull="false" />
<Column Name="[Company Name]" Member="Company_Name" Type="System.String" DbType="VarChar(30) NOT NULL" IsReadOnly="true" IsPrimaryKey="true" CanBeNull="false" />
<Column Name="ID" Type="System.Int32" DbType="Int NOT NULL" IsReadOnly="true" IsPrimaryKey="true" CanBeNull="false" />
<Column Name="Name" Type="System.String" DbType="VarChar(30) NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="Modified" Type="System.Byte" DbType="TinyInt NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="Compiled" Type="System.Byte" DbType="TinyInt NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="Date" Type="System.DateTime" DbType="DateTime NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="Time" Type="System.DateTime" DbType="DateTime NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="[Version List]" Member="Version_List" Type="System.String" DbType="VarChar(248) NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="Locked" Type="System.Byte" DbType="TinyInt NOT NULL" IsReadOnly="true" CanBeNull="false" />
<Column Name="[Locked By]" Member="Locked_By" Type="System.String" DbType="VarChar(132) NOT NULL" IsReadOnly="true" CanBeNull="false" />
</Type>
</Table>
</Database>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<ordesignerObjectsDiagram dslVersion="1.0.0.0" absoluteBounds="0, 0, 11, 8.5" name="NavSQL">
<DataContextMoniker Name="/NavSQLDataContext" />
<nestedChildShapes>
<classShape Id="8c6e51fb-7d5f-4746-8740-0eda298f32a6" absoluteBounds="1.875, 0.75, 2, 3.1170068359375">
<DataClassMoniker Name="/NavSQLDataContext/NavObject" />
<nestedChildShapes>
<elementListCompartment Id="4da92c62-b2ce-4bc2-9a36-eba03eac978d" absoluteBounds="1.8900000000000001, 1.21, 1.9700000000000002, 2.5570068359375" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
</nestedChildShapes>
</classShape>
</nestedChildShapes>
</ordesignerObjectsDiagram>

View File

@ -0,0 +1,423 @@
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:4.0.30319.42000
//
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NavScm.NavInterface
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="TERRABIT 2015 DEV")]
public partial class NavSQLDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Definitionen der Erweiterungsmethoden
partial void OnCreated();
partial void InsertNavObject(NavObject instance);
partial void UpdateNavObject(NavObject instance);
partial void DeleteNavObject(NavObject instance);
#endregion
public NavSQLDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NavSQLDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NavSQLDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public NavSQLDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<NavObject> NavObject
{
get
{
return this.GetTable<NavObject>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Object")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class NavObject : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Data.Linq.Binary _timestamp = default(System.Data.Linq.Binary);
private int _Type = default(int);
private string _Company_Name = default(string);
private int _ID = default(int);
private string _Name = default(string);
private byte _Modified = default(byte);
private byte _Compiled = default(byte);
private System.DateTime _Date = default(System.DateTime);
private System.DateTime _Time = default(System.DateTime);
private string _Version_List = default(string);
private byte _Locked = default(byte);
private string _Locked_By = default(string);
#region Definitionen der Erweiterungsmethoden
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OntimestampChanging(System.Data.Linq.Binary value);
partial void OntimestampChanged();
partial void OnTypeChanging(int value);
partial void OnTypeChanged();
partial void OnCompany_NameChanging(string value);
partial void OnCompany_NameChanged();
partial void OnIDChanging(int value);
partial void OnIDChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
partial void OnModifiedChanging(byte value);
partial void OnModifiedChanged();
partial void OnCompiledChanging(byte value);
partial void OnCompiledChanged();
partial void OnDateChanging(System.DateTime value);
partial void OnDateChanged();
partial void OnTimeChanging(System.DateTime value);
partial void OnTimeChanged();
partial void OnVersion_ListChanging(string value);
partial void OnVersion_ListChanged();
partial void OnLockedChanging(byte value);
partial void OnLockedChanged();
partial void OnLocked_ByChanging(string value);
partial void OnLocked_ByChanged();
#endregion
public NavObject()
{
this.Initialize();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_timestamp", AutoSync=AutoSync.Always, DbType="rowversion NOT NULL", CanBeNull=false, IsDbGenerated=true, IsVersion=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=1)]
public System.Data.Linq.Binary timestamp
{
get
{
return this._timestamp;
}
set
{
if ((this._timestamp != value))
{
this.OntimestampChanging(value);
this.SendPropertyChanging();
this._timestamp = value;
this.SendPropertyChanged("timestamp");
this.OntimestampChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Type", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=2)]
public int Type
{
get
{
return this._Type;
}
set
{
if ((this._Type != value))
{
this.OnTypeChanging(value);
this.SendPropertyChanging();
this._Type = value;
this.SendPropertyChanged("Type");
this.OnTypeChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="[Company Name]", Storage="_Company_Name", DbType="VarChar(30) NOT NULL", CanBeNull=false, IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=3)]
public string Company_Name
{
get
{
return this._Company_Name;
}
set
{
if ((this._Company_Name != value))
{
this.OnCompany_NameChanging(value);
this.SendPropertyChanging();
this._Company_Name = value;
this.SendPropertyChanged("Company_Name");
this.OnCompany_NameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", DbType="Int NOT NULL", IsPrimaryKey=true, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=4)]
public int ID
{
get
{
return this._ID;
}
set
{
if ((this._ID != value))
{
this.OnIDChanging(value);
this.SendPropertyChanging();
this._ID = value;
this.SendPropertyChanged("ID");
this.OnIDChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="VarChar(30) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=5)]
public string Name
{
get
{
return this._Name;
}
set
{
if ((this._Name != value))
{
this.OnNameChanging(value);
this.SendPropertyChanging();
this._Name = value;
this.SendPropertyChanged("Name");
this.OnNameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Modified", DbType="TinyInt NOT NULL", UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=6)]
public byte Modified
{
get
{
return this._Modified;
}
set
{
if ((this._Modified != value))
{
this.OnModifiedChanging(value);
this.SendPropertyChanging();
this._Modified = value;
this.SendPropertyChanged("Modified");
this.OnModifiedChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Compiled", DbType="TinyInt NOT NULL", UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=7)]
public byte Compiled
{
get
{
return this._Compiled;
}
set
{
if ((this._Compiled != value))
{
this.OnCompiledChanging(value);
this.SendPropertyChanging();
this._Compiled = value;
this.SendPropertyChanged("Compiled");
this.OnCompiledChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Date", DbType="DateTime NOT NULL", UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=8)]
public System.DateTime Date
{
get
{
return this._Date;
}
set
{
if ((this._Date != value))
{
this.OnDateChanging(value);
this.SendPropertyChanging();
this._Date = value;
this.SendPropertyChanged("Date");
this.OnDateChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Time", DbType="DateTime NOT NULL", UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=9)]
public System.DateTime Time
{
get
{
return this._Time;
}
set
{
if ((this._Time != value))
{
this.OnTimeChanging(value);
this.SendPropertyChanging();
this._Time = value;
this.SendPropertyChanged("Time");
this.OnTimeChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="[Version List]", Storage="_Version_List", DbType="VarChar(248) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=10)]
public string Version_List
{
get
{
return this._Version_List;
}
set
{
if ((this._Version_List != value))
{
this.OnVersion_ListChanging(value);
this.SendPropertyChanging();
this._Version_List = value;
this.SendPropertyChanged("Version_List");
this.OnVersion_ListChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Locked", DbType="TinyInt NOT NULL", UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=11)]
public byte Locked
{
get
{
return this._Locked;
}
set
{
if ((this._Locked != value))
{
this.OnLockedChanging(value);
this.SendPropertyChanging();
this._Locked = value;
this.SendPropertyChanged("Locked");
this.OnLockedChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="[Locked By]", Storage="_Locked_By", DbType="VarChar(132) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=12)]
public string Locked_By
{
get
{
return this._Locked_By;
}
set
{
if ((this._Locked_By != value))
{
this.OnLocked_ByChanging(value);
this.SendPropertyChanging();
this._Locked_By = value;
this.SendPropertyChanged("Locked_By");
this.OnLocked_ByChanged();
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Initialize()
{
OnCreated();
}
[global::System.Runtime.Serialization.OnDeserializingAttribute()]
[global::System.ComponentModel.EditorBrowsableAttribute(EditorBrowsableState.Never)]
public void OnDeserializing(StreamingContext context)
{
this.Initialize();
}
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NavScm.NavInterface
{
/// <summary>
/// Default data context to interact with NAV Databases, Object tracking is disabled
/// to avoid any caching problems throught program execution. If we query the DB, it
/// should always be executed.
/// </summary>
public partial class NavSQLDataContext
{
partial void OnCreated()
{
ObjectTrackingEnabled = false;
}
}
}

View File

@ -0,0 +1,26 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:4.0.30319.42000
//
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NavScm.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles />
<Settings />
</SettingsFile>