在插入Access之前检查重复项 [英] Check for duplicates before inserting into Access

查看:87
本文介绍了在插入Access之前检查重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在进入

Access数据库表之前检查重复项。我基本上想提醒用户,如果

''ProjectName''和''MileStones''已经在表格中......

谢谢

以下代码:

Private Sub btnADD_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理btnADD.Click

strSQL =" INSERT INTO Project" &安培; _

"(ProjectName,Planned,Actual,MileStones,Status,

[Next],Comments)VALUES" &安培; _

"(''"& txtProject.Text&"'',"& _

"''"& txtPlanned .Text&"'',''"& txtActual.Text&"'',"& _

"''"& txtMile.Text& amp; txtMile.Text& ;"'',''"& txtStatus.Text&"'',"& _

"''"& txtNext.Text&" '',''"& txtComment.Text&"'')"

connection.Open()

Dim cmd As New OleDbCommand(strSQL,连接)

cmd.ExecuteNonQuery()

connection.Close()

End Sub

I''m trying to check for duplicates before I do an INSERT into the
Access database table. I basically want to alert the user if the
''ProjectName'' and the ''MileStones'' are already in the table..
thanks
Code Below:
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnADD.Click
strSQL = "INSERT INTO Project" & _
"(ProjectName, Planned, Actual, MileStones, Status,
[Next], Comments)VALUES " & _
"(''" & txtProject.Text & "''," & _
"''" & txtPlanned.Text & "'',''" & txtActual.Text & "''," & _
"''" & txtMile.Text & "'',''" & txtStatus.Text & "''," & _
"''" & txtNext.Text & "'',''" & txtComment.Text & "'')"
connection.Open()
Dim cmd As New OleDbCommand(strSQL, connection)
cmd.ExecuteNonQuery()
connection.Close()
End Sub

推荐答案

理查德,


一个选项是选择

项目名称和里程碑等于的行数您要插入的值。使用

executioncalar返回计数。如果计数大于零,那么你

知道表格中已存在这些值的行。


我还建议你使用参数来为你的sql

语句提供值,而不是将文本框值连接到sql语句中。

你使用的技术可能导致所谓的SQL注入攻击。


Kerry Moorman

" ri *********** @ northwesternmutual.com"写道:
Richard,

One option would be to select the count of the number of rows where
projectname and milestones equal the values you are about to insert. Use
executescalar to return the count. If the count is greater than zero then you
know a row with those values already exists in the table.

I would also recommend that you use parameters to supply values to your sql
statements instead of concatenating textbox values into the sql statement.
The techinque you are using can lead to so-called sql injection attacks.

Kerry Moorman
"ri***********@northwesternmutual.com" wrote:

在我进入

Access数据库表之前,我正在尝试检查重复项。我基本上想提醒用户,如果

''ProjectName''和''MileStones''已经在表格中......


谢谢

代码如下:

Private Sub btnADD_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理btnADD.Click

strSQL =" INSERT INTO Project" &安培; _

"(ProjectName,Planned,Actual,MileStones,Status,

[Next],Comments)VALUES" &安培; _

"(''"& txtProject.Text&"'',"& _

"''"& txtPlanned .Text&"'',''"& txtActual.Text&"'',"& _

"''"& txtMile.Text& amp; txtMile.Text& ;"'',''"& txtStatus.Text&"'',"& _

"''"& txtNext.Text&" '',''"& txtComment.Text&"'')"

connection.Open()

Dim cmd As New OleDbCommand(strSQL,连接)

cmd.ExecuteNonQuery()

connection.Close()

End Sub
I''m trying to check for duplicates before I do an INSERT into the
Access database table. I basically want to alert the user if the
''ProjectName'' and the ''MileStones'' are already in the table..
thanks
Code Below:
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnADD.Click
strSQL = "INSERT INTO Project" & _
"(ProjectName, Planned, Actual, MileStones, Status,
[Next], Comments)VALUES " & _
"(''" & txtProject.Text & "''," & _
"''" & txtPlanned.Text & "'',''" & txtActual.Text & "''," & _
"''" & txtMile.Text & "'',''" & txtStatus.Text & "''," & _
"''" & txtNext.Text & "'',''" & txtComment.Text & "'')"
connection.Open()
Dim cmd As New OleDbCommand(strSQL, connection)
cmd.ExecuteNonQuery()
connection.Close()
End Sub


Kerry,
Kerry,

>

我还建议您使用参数为您的
sql

语句而不是将文本框值连接到sql语句中。

您使用的技术可能会导致所谓的SQL注入攻击。
>
I would also recommend that you use parameters to supply values to your
sql
statements instead of concatenating textbox values into the sql statement.
The techinque you are using can lead to so-called sql injection attacks.



我总是好奇如何在Access(Jet)数据库中发生这种情况,

无法直接连接到互联网。


因为这可能是一个问题实习生Lan或数据库直接在自己的

驱动器上,然后确定更重要的lecks关闭。


Cor


Richard,


数据库中没有重复的部分是什么?


Cor
Richard,

What is the part that has not to be insterted as duplicate in the database?

Cor


这篇关于在插入Access之前检查重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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