如何修复与cnn.execute相关的运行时错误? [英] How do I fix a runtime error relating to cnn.execute?

查看:156
本文介绍了如何修复与cnn.execute相关的运行时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我需要将单词表单中的数据插入到我的访问表中。我正在使用word和access 2016

I am new to this and I need to insert data from a word form into my access table. I am using word and access 2016

"INSERT INTO Shippers" _
& "(Company, BusinessPhone) " _
& "VALUES (" _
& strCompany & ", " _
& strBusinessPhone & ")"
Debug.Print strSQL
'Substitute path and connection string with DSN if available.
strPath = "C:\Users\Admin\Documents\northwind.accdb"
strConnection = "Provider=Microsoft.ace.OLEDB.16.0;" _
& "Data Source = " & strPath
Debug.Print strConnection
Set cnn = New ADODB.Connection
cnn.Open strConnection
cnn.Execute strSQL, lngSuccess 'this is the problem
cnn.Close





我的尝试:



我评论该行并没有错误,但它没有插入数据



What I have tried:

I comment the line and there is no error but it does not insert the data

推荐答案

不是那样的!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



修复这可能也会解决你注意到的问题......这是由公司名称假设引起的通过SQL成为变量名而不是字符串。

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fixing that will probably also cure the problem you have noticed ... which is caused by the company name being assumed by SQL to be a variable name rather than a string.


除了OriginalGriff的解决方案1 ​​...



你应该使用执行方法 [ ^ ] for ADODB.Command而不是执行方法 [ ^ ]。一个主要的区别是,在1. case你可以传递参数。



请阅读:参数集合(ADO)| Microsoft Docs [ ^ ]以了解如何将参数传递到ADODB.Command。



A CommandText 应如下所示:

In addition to solution 1 by OriginalGriff...

You should use Execute method[^] for ADODB.Command instead of Execute method[^] for ADODB.Connection. A main difference is that in 1. case you're able to pass parameters.

Please, read this: Parameters Collection (ADO) | Microsoft Docs[^] to find out how to pass parameters into ADODB.Command.

A CommandText should looks like:
Dim oCommand As ADODB.Command, oRst As ADODB.Recordset
Dim sCommand As String

sCommand = "INSERT INTO Shippers(Company, BusinessPhone) VALUES(@pCompany, @pBPhone);"
Set oCommand = New ADODB.Command
With o Command
    .CommandType = 1 'adCmdText
    .CommandText = sCommand
    .Parameters.Append ccmd.CreateParameter("@pCompany", adVarChar, adParamInput, 150, "CompanyNameHere")   
    .Parameters.Append ccmd.CreateParameter("@pBPhone", adVarChar, adParamInput, 150, "PhoneNumberHere") 
    Set oRst = .Execute
End With





注意:以上代码直接来自我的头脑。所以,它可能包含错误(但我相信它没有)。



Note: Above code comes directly from my head. So, it can contains bugs (but i believe it doesn't).


这篇关于如何修复与cnn.execute相关的运行时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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