diff --git a/README.md b/README.md index 4913110..de846f5 100644 --- a/README.md +++ b/README.md @@ -1,77 +1,100 @@ -# How to bind the SQL Database in UWP Chart (SfChart)? +# How to bind the SQL Database in UWP Chart? This example illustrates how to establish the SQL connection and bind the retrieving data from database in a step by step process: -**Step 1:** Retrieve the data table from the SQL DataSet using the connection string. -``` +**Step 1:** Create a ViewModel class that establishes a connection to your SQLite database, executes a query, and retrieves the data into an ObservableCollection. The database file is created and seeded in the app’s local folder on first run in this example. +```csharp public class ViewModel { - public ViewModel() - { - try - { - SqlConnection thisConnection = new SqlConnection(ConnectionString); - thisConnection.Open(); - string Get_Data = "SELECT * FROM ChartData"; - SqlCommand cmd = thisConnection.CreateCommand(); - cmd.CommandText = Get_Data; - SqlDataAdapter sda = new SqlDataAdapter(cmd); - DataSet ds = new DataSet(); - sda.Fill(ds); - var table = ds.Tables[0]; - this.DataTable = table; - } - catch - { - return; - } - } + public ObservableCollection DataTable { get; } = new ObservableCollection(); - public object DataTable { get; set; } + . . . - public static string ConnectionString + // Creates a SQLite DB in LocalFolder on first run and seeds simple rows + private static async Task EnsureAndSeedDatabaseAsync(string dbFileName) { - get + var localFolder = ApplicationData.Current.LocalFolder; + var existing = await localFolder.TryGetItemAsync(dbFileName); + var dbPath = Path.Combine(localFolder.Path, dbFileName); + + if (existing == null) { - string currentDir = System.Environment.CurrentDirectory; - currentDir = currentDir.Substring(0, currentDir.Length - 10) + "\\LocalDataBase"; - return @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + currentDir + @"\SeriesItemsSource.mdf;Integrated Security=True"; + using (var connection = new SqliteConnection($"Data Source={dbPath}")) + { + await connection.OpenAsync(); + + // Create table + using (var create = connection.CreateCommand()) + { + create.CommandText = @" + CREATE TABLE IF NOT EXISTS ChartData ( + xValue REAL NOT NULL, + yValue REAL NOT NULL + );"; + await create.ExecuteNonQueryAsync(); + } + + // Seed a few sample points + using (var tx = connection.BeginTransaction()) + using (var insert = connection.CreateCommand()) + { + insert.CommandText = "INSERT INTO ChartData (xValue, yValue) VALUES ($x, $y)"; + var px = insert.CreateParameter(); px.ParameterName = "$x"; insert.Parameters.Add(px); + var py = insert.CreateParameter(); py.ParameterName = "$y"; insert.Parameters.Add(py); + + var points = new (double x, double y)[] + { + (1, 10), (2, 14), (3, 9), (4, 18), (5, 22), (6, 17), (7, 25) + }; + + foreach (var (x, y) in points) + { + px.Value = x; + py.Value = y; + await insert.ExecuteNonQueryAsync(); + } + + tx.Commit(); + } + } } + + return dbPath; } } ``` -**Step 2:** In the main page, initialize the SfChart control and bind the retrieved data. -``` +**Step 2:** Set up the [SfChart](https://help.syncfusion.com/cr/uwp/Syncfusion.UI.Xaml.Charts.SfChart.html) control with appropriate axes and bind the ItemsSource of a chart series to the DataTable property from your ViewModel. Specify the XBindingPath and YBindingPath to map to the respective columns in your database table. +```xml -        -            -        -        -        -            -            -                -            -  -            -                -            -  -            -        -        + + + + + + + + + + + + + + + + ``` -## Output: +## Output +image -![SQL DataBinding to UWP SfChart](https://user-images.githubusercontent.com/53489303/200761566-1e210d5c-0740-4a08-9b58-8cc264ff0691.png) +## Troubleshooting -KB article - [How to bind the SQL Database in UWP Chart (SfChart)?](https://www.syncfusion.com/kb/11664/how-to-bind-the-sql-database-in-uwp-chart) +### Path Too Long Exception -## See also +If you are facing a "Path too long" exception when building this example project, close Visual Studio and rename the repository to a shorter name before building the project. -[How to bind the SQL Database to WPF Charts](https://www.syncfusion.com/kb/11595/how-to-bind-the-sql-database-to-wpf-charts) +Refer to the knowledge base article [How to bind the SQL Database in UWP Chart](https://support.syncfusion.com/kb/article/10133/how-to-bind-the-sql-database-in-uwp-chart). diff --git a/SQLwithChartUWP/App.xaml b/SQLiteChartBinding_UWP/App.xaml similarity index 64% rename from SQLwithChartUWP/App.xaml rename to SQLiteChartBinding_UWP/App.xaml index a0d35a5..6ddee5f 100644 --- a/SQLwithChartUWP/App.xaml +++ b/SQLiteChartBinding_UWP/App.xaml @@ -1,7 +1,7 @@ - - - + + + diff --git a/SQLwithChartUWP/App.xaml.cs b/SQLiteChartBinding_UWP/App.xaml.cs similarity index 96% rename from SQLwithChartUWP/App.xaml.cs rename to SQLiteChartBinding_UWP/App.xaml.cs index 11069e1..e166ead 100644 --- a/SQLwithChartUWP/App.xaml.cs +++ b/SQLiteChartBinding_UWP/App.xaml.cs @@ -1,100 +1,100 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.ApplicationModel; -using Windows.ApplicationModel.Activation; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; - -namespace SQLLiteChartBinding -{ - /// - /// Provides application-specific behavior to supplement the default Application class. - /// - sealed partial class App : Application - { - /// - /// Initializes the singleton application object. This is the first line of authored code - /// executed, and as such is the logical equivalent of main() or WinMain(). - /// - public App() - { - this.InitializeComponent(); - this.Suspending += OnSuspending; - } - - /// - /// Invoked when the application is launched normally by the end user. Other entry points - /// will be used such as when the application is launched to open a specific file. - /// - /// Details about the launch request and process. - protected override void OnLaunched(LaunchActivatedEventArgs e) - { - Frame rootFrame = Window.Current.Content as Frame; - - // Do not repeat app initialization when the Window already has content, - // just ensure that the window is active - if (rootFrame == null) - { - // Create a Frame to act as the navigation context and navigate to the first page - rootFrame = new Frame(); - - rootFrame.NavigationFailed += OnNavigationFailed; - - if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) - { - //TODO: Load state from previously suspended application - } - - // Place the frame in the current Window - Window.Current.Content = rootFrame; - } - - if (e.PrelaunchActivated == false) - { - if (rootFrame.Content == null) - { - // When the navigation stack isn't restored navigate to the first page, - // configuring the new page by passing required information as a navigation - // parameter - rootFrame.Navigate(typeof(MainPage), e.Arguments); - } - // Ensure the current window is active - Window.Current.Activate(); - } - } - - /// - /// Invoked when Navigation to a certain page fails - /// - /// The Frame which failed navigation - /// Details about the navigation failure - void OnNavigationFailed(object sender, NavigationFailedEventArgs e) - { - throw new Exception("Failed to load Page " + e.SourcePageType.FullName); - } - - /// - /// Invoked when application execution is being suspended. Application state is saved - /// without knowing whether the application will be terminated or resumed with the contents - /// of memory still intact. - /// - /// The source of the suspend request. - /// Details about the suspend request. - private void OnSuspending(object sender, SuspendingEventArgs e) - { - var deferral = e.SuspendingOperation.GetDeferral(); - //TODO: Save application state and stop any background activity - deferral.Complete(); - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.ApplicationModel; +using Windows.ApplicationModel.Activation; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +namespace SQLiteChartBinding_UWP +{ + /// + /// Provides application-specific behavior to supplement the default Application class. + /// + sealed partial class App : Application + { + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() + { + this.InitializeComponent(); + this.Suspending += OnSuspending; + } + + /// + /// Invoked when the application is launched normally by the end user. Other entry points + /// will be used such as when the application is launched to open a specific file. + /// + /// Details about the launch request and process. + protected override void OnLaunched(LaunchActivatedEventArgs e) + { + Frame rootFrame = Window.Current.Content as Frame; + + // Do not repeat app initialization when the Window already has content, + // just ensure that the window is active + if (rootFrame == null) + { + // Create a Frame to act as the navigation context and navigate to the first page + rootFrame = new Frame(); + + rootFrame.NavigationFailed += OnNavigationFailed; + + if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) + { + //TODO: Load state from previously suspended application + } + + // Place the frame in the current Window + Window.Current.Content = rootFrame; + } + + if (e.PrelaunchActivated == false) + { + if (rootFrame.Content == null) + { + // When the navigation stack isn't restored navigate to the first page, + // configuring the new page by passing required information as a navigation + // parameter + rootFrame.Navigate(typeof(MainPage), e.Arguments); + } + // Ensure the current window is active + Window.Current.Activate(); + } + } + + /// + /// Invoked when Navigation to a certain page fails + /// + /// The Frame which failed navigation + /// Details about the navigation failure + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) + { + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); + } + + /// + /// Invoked when application execution is being suspended. Application state is saved + /// without knowing whether the application will be terminated or resumed with the contents + /// of memory still intact. + /// + /// The source of the suspend request. + /// Details about the suspend request. + private void OnSuspending(object sender, SuspendingEventArgs e) + { + var deferral = e.SuspendingOperation.GetDeferral(); + //TODO: Save application state and stop any background activity + deferral.Complete(); + } + } +} diff --git a/SQLwithChartUWP/Assets/LockScreenLogo.scale-200.png b/SQLiteChartBinding_UWP/Assets/LockScreenLogo.scale-200.png similarity index 100% rename from SQLwithChartUWP/Assets/LockScreenLogo.scale-200.png rename to SQLiteChartBinding_UWP/Assets/LockScreenLogo.scale-200.png diff --git a/SQLwithChartUWP/Assets/SplashScreen.scale-200.png b/SQLiteChartBinding_UWP/Assets/SplashScreen.scale-200.png similarity index 100% rename from SQLwithChartUWP/Assets/SplashScreen.scale-200.png rename to SQLiteChartBinding_UWP/Assets/SplashScreen.scale-200.png diff --git a/SQLwithChartUWP/Assets/Square150x150Logo.scale-200.png b/SQLiteChartBinding_UWP/Assets/Square150x150Logo.scale-200.png similarity index 100% rename from SQLwithChartUWP/Assets/Square150x150Logo.scale-200.png rename to SQLiteChartBinding_UWP/Assets/Square150x150Logo.scale-200.png diff --git a/SQLwithChartUWP/Assets/Square44x44Logo.scale-200.png b/SQLiteChartBinding_UWP/Assets/Square44x44Logo.scale-200.png similarity index 100% rename from SQLwithChartUWP/Assets/Square44x44Logo.scale-200.png rename to SQLiteChartBinding_UWP/Assets/Square44x44Logo.scale-200.png diff --git a/SQLwithChartUWP/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/SQLiteChartBinding_UWP/Assets/Square44x44Logo.targetsize-24_altform-unplated.png similarity index 100% rename from SQLwithChartUWP/Assets/Square44x44Logo.targetsize-24_altform-unplated.png rename to SQLiteChartBinding_UWP/Assets/Square44x44Logo.targetsize-24_altform-unplated.png diff --git a/SQLwithChartUWP/Assets/StoreLogo.png b/SQLiteChartBinding_UWP/Assets/StoreLogo.png similarity index 100% rename from SQLwithChartUWP/Assets/StoreLogo.png rename to SQLiteChartBinding_UWP/Assets/StoreLogo.png diff --git a/SQLwithChartUWP/Assets/Wide310x150Logo.scale-200.png b/SQLiteChartBinding_UWP/Assets/Wide310x150Logo.scale-200.png similarity index 100% rename from SQLwithChartUWP/Assets/Wide310x150Logo.scale-200.png rename to SQLiteChartBinding_UWP/Assets/Wide310x150Logo.scale-200.png diff --git a/SQLwithChartUWP/MainPage.xaml b/SQLiteChartBinding_UWP/MainPage.xaml similarity index 70% rename from SQLwithChartUWP/MainPage.xaml rename to SQLiteChartBinding_UWP/MainPage.xaml index 621a949..f811617 100644 --- a/SQLwithChartUWP/MainPage.xaml +++ b/SQLiteChartBinding_UWP/MainPage.xaml @@ -1,34 +1,32 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/SQLiteChartBinding_UWP/MainPage.xaml.cs b/SQLiteChartBinding_UWP/MainPage.xaml.cs new file mode 100644 index 0000000..0b4ff34 --- /dev/null +++ b/SQLiteChartBinding_UWP/MainPage.xaml.cs @@ -0,0 +1,17 @@ +using Windows.UI.Xaml.Controls; + +// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 + +namespace SQLiteChartBinding_UWP +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class MainPage : Page + { + public MainPage() + { + InitializeComponent(); + } + } +} diff --git a/SQLwithChartUWP/Package.appxmanifest b/SQLiteChartBinding_UWP/Package.appxmanifest similarity index 69% rename from SQLwithChartUWP/Package.appxmanifest rename to SQLiteChartBinding_UWP/Package.appxmanifest index 09963b7..4ad20de 100644 --- a/SQLwithChartUWP/Package.appxmanifest +++ b/SQLiteChartBinding_UWP/Package.appxmanifest @@ -1,49 +1,49 @@ - - - - - - - - - - SQLLiteChartBinding - RachelA - Assets\StoreLogo.png - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + SQLiteChartBinding_UWP + Subash + Assets\StoreLogo.png + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SQLwithChartUWP/Properties/AssemblyInfo.cs b/SQLiteChartBinding_UWP/Properties/AssemblyInfo.cs similarity index 65% rename from SQLwithChartUWP/Properties/AssemblyInfo.cs rename to SQLiteChartBinding_UWP/Properties/AssemblyInfo.cs index f80745b..51bdcdd 100644 --- a/SQLwithChartUWP/Properties/AssemblyInfo.cs +++ b/SQLiteChartBinding_UWP/Properties/AssemblyInfo.cs @@ -1,29 +1,27 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("SQLLiteChartBinding")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SQLLiteChartBinding")] -[assembly: AssemblyCopyright("Copyright © 2020")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: ComVisible(false)] \ No newline at end of file +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SQLiteChartBinding_UWP")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SQLiteChartBinding_UWP")] +[assembly: AssemblyCopyright("Copyright © 2025")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// + +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] diff --git a/SQLwithChartUWP/Properties/Default.rd.xml b/SQLiteChartBinding_UWP/Properties/Default.rd.xml similarity index 97% rename from SQLwithChartUWP/Properties/Default.rd.xml rename to SQLiteChartBinding_UWP/Properties/Default.rd.xml index 74b03e3..af00722 100644 --- a/SQLwithChartUWP/Properties/Default.rd.xml +++ b/SQLiteChartBinding_UWP/Properties/Default.rd.xml @@ -1,31 +1,31 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/SQLwithChartUWP/SQLLiteChartBinding.csproj b/SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.csproj similarity index 87% rename from SQLwithChartUWP/SQLLiteChartBinding.csproj rename to SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.csproj index 84fe992..8086f5e 100644 --- a/SQLwithChartUWP/SQLLiteChartBinding.csproj +++ b/SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.csproj @@ -1,183 +1,178 @@ - - - - - Debug - x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67} - AppContainerExe - Properties - SQLLiteChartBinding - SQLLiteChartBinding - en-US - UAP - 10.0.17134.0 - 10.0.17134.0 - 14 - 512 - {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - true - false - - - true - bin\x86\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x86 - false - prompt - true - - - bin\x86\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x86 - false - prompt - true - true - - - true - bin\ARM\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM - false - prompt - true - - - bin\ARM\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM - false - prompt - true - true - - - true - bin\ARM64\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - ARM64 - false - prompt - true - true - - - bin\ARM64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - ARM64 - false - prompt - true - true - - - true - bin\x64\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP - ;2008 - full - x64 - false - prompt - true - - - bin\x64\Release\ - TRACE;NETFX_CORE;WINDOWS_UWP - true - ;2008 - pdbonly - x64 - false - prompt - true - true - - - PackageReference - - - - App.xaml - - - MainPage.xaml - - - - - - Designer - - - - - SeriesItemsSource.mdf - Always - - - Always - - - - - - - - - - - - - MSBuild:Compile - Designer - - - MSBuild:Compile - Designer - - - - - 3.1.5 - - - 6.2.10 - - - - - - - - - 14.0 - - - + + + + + Debug + x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5} + AppContainerExe + Properties + SQLiteChartBinding_UWP + SQLiteChartBinding_UWP + en-US + UAP + 10.0.26100.0 + 10.0.17763.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true + false + + + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + true + + + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + true + true + + + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + true + + + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + true + true + + + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM64 + false + prompt + true + true + + + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM64 + false + prompt + true + true + + + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + true + + + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + true + true + + + PackageReference + + + + App.xaml + + + MainPage.xaml + + + + + + + Designer + + + + + + + + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + + + 10.0.1 + + + 6.2.14 + + + 2.1.11 + + + * + + + + 14.0 + + + \ No newline at end of file diff --git a/SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.sln b/SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.sln new file mode 100644 index 0000000..9239938 --- /dev/null +++ b/SQLiteChartBinding_UWP/SQLiteChartBinding_UWP.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11222.15 d18.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteChartBinding_UWP", "SQLiteChartBinding_UWP.csproj", "{4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|ARM = Debug|ARM + Debug|ARM64 = Debug|ARM64 + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|ARM = Release|ARM + Release|ARM64 = Release|ARM64 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM.ActiveCfg = Debug|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM.Build.0 = Debug|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM.Deploy.0 = Debug|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM64.Build.0 = Debug|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|ARM64.Deploy.0 = Debug|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x64.ActiveCfg = Debug|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x64.Build.0 = Debug|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x64.Deploy.0 = Debug|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x86.ActiveCfg = Debug|x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x86.Build.0 = Debug|x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Debug|x86.Deploy.0 = Debug|x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM.ActiveCfg = Release|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM.Build.0 = Release|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM.Deploy.0 = Release|ARM + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM64.ActiveCfg = Release|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM64.Build.0 = Release|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|ARM64.Deploy.0 = Release|ARM64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x64.ActiveCfg = Release|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x64.Build.0 = Release|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x64.Deploy.0 = Release|x64 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x86.ActiveCfg = Release|x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x86.Build.0 = Release|x86 + {4A8814AF-1EB9-4831-B2B9-8190AE4BD1B5}.Release|x86.Deploy.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F5BD1C80-E327-4B0D-AFE8-9C88C19EF62C} + EndGlobalSection +EndGlobal diff --git a/SQLiteChartBinding_UWP/ViewModel/ViewModel.cs b/SQLiteChartBinding_UWP/ViewModel/ViewModel.cs new file mode 100644 index 0000000..dc1ee1d --- /dev/null +++ b/SQLiteChartBinding_UWP/ViewModel/ViewModel.cs @@ -0,0 +1,111 @@ +using Microsoft.Data.Sqlite; +using System; +using System.Collections.ObjectModel; +using System.IO; +using System.Threading.Tasks; +using Windows.Storage; + +namespace SQLiteChartBinding_UWP +{ + public class ViewModel + { + public ObservableCollection DataTable { get; } = new ObservableCollection(); + + public ViewModel() + { + _ = InitializeAsync(); + } + + private async Task InitializeAsync() + { + try + { + string dbPath = await EnsureAndSeedDatabaseAsync("DataSource.sqlite"); + + using (var connection = new SqliteConnection($"Data Source={dbPath}")) + { + await connection.OpenAsync(); + + using (var cmd = connection.CreateCommand()) + { + cmd.CommandText = "SELECT xValue, yValue FROM ChartData"; + using (var reader = await cmd.ExecuteReaderAsync()) + { + while (await reader.ReadAsync()) + { + DataTable.Add(new ChartDataItem + { + XValue = reader.GetDouble(0), + YValue = reader.GetDouble(1) + }); + } + } + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine("SQLite init/load error: " + ex.Message); + } + } + + // Creates a SQLite DB in LocalFolder on first run and seeds simple rows + private static async Task EnsureAndSeedDatabaseAsync(string dbFileName) + { + var localFolder = ApplicationData.Current.LocalFolder; + var existing = await localFolder.TryGetItemAsync(dbFileName); + var dbPath = Path.Combine(localFolder.Path, dbFileName); + + if (existing == null) + { + using (var connection = new SqliteConnection($"Data Source={dbPath}")) + { + await connection.OpenAsync(); + + // Create table + using (var create = connection.CreateCommand()) + { + create.CommandText = @" + CREATE TABLE IF NOT EXISTS ChartData ( + xValue REAL NOT NULL, + yValue REAL NOT NULL + );"; + await create.ExecuteNonQueryAsync(); + } + + // Seed a few sample points + using (var tx = connection.BeginTransaction()) + using (var insert = connection.CreateCommand()) + { + insert.CommandText = "INSERT INTO ChartData (xValue, yValue) VALUES ($x, $y)"; + var px = insert.CreateParameter(); px.ParameterName = "$x"; insert.Parameters.Add(px); + var py = insert.CreateParameter(); py.ParameterName = "$y"; insert.Parameters.Add(py); + + var points = new (double x, double y)[] + { + (1, 10), (2, 14), (3, 9), (4, 18), (5, 22), (6, 17), (7, 25) + }; + + foreach (var (x, y) in points) + { + px.Value = x; + py.Value = y; + await insert.ExecuteNonQueryAsync(); + } + + tx.Commit(); + } + } + } + + return dbPath; + } + } + + public class ChartDataItem + { + public double XValue { get; set; } + + public double YValue { get; set; } + } +} diff --git a/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.ldf b/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.ldf deleted file mode 100644 index 6601bfd..0000000 Binary files a/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.ldf and /dev/null differ diff --git a/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.mdf b/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.mdf deleted file mode 100644 index 0351e0a..0000000 Binary files a/SQLwithChartUWP/LocalDataBase/SeriesItemsSource.mdf and /dev/null differ diff --git a/SQLwithChartUWP/LocalDataBase/desktop.ini b/SQLwithChartUWP/LocalDataBase/desktop.ini deleted file mode 100644 index bb9f3d6..0000000 --- a/SQLwithChartUWP/LocalDataBase/desktop.ini +++ /dev/null @@ -1,4 +0,0 @@ -[ViewState] -Mode= -Vid= -FolderType=Generic diff --git a/SQLwithChartUWP/MainPage.xaml.cs b/SQLwithChartUWP/MainPage.xaml.cs deleted file mode 100644 index 6d2d74b..0000000 --- a/SQLwithChartUWP/MainPage.xaml.cs +++ /dev/null @@ -1,73 +0,0 @@ -using Microsoft.Data.Sqlite; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Data; -using System.Data.SqlClient; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; - - -// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 - -namespace SQLLiteChartBinding -{ - /// - /// An empty page that can be used on its own or navigated to within a Frame. - /// - public sealed partial class MainPage : Page - { - - public MainPage() - { - this.InitializeComponent(); - - } - } - - public class ViewModel - { - public ViewModel() - { - try - { - SqlConnection thisConnection = new SqlConnection(ConnectionString); - thisConnection.Open(); - string Get_Data = "SELECT * FROM ChartData"; - SqlCommand cmd = thisConnection.CreateCommand(); - cmd.CommandText = Get_Data; - SqlDataAdapter sda = new SqlDataAdapter(cmd); - DataSet ds = new DataSet(); - sda.Fill(ds); - var table = ds.Tables[0]; - this.DataTable = table; - } - catch - { - return; - } - } - - public object DataTable { get; set; } - - public static string ConnectionString - { - get - { - string currentDir = System.Environment.CurrentDirectory; - currentDir = currentDir.Substring(0, currentDir.Length - 10) + "\\LocalDataBase"; - return @"Data Source=Data Source=ata Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RachelA\source\repos\SQLLiteChartBinding\LocalDataBase\SeriesItemsSource.mdf;Integrated Security=True;Connect Timeout=30"; - } - } - } -} diff --git a/SQLwithChartUWP/SQLLiteChartBinding.sln b/SQLwithChartUWP/SQLLiteChartBinding.sln deleted file mode 100644 index 629e570..0000000 --- a/SQLwithChartUWP/SQLLiteChartBinding.sln +++ /dev/null @@ -1,51 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30204.135 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLLiteChartBinding", "SQLLiteChartBinding.csproj", "{ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|ARM = Debug|ARM - Debug|ARM64 = Debug|ARM64 - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|ARM = Release|ARM - Release|ARM64 = Release|ARM64 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM.ActiveCfg = Debug|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM.Build.0 = Debug|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM.Deploy.0 = Debug|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM64.ActiveCfg = Debug|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM64.Build.0 = Debug|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|ARM64.Deploy.0 = Debug|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x64.ActiveCfg = Debug|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x64.Build.0 = Debug|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x64.Deploy.0 = Debug|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x86.ActiveCfg = Debug|x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x86.Build.0 = Debug|x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Debug|x86.Deploy.0 = Debug|x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM.ActiveCfg = Release|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM.Build.0 = Release|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM.Deploy.0 = Release|ARM - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM64.ActiveCfg = Release|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM64.Build.0 = Release|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|ARM64.Deploy.0 = Release|ARM64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x64.ActiveCfg = Release|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x64.Build.0 = Release|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x64.Deploy.0 = Release|x64 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x86.ActiveCfg = Release|x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x86.Build.0 = Release|x86 - {ED371EC7-587F-4DCC-A1F3-3EBD337A7F67}.Release|x86.Deploy.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {B50B061E-C4FA-42E1-8F8E-79304AB22FD8} - EndGlobalSection -EndGlobal