尝试读取SQLite数据库Windows Universal Apps VB.NET时出错

时间:2016-03-28 06:39:18

标签: vb.net sqlite win-universal-app

我使用以下代码尝试访问数据库并填充组合框:

            With cmbPaciente.Items
               Dim sConexao As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "\Banco\Deal.db")
               Dim aConexao As New SQLiteConnection(sConexao)
               Dim Comando = aConexao.CreateCommand("select Name from PData")
               Dim Resultado As String = aConexao.Query(Comando)
               For Each item In Resultado
                   .Add(item)
               Next
            End With

但是我收到了这个错误:

BC32050  VB.NET Type parameter '' for '' cannot be inferred.

已经尝试过:

            With cmbPaciente.Items
               Dim sConexao As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "\Banco\Deal.db")
               Dim aConexao As New SQLiteConnection(sConexao)
               Dim Comando = aConexao.CreateCommand("select Name from PData")
               Dim Resultado As String = aConexao.Execute(Comando)
               For Each item In Resultado
                   .Add(item)
               Next
            End With

也没有成功。当然我做错了什么,但我的知识还不足以理解什么。任何线索都将非常感激。

1 个答案:

答案 0 :(得分:1)

您不需要CreateCommand从数据库中读取数据,我发现您的代码Dim aConexao As New SQLiteConnection(sConexao)有点问题,在UWP应用中,它应该是这样的:< / p>

Dim aConexao As New SQLiteConnection(New SQLitePlatformWinRT(), sConexao)

这是我的样本:

XAML代码:

<ComboBox x:Name="cmbPaciente" VerticalAlignment="Center" HorizontalAlignment="Center">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Symbol}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<Button Content="create database" Click="Button_Click" VerticalAlignment="Top" Margin="0,30,0,0" />
<Button Content="read data from database" Click="Button_Click_1" VerticalAlignment="Top" Margin="0,100,0,0" />

代码背后:

Private combolist As New ObservableCollection(Of Stockvb)()
Private Sub showComboBoxItem()
    With cmbPaciente.Items
        Dim sConexao As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Deal.db")
        Dim aConexao As New SQLiteConnection(New SQLitePlatformWinRT(), sConexao)
        For Each item As Stockvb In aConexao.Table(Of Stockvb)()
            Dim newlist As New Stockvb()
            newlist.Id = item.Id
            newlist.Symbol = item.Symbol
            combolist.Add(newlist)
        Next
        cmbPaciente.ItemsSource = combolist
    End With
End Sub

Private Async Function CheckFileExists(fileName As String) As Task(Of Boolean)
    Try
        Dim store = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName)
        Return True
    Catch
    End Try
    Return False
End Function

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    If Not CheckFileExists("Deal.db").Result Then
        Dim spath As String = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Deal.db")
        Dim db As New SQLiteConnection(New SQLitePlatformWinRT(), spath)
        db.CreateTable(Of Stockvb)()

        For i As Integer = 0 To 5
            Dim stocklist As New Stockvb()
            stocklist.Id = i
            stocklist.Symbol = "Item" + i.ToString()
            db.Insert(stocklist)
        Next
    End If
End Sub

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    showComboBoxItem()
End Sub

这是Stockvb类:

Public Class Stockvb
    <PrimaryKey, AutoIncrement>
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer

    <MaxLength(8)>
    Public Property Symbol() As String
        Get
            Return m_Symbol
        End Get
        Set
            m_Symbol = Value
        End Set
    End Property
    Private m_Symbol As String
End Class

我将您的数据库名称更改为&#34; Deal.db&#34;在我的代码中。