插入数据时检查数据库中是否存在查询 [英] querry for when inserting data check it is exist or not in databse
本文介绍了插入数据时检查数据库中是否存在查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我通过数据库中的文本框插入数据时.我想检查表中是否已经存在数据,只是显示一条消息数据已存在".
在表中,只有两列id和documentname.
我正在使用sql server和vs2005.
请给我一个酒杯.
在此先感谢
Hi, when i am inserting a data through text box in database. i want to check if data is already exist in the table just shows a message "Data is already exist".
In table there is only two columns id and documentname.
I am using sql server and vs 2005.
Please give me a querry for that.
Thanks in Advance
推荐答案
SELECT * FROM TABLENAME WHERE FIELDNAME = '" + YOURSEARCHSTRING + "';
从此查询获取数据到datatable和
Get the data from this query to datatable and
if(MYDATATABLE.Rows.Count > 0)
{
Messagebox.Show("Record Already Exists");
}else
{
//Fire your insert query
}
希望您能从中得到启发.
Hope you will get an idea through this.
Hai fried,
1.最好的方法是在数据库中创建唯一键,并跟踪错误代码以显示错误.
2.另一种方法
Hai fried,
1.The best method is create a unique key in database, and track the error code for displaying error.
2.Another method
IF EXISTS(SELECT * FROM tablename WHERE condition )
BEGIN
RAISERROR ('already entered',11,1)
END
ELSE
BEGIN
-- Insert query
END
这篇关于插入数据时检查数据库中是否存在查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文