在VB.NET中更新访问时获取SYNTAX ERROR [英] Getting a SYNTAX ERROR in updating access in VB.NET

查看:83
本文介绍了在VB.NET中更新访问时获取SYNTAX ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我一直在尝试插入Access数据库,但我收到错误:



语法错误在INSERT INTO语句中。



我已经尝试了一些我能想到的东西来克服这个错误,但无济于事。我已经运行了我在Access本身尝试过的Insert语句,一切正常。我还有一个INSERT / SELECT语句也失败了



From子句中的SYNTAX ERROR。



但在Access中运行良好。该表有一个AutoNumber字段。我在Access 2007数据库上运行此操作,然后在Access 2013中的新数据库中重新创建表,两者都没有工作。



我尝试过:



strSql =INSERT INTO TRANSACTION([Location],[CoilNo],[HeatNo],[Matsize],[Net],[Gross],[ TransDate],[BOL],[PackSlip],[TransType],[Flag],[NewLocation],[CreateDate],[ShipName],[Cost])VALUES(

@Location,@ CoilNo ,@ HeatNo,@ Matsize,@ Net,@ Gross,

@ TransDate,@ BOL,@ PackSlip,@ TransType,@ Flag,@ NewLocation,@ CreateDate,@ SshipName,@ Cost)



尝试

使用conn作为新的OleDbConnection(ConnStr)

conn.Open()

使用cmd作为新的OleDbCommand(strSql,conn)

使用cmd

.Parameters.Clear()

.Parameters.AddWithValue( @Location,txtLocation.Text)

.Pa rameters.AddWithValue(@ CoilNo,txtCoilNumber.Text)

.Parameters.AddWithValue(@ HeatNo,txtHeatNumber.Text)

....... 。



'cmd.Parameters.Add(新OleDbParameter(@ Location,Trim(txtLocation.Text)))

' cmd.Parameters.Add(New OleDbParameter(@ CoilNo,Trim(txtCoilNumber.Text)))

'cmd.Parameters.Add(New OleDbParameter(@ HeatNo,Trim(txtHeatNumber.Text) )))

........



'.Parameters.Add(New OleDb.OleDbParameter(@ Location ,OleDb.OleDbType.VarChar))

'.Parameters(@ Location)。Value = txtLocation.Text

'.Parameters.Add(New OleDb.OleDbParameter (@ CoilNo,OleDb.OleDbType.VarChar))

'.Parameters(@ CoilNo)。Value = txtCoilNumber.Text

.Parameters.Add(新OleDb.OleDbParameter(@ HeatNo,OleDb.OleDbType.VarChar))

'.Parameters(@ HeatNo)。Value = txtHeatNumber.Text

......

结束



昏暗记录受影响为Integer = cmd.ExecuteNonQuery()



结束使用

结束使用

Catch ex As Exception

MessageBox.Show(ex.Message) ,SaveConsignedData,MessageBoxButtons.OK,MessageBoxIcon.Error)

结束尝试

Hello,
I have been tryng to do an Insert into an Access database but I am getting the error:

SYNTAX ERROR in INSERT INTO statement.

I have tried evreything that I can think of to overcome this error, to no avail. I have run the Insert statements that I have tried in Access itself and everything works just fine. I also have an INSERT/SELECT statement that also fails with

SYNTAX ERROR in From clause.

but runs fine in Access. The table has an AutoNumber field. I ran this against an Access 2007 database, then recreated the table in a new database in Access 2013, neither worked.

What I have tried:

strSql = "INSERT INTO TRANSACTION ([Location], [CoilNo], [HeatNo], [Matsize], [Net], [Gross], [TransDate], [BOL], [PackSlip], [TransType], [Flag], [NewLocation], [CreateDate], [ShipName], [Cost]) VALUES (
@Location, @CoilNo, @HeatNo, @Matsize, @Net, @Gross,
@TransDate, @BOL, @PackSlip, @TransType, @Flag, @NewLocation, @CreateDate, @ShipName, @Cost)"

Try
Using conn As New OleDbConnection(ConnStr)
conn.Open()
Using cmd As New OleDbCommand(strSql, conn)
With cmd
.Parameters.Clear()
.Parameters.AddWithValue("@Location", txtLocation.Text)
.Parameters.AddWithValue("@CoilNo", txtCoilNumber.Text)
.Parameters.AddWithValue("@HeatNo", txtHeatNumber.Text)
........
OR
'cmd.Parameters.Add(New OleDbParameter("@Location", Trim(txtLocation.Text)))
'cmd.Parameters.Add(New OleDbParameter("@CoilNo", Trim(txtCoilNumber.Text)))
'cmd.Parameters.Add(New OleDbParameter("@HeatNo", Trim(txtHeatNumber.Text)))
........
OR
'.Parameters.Add(New OleDb.OleDbParameter("@Location", OleDb.OleDbType.VarChar))
'.Parameters("@Location").Value = txtLocation.Text
'.Parameters.Add(New OleDb.OleDbParameter("@CoilNo", OleDb.OleDbType.VarChar))
'.Parameters("@CoilNo").Value = txtCoilNumber.Text
'.Parameters.Add(New OleDb.OleDbParameter("@HeatNo", OleDb.OleDbType.VarChar))
'.Parameters("@HeatNo").Value = txtHeatNumber.Text
......
End With

Dim recordsAffected As Integer = cmd.ExecuteNonQuery()

End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "SaveConsignedData", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

推荐答案

信息或代码不足。



我可以告诉你的是Access没有使用命名参数。您仍然可以为它们指定名称,但是您添加的参数必须按它们在SQL语句中出现的顺序显示。



接下来,您拥有的每个参数都是如果您直接从TextBox分配,则将其视为字符串。这是不好的形式,因为您似乎没有对数据进行任何验证。



现在,如果数据库中的某些列是数字的,例如CoilNo,在将其添加为参数之前,必须将TextBox中的文本转换为整数。您不能将字符串插入设计为数字的列中。
Not enough information or code.

What I can tell you is that Access doesn't used named parameters. You can still assign names to them, but the parameters you add have to appear in the order that they appear in the SQL statement.

Next, every parameter you have is being treated as a string if you're assigning directly from a TextBox. This is bad form as you don't appear to be doing any validation of the data.

Now, if certain columns in your database are numeric, such as CoilNo, you have to convert the text in the TextBox to an integer before you add it as a parameter. You cannot insert a string into a column designed as a number.


这篇关于在VB.NET中更新访问时获取SYNTAX ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆