在vb.net中经常调用sqlite

时间:2018-05-16 00:07:44

标签: vb.net

vb.net新手! 使用MS Visual Studio 2017(Visual Basic)

开发一个从SQLite数据库获取数据(电子邮件地址)并加载到字符串中的应用程序,然后打开该字符串,并预先填写电子邮件地址的Outlook(新邮件窗口)。

问题在于,当我第一次单击“创建新消息”按钮时,但是当我第二次执行该操作时,应用程序挂起并且没有错误或Visual Studio的任何反馈。

    Function GetAddresses(table As String)
    Dim Path As String = Directory.GetCurrentDirectory() + "\\"
    Dim dataBase As String = "test.db"
    Dim cs As String = Path & dataBase
    Dim myConnection As New SQLiteConnection()
    Dim cadeomSQL As String = "Data Source=" & cs & ";"
    Dim resultString As String = ""

    myConnection.ConnectionString = cadeomSQL
    myConnection.Open()

    Dim sSQL As String = "SELECT * FROM " + table + ";"
    Using cmd As New SQLiteCommand(sSQL, myConnection)

        Dim rdr As SQLiteDataReader = cmd.ExecuteReader()

        Using rdr
            While (rdr.Read())
                'MsgBox(rdr.GetInt32(0).ToString & " - " & rdr.GetString(1))
                resultString &= rdr.GetInt32(0).ToString & " - " & rdr.GetString(1)
            End While
        End Using
    End Using
    myConnection.Close()
    myConnection = Nothing
    Return resultString
End Function

这是调用方法:

    Sub CreateEmail()
    Dim mapi As New My.MAPI
    Dim EmailAddresses As String = ""


    'get email addresses
    'based on checkboxes ticked
    For Each c As Control In GroupBox2.Controls
        If c.GetType Is GetType(CheckBox) Then
            Dim cb As CheckBox = DirectCast(c, CheckBox)
            If cb.Checked Then
                MsgBox(cb.Text & " is checked")
                EmailAddresses &= GetAddresses(cb.Text)
            End If
        End If
    Next

    MsgBox(EmailAddresses)


    'Attach files
    For Each rowView As DataRowView In ListBox1.Items
        Dim value As String = rowView("Value")
        'MsgBox(value)
        mapi.AddAttachment(value)
    Next


    mapi.AddRecipientTo(EmailAddresses)
    mapi.SendMailPopup("Subject", "body text")

    EmailAddresses = Nothing
    mapi = Nothing
End Sub

所以我不确定第二次按下创建新消息按钮时导致应用程序挂起的原因。

Screen shot of email form

1 个答案:

答案 0 :(得分:0)

当您使用Directory.GetCurrentDirectory()获取数据库文件夹路径时,我怀疑该文件与EXE位于同一文件夹中。当前目录通常是程序文件夹,默认情况下可能不是,它可以在会话过程中更改,因此除非您特别想要当前目录,否则最好避免。

在WinForms项目中,您可以使用Application.StartupPath获取程序文件夹的路径,因此我建议您替换它:

Dim Path As String = Directory.GetCurrentDirectory() + "\\"
Dim dataBase As String = "test.db"
Dim cs As String = Path & database

用这个:

Dim cs = IO.Path.Combine(Application.StartupPath, "test.db")