我不断收到错误消息“您的 sql 语法有错误”。当我使用此sql语句时:
SELECT * FROM gsm_oceanwide_integration
编辑:
要将其放在上下文中,我在(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
在我的frmMain表单上,我有一个包含项目列表的列表框(其中一个是 gsm_oceanwide_integration),我想这样做,以便当我单击该选择时,它将显示另一个带有 datagridview 的表单。该 datagridview 需要填充来自我称为 gsm_oceanwide_integration 的表中的数据。 (如列表框选择)
所以我当前的查询语句(包括 vb.net 标签)是:
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)
它今天早些时候工作,但我一定已经更改了其中的某些内容并忘记了......
从那时起,我能做的最好的事情就是消除所有错误,但 datagridview 仍然不会在我的数据库上显示任何内容。是的,我已经检查了所有的拼写。
更新2:
我将 sqlpage 命令语句从: 更改
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM '" & frmMain.ListBox1.SelectedItem & "';", con)
为:
Dim sqlpage As MySqlCommand = New MySqlCommand("SELECT * FROM [" & Me.ListBox1.SelectedItem.Value & "]", con)
我在另一个表单上收到了一个新错误,但它链接到了 listbox1;其代码如下:
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
该屏幕截图显示了错误(我认为屏幕截图可能是显示错误详细信息的最佳方法):
错误是:(以防您无法阅读屏幕截图)
创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:未设置对象变量或With块变量。
另外,感谢您的快速回复,抱歉我无法尽快更新我的帖子...
谢谢!
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 MySqlCommand = 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):
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!
发布评论
评论(4)
从列表框中输入的表名称周围的双引号之前的单引号怎么样?那不应该在那里。你有:
应该是:
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:
It should be:
避免在表名称或字段名称中添加任何特殊字符。坚持约定,例如...
myFieldName
- 称为驼峰式大小写或...my_field_name
- 只需使用下划线至于sql语句,我无法立即看到任何内容错了。表名是否正确?
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 underscoresAs for the sql statement I can't see anything immediately wrong with it. Is the table name correct?
关于您对表名中空格的查询:
简短的答案 - 是但否
但是,我会看一下 关于架构对象名称的 MySQL 文档
另外,正如评论中提到的,允许使用带引号的标识符作为表标识符,但我不鼓励使用它们,主要是出于可读性的原因。
至于您的查询,将需要更多信息,例如您的表列表。
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.
我得到了与提问者相同的错误,但原因不同。
我将其中一张表命名为
order
。当对此表执行查询时,sql 会认为我的表名是关键字ORDER
。ie
select * from order;
returns为了解决这个问题,我能够将表名用双引号引起来并成功运行我的搜索。
从“订单”中选择*;
有效!I got the same error as the asker, but for a different reason.
I named one of my tables
order
. When executing a query against this table results in sql thinking that my table name was the keywordORDER
.i.e.
select * from order;
returnsTo get around this I was able to surround my table name in double quotation marks and successfully run my search.
select * from "order";
works!