0

嗨,我已经学习 VB.NET 大约一个月了。我被困在某件事上,我收到一条错误消息说 Syntax error in union query.

有问题的代码是这样的。

Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
    Dim Builder As New OleDb.OleDbConnectionStringBuilder With
        {
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = IO.Path.Combine(Application.StartupPath, "C:\Users\Nige\Documents\Visual Studio 2012\Projects\MS_Access_SimplePassword\bin\Debug\Database1.mdb"),
            .PersistSecurityInfo = True
        }
    LoginForm.txtUserName.Text = LoginForm.txtUserName.Text

    If LoginForm.txtUserName.Text <> "" Then

    End If
    'do what you want to do
    lblName.Text = LoginForm.txtUserName.Text
    If lblName.Text = LoginForm.txtUserName.Text Then
        Builder.Add("Jet OLEDB:Database Password", "password")
        Using con As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
            Using cmd As New OleDb.OleDbCommand With
                {
                    .Connection = con,
                    .CommandText =
                    <SQL>
                   ("SELECT * FROM tblContacts WHERE number" = '55')
            Identifier,
            UserName,
            UserPassword,
            UserTimer
            FROM tblContacts
                </SQL>.Value
                }
                con.Open()
                Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader
                If Reader.HasRows Then
                    Reader.Read()
                    TextBox1.Text = Reader.GetInt32(0).ToString
                    TextBox2.Text = Reader.GetString(1)
                    TextBox3.Text = Reader.GetString(2)
                    TextBox4.Text = Reader.GetString(3)
                End If
            End Using
        End Using
    End If
End Sub

上面的代码应该在我单击 button5 时搜索名为“tblContacts”的“ms 访问数据库”以查找引用数字“55”的行

如果我的代码在某些地方看起来很奇怪,那是因为我还在学习路上:)

干杯

4

1 回答 1

0

我可以在上面的代码中看到几个错误:

Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
    .Provider = "Microsoft.ACE.OLEDB.12.0",
    .DataSource = "C:\Users\Nige\Documents\Visual Studio 2012\Projects\MS_Access_SimplePassword\bin\Debug\Database1.mdb"),
    .PersistSecurityInfo = True
}

使用两个绝对文件名在Path.Combine逻辑上是错误的,但是它仍然有效,因为Path.Combine能够识别传递的两个字符串是两个完整的文件名,因此不会尝试创建不可能的路径,而是直接返回第二个字符串。

来自 Path.Combine 上的 MSDN

组合路径。如果指定路径之一是零长度字符串,则此方法返回另一个路径。如果 path2 包含绝对路径,则此方法返回 path2。

然后,查看您的查询,它显然是一个无效的选择文本,因此您得到语法错误。
假设这可能是查询数据表的正确方法

  • 您搜索包含名为“数字”的列的行
  • 此列是文本类型文本
  • 一行或多行在此列中包含等于“55”的文本

    Dim sqlText = "SELECT Identifier, UserName, UserPassword, UserTimer " + 
                 "FROM tblContacts WHERE number = '55'";
    Using con = New OleDb.OleDbConnection(Builder.ConnectionString)
    Using cmd = New OleDb.OleDbCommand(sqlText, con)
       con.Open()
       Using Reader = cmd.ExecuteReader
           While Reader.Read()
                TextBox1.Text = Reader.GetInt32(0).ToString
                TextBox2.Text = Reader.GetString(1)
                TextBox3.Text = Reader.GetString(2)
                TextBox4.Text = Reader.GetString(3)
           End While
       End Using
    End Using
    End Using
    
于 2013-06-08T12:12:31.667 回答