避免从VB在SQL中插入相同的数据 [英] Avoid inserting same data in SQL from VB
问题描述
大家好,
我在这里有一个小问题.我正在使用Visual Studio 2008,并且希望用户使用该应用程序不能将相同的数据插入表中的特定列.
列的值不能相同.
[代表OP进行更新]
感谢您的回答,
我使用了选项1,但即使sql列中的数据仍然与以前相同,数据集中的数据仍会更新.
它让我发疯.
这是我的桌子:
Hello all,
i have a small problem here. i''m using visual studio 2008, and i want the user that using the application can''t insert the same data into the table, for specific column.
the value of the column can''t be same.
[Update on OP''s behalf]
Thanks for the answer,
I used the option 1, but the data in the data set is still update even tough the data in sql column is still the same as before.
it makes me nuts.
Heres my table:
NoFile NOSk subject SKKind SKDate SKRgion SKSource DetailSK
1 28279 Transportasi Mengikat 19/01/2010 Sibolga Kepegawaian .rtf
2 28279 Uang Lembur Mengikat 22/04/2011 Sibolga Kepegawaian .rtf
[结束更新]
感谢您的帮助.
关于Tandibua
[End Update]
thanks for your help.
regard Tandibua
推荐答案
您有两个选择:
You have two options:
- 使该列成为该表的主键.
- 在该列上放置唯一的索引约束.
从以上选项中选择哪种方式都将阻止用户在该表中输入行,而指定的列将包含相同的条目.
最好的问候,
-MRB
Whichever way you choose from the above options will keep the user from entering rows into that table where the specified column would contain identical entries.
Best Regards,
-MRB
定义表时,如果数据库允许,则使字段定义"Unique" =是.
另外,不要依赖异常,而是要首先检查行是否存在:
When you define the table, make the field definition "Unique"=Yes if your database allows it.
In addition, rather than relying on exceptions, check if the row exists first:
SELECT COUNT(*) FROM myTable WHERE myColumn=myValue
如果使用SqlCommand.ExecuteScalar函数,则该函数将返回零(如果不存在).
If you use the SqlCommand.ExecuteScalar function it will return zero if it does not exist.
这篇关于避免从VB在SQL中插入相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!