开发者

Standard SQL SELECT * FROM TABLE return syntax error

I keep getting an error saying "there is an error in your sql syntax." when i use this sql statement:

SELECT * FROM gsm_oceanwide_integration

EDIT:

To put this in context heres the code im using this query statement in (VB.net):

Dim con As MySqlConnection = New MySqlConnection("Data Source=" & frmLogin.txtserver.Text & ";Database=stratocast;User ID=" & frmLogin.txtusername.Text & ";Password=" & frmLogin.txtpassword.Text & ";")
Dim sqladmin As MySqlCommand = New MySqlCommand("SELECT * FROM employee", con)
Dim sqlprojects As MySqlCommand = New MySqlCommand("SELECT * FROM projects", con)
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)

Dim ds5 As DataSet = New DataSet()
Dim DataAdapter5 As MySqlDataAdapter = New MySqlDataAdapter()
Dim Comb As MySqlCommandBuilder

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


    ' retrieving the Project Page.
    Try
        con.Open()
        DataAdapter5.SelectCommand = sqlpage
        DataAdapter5.Fill(ds5, "stratocast")
        DataGridView3.DataSource = ds5
        DataGridView3.DataMember = "stratocast"
        con.Close()

    Catch myerror As MySqlException
        MessageBox.Show("Error Retrieving Project Page: " & myerror.Message)
    End Try
End Sub

on my frmMain form i have a listbox with a list of projects (one of them being gsm_oceanwide_integration) and i want to make it so that when i click on that selection it will display another form with a datagridview. this datagridview needs to be filled with data from the table i called gsm_oceanwide_integration. (like the listbox selection)

so my current query statement (including vb.net tags) is:

Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)

It was working earlier today, but i must have changed something in it and forgot... since then the best i can do is get rid of all the errors but the datagridview still won't display anything on my database. Yes, i've checked all the spelling.


UPDATE 2: I changed my sqlpage command statement from:

Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)

To:

 Dim sqlpage As M开发者_运维技巧ySqlCommand = New MySqlCommand("SELECT * FROM [" & Me.ListBox1.SelectedItem.Value & "]", con)

and i got a new error on another form all together but it's linked to the listbox1; the code for which is below:

Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
    Form1.indprojectname.Text = ListBox1.SelectedItem
    Form1.Show()
End Sub

The error is shown in this screenshot (i thought a screen shot might be the best method for displaying the error details):

Standard SQL SELECT * FROM TABLE return syntax error

the error is: (in case you couldn't read the screenshot)

An error occurred creating the form. See Exception.InnerException for details. The error is: Object variable or With block variable not set.

Also, thanks for the quick replies, sorry i couldn't update my post sooner...

Thanks!


How about the single quote before your double quote around the table name coming in from your listbox? That should not be there. You have:

Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)

It should be:

Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM " & frmMain.ListBox1.SelectedItem & ";", con)


Steer clear of any adding special characters in your table names or field names. Stick to a convention such as...

myFieldName - known as camel casing or...

my_field_name - just use underscores

As for the sql statement I can't see anything immediately wrong with it. Is the table name correct?


Regarding your query about spaces in table names:

Short Answer - yes but no

However, I would take a look at the MySQL documentation on Schema Object Names

Also as mentioned in the comments, spaces are allowed as a table indentifer using quoted indentifiers but I would not encourage the use of them, mainly for readability reasons.

As for your query, would require more information, such as your list of tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜