如何在过滤搜索ASP.NET C#后检查是否找到任何记录 [英] How do I check if there are any records found after filter search ASP.NET C#
本文介绍了如何在过滤搜索ASP.NET C#后检查是否找到任何记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定如何检查select是否从数据库返回任何行。如果没有找到行,我想要一条错误消息
我尝试过:
I am unsure how to check if select returns any rows from database. I want to have an error message if there are no rows found
What I have tried:
try
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(strConnString))
{
string strQuery = "SELECT * FROM TDLoanRate where LoanType Like'" + txtValue.Text + "%'";
SqlCommand cmd = new SqlCommand(strQuery);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
推荐答案
int count = dt.Rows.Count; // get the count
您的代码容易受到 SQL注入 [ ^ ]攻击。
始终使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]
Your code is vulnerable to SQL injection [^] attacks.
Always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
<如果计数> br />
只需从特定表中获取计数1然后有记录找到,其他明智的是没有找到记录。
simply get count from particular table if count > 1 then there is record found, other wise there is no record found.
如果我想检查是否有客户那么我会做什么
i将计算
从客户中选择计数(1)
存储该值在任何变量中检查你想要的条件。
假设我将值存储在字符串s中;
if(convert.int32(s)> 0)
{
}
if i want to check is there any customer then what i will do
i will count
select count(1) from customers
store that value in any variable and check what ever condition you want.
suppose i store value in string s;
if(convert.int32(s)>0)
{
}
这篇关于如何在过滤搜索ASP.NET C#后检查是否找到任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文