运行插入SQL查询 [英] Run Insert SQL queries

查看:64
本文介绍了运行插入SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Microsoft Access中运行INSERT SQL查询?

How would I run an INSERT SQL query in Microsoft Access?

例如:

INSERT INTO tbl_UserPurchase (ID, Name) Values (321, Joe)

推荐答案

您可以在Access的查询设计器中构建查询,然后在仍处于设计视图"中时,在功能区上单击运行". (寻找红色的解释点.)

You can build your query in Access' query designer, then while still in Design View, click "Run" on the ribbon. (Look for the red explanation point.)

如果要通过代码执行此操作,则可以使用DAO数据库对象或ADO CurrentProject.Connection对象的.Execute方法.

If you want to do it from code, you can use the .Execute method of either the DAO database object or the ADO CurrentProject.Connection object.

Dim strInsert As String
strInsert = "INSERT INTO tbl_UserPurchase (ID, Name)" & vbCrLf & _
    "VALUES(321, 'Joe');"
CurrentDb.Execute strInsert, dbFailOnError

但是,参数查询将更加灵活(对其他用户名有用,而不仅仅是Joe),并且可以防止SQL注入.

However, a parameter query would be more flexible (useful for other user names, not just Joe) and prevent SQL injection.

Dim db As DAO.database
Dim qdf As DAO.QueryDef
Dim strInsert As String

strInsert = "INSERT INTO tbl_UserPurchase (ID, Name)" & vbCrLf & _
    "VALUES (321, [which_user]);"
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strInsert)
' you could read the parameter value from a text box on a form,
' but this example will just hard code Joe
qdf.Parameters("[which_user]").value = "Joe"
qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing

还有其他方法可以给这只猫蒙皮.如果所有建议都不令人满意,请向我们提供有关如何以及在何处运行INSERT的详细信息.

And there are still other ways to skin this cat. If none of the suggestions is satisfactory, give us more details about how and where you want to run your INSERT.

这篇关于运行插入SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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