我想如何检查数据库是否找到了输入的数据 [英] i want how to check the database whether the entered data is found or not

查看:108
本文介绍了我想如何检查数据库是否找到了输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有某些数据..im在表中有国家/地区名称和国家/地区名称作为列名...我想在Web应用程序中使用示例C#编码..这个概念是我必须检查该国家/地区的名称和ID必须在前端进行检查

im having certain data in the table..im having country id and country name as column name in table...i want sample C# coding in web application..the concept is that i have to check that country name and id has to checked in front end

推荐答案

最好在SQL本身中进行处理.将列国家/地区名称作为唯一键.将您的Insert查询放入try catch中这样的SQL,

开始尝试
开始
INSERT查询
结束
结束尝试

开始捕捉
如果ERROR_NUMBER()= 2601-重复记录
开始
SELECT -1
END
结束观看


在前端检查返回值是否为-1.如果为-1,则给出相应的错误消息
It is better to handle it in SQL itself.Make columns Country name as Unique key.Put your Insert query inside a try catch in SQL like this,

BEGIN TRY
BEGIN
INSERT query
END
END TRY

BEGIN CATCH
IF ERROR_NUMBER()=2601 -- Duplicate Record
BEGIN
SELECT -1
END
END CATCH


In frontend , check whether the return value is -1 .If it is -1 give the corresponding error message


您需要从前端编写查询……查询包含count函数,并在前端使用execute标量函数执行该查询.

例如:
str =从表名中选择count(*)"
cmd.Executescalar()
you need to write a query from front end......query contains count function and execute that query using execute scalar function in front end.

For example :
str = "select count(*) from table_name"
cmd.Executescalar()


这篇关于我想如何检查数据库是否找到了输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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