组合框中的FROM子句中的语法错误到DGV

时间:2013-09-24 14:21:45

标签: vb.net winforms

您好我在尝试将组合框连接到SQL查询时收到此语法错误“FROM子句中的语法错误”。这是我的代码:

Public Class Form4
  Dim m_commandbuilder1 As New OleDb.OleDbCommandBuilder
  Dim conn As New OleDb.OleDbConnection
  Dim myadapter As New OleDb.OleDbDataAdapter
  Dim mydataset As New DataSet
  Dim m_dTable1 As New DataTable

  'Variable to hold path to database
  Dim DatabasePath As String = "C:\Physics1.accdb;" 'DO NOT FORGET THE SEMICOLON(;) AT THE END

  'SQL String to connect to database
  Dim ConnString As String = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source = C:\Physics1.accdb; Persist Security Info =False;"

  Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim SchemaTable As DataTable

    'Connect to the database
    Dim conn As New System.Data.OleDb.OleDbConnection(ConnString)

    Try
        z.Items.Clear()

        conn.Open()

        'Get table and view names
        SchemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, Nothing})

        Dim int As Integer
        For int = 0 To SchemaTable.Rows.Count - 1
            If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
                'Add items to list box
                z.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
            End If
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString(), "Data Load Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Try

    conn.Close()

    conn.ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source = C:\Physics1.accdb; Persist Security Info =False;"
    Try
        conn.Open()
        myadapter = New OleDb.OleDbDataAdapter("select * FROM " & z.SelectedText.ToString & " ", conn)
        m_commandbuilder1 = New OleDb.OleDbCommandBuilder(myadapter)
        myadapter.Fill(m_dTable1)
        b.DataSource = m_dTable1
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

有谁可以帮助我吗?谢谢你的关注。

0 个答案:

没有答案