Reputation:
I am working with a database embedded within the files of the application. To the database I put the Build Action: Content, its route is "Database/Original.db", but I can't get it connect. The error is in the line of:
SQLiteConnection dbConnection = new SQLiteConnection("Database/Original.db");
This is my code:
PageMetrados.xaml.cs
using System;
using System.Collections.Generic;
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;
using Windows.Storage;
using SQLitePCL;
namespace Presupuestos
{
public sealed partial class PageMetrados : Page
{
public PageMetrados()
{
this.InitializeComponent();
}
private void buttonShowList_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection dbConnection = new SQLiteConnection("Database/Original.db");
var datos = new List<Metrados_Head>();
string sSQL = @"SELECT
[A],
[B],
[C],
[D],
[E],
[F]
FROM Metrados_Head";
ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
while (dbState.Step() == SQLiteResult.ROW)
{
string sA = dbState["A"] as string;
string sB = dbState["B"] as string;
string sC = dbState["C"] as string;
string sD = dbState["D"] as string;
string sE = dbState["E"] as string;
string sF = dbState["F"] as string;
Metrados_Head Datos_Metrados_Head = new Metrados_Head() { A = sA, B = sB, C = sC, D = sD, E = sE, F = sF };
datos.Add(Datos_Metrados_Head);
}
ListMetrados_Head.ItemsSource = datos; }
}
}
Metrados_Head.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Presupuestos
{
public class Metrados_Head
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
}
}
It is showed in a Listview:
<ListView x:Name="ListMetrados_Head" HorizontalAlignment="Left" Height="600" Margin="10,109,0,0" VerticalAlignment="Top" Width="1260" BorderBrush="Black" BorderThickness="1" Background="White">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="{Binding ActualWidth, ElementName=ListMetrados_Head}" Padding="0" Margin="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBlock x:Name="TextBlock_A" Grid.Column="0" Text="{Binding Path=A}" TextWrapping="Wrap" />
<TextBox x:Name="TextBox_B" Grid.Column="1" Text="{Binding Path=B}" TextWrapping="Wrap" />
<TextBox x:Name="TextBox_C" Grid.Column="2" Text="{Binding Path=C}" TextWrapping="Wrap" />
<TextBox x:Name="TextBox_D" Grid.Column="3" Text="{Binding Path=D}" TextWrapping="Wrap" />
<TextBox x:Name="TextBox_E" Grid.Column="4" Text="{Binding Path=E}" TextWrapping="Wrap" />
<TextBox x:Name="TextBox_F" Grid.Column="5" Text="{Binding Path=F}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Upvotes: 0
Views: 558
Reputation: 1090
In order to access resources that come packaged with your app you should use the following form ms-appx:///Database/Original.db
(if the Database folder is directly under the root folder)
The ms-appx:///
scheme gives you access to the installation folder of your app.
Keep in mind that files in the installation folder are read only so you cannot make changes to them.
Alternatively this might also work /Database/Original.db
Also you should check if you are using a valid path. For example I see in your question that the path is Databases/Original.db
while in your code it is Database/Original.db
. Make sure you are using the correct one.
Upvotes: 1