sql语句查询怎么判断查询结果为空?

时间:2021-06-10 01:06:19
sql语句查询怎么判断查询结果为空?
我的代码如下

try
            {
                conn.Open();
                string sou = "select * from art where content like '%" + TextBox1.Text + "%'";
                SqlDataAdapter da = new SqlDataAdapter(sou, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds == null)
                {
                    Response.Write("没有记录!");
                }
                else
                {
                    this.sou.DataSource = ds;
                    this.sou.DataBind();
                }
            }
            catch
            {
            }

if (ds == null)这里怎么写?
这里执行总是不行,为空的时候也不提示response

39 个解决方案

#1


ds.tables[0].rows.count==0

#2


ds的tables[0]有多少行

#3


一楼正解
ds.tables[0].rows.count

#4


你这里用DataSet 的 很好解决
ds有 tables[]属性  然后再点属性rows.count  
  楼上以回答了 。。

#5


我在想,都用了DataSet ds = new DataSet();
如何还用ds == null去判断?如果Fill有表的话,是用ds.tables[0].rows.count去判断

#6


一楼正解

#7


ds是不是为null,还有就是判断ds.table.count是不是>0

#8


 if (ds.Tables[0].Count>0 )
{}或SqlDateReader

#9


if (ds.Tables[0].Rows.Count>0){}

#10


引用 9 楼 wuyq11 的回复:
if (ds.Tables[0].Rows.Count>0){}

如果我查询的是一个字段的怎么判断呢?
比如
string sou="select name from art where name like '%张三%'";
sqlcommand cmd=new sqlcommad(sou,conn);
string name=cmd.ExecuteScalar().tostring;
如果查询出来没有张三这个名字,就是说值为空的话,上面这个代码会出什么样的问题?怎么解决?
怎么判断他的直为空?

#11


看看

#12


string sou="select name from art where name like '%张三%'"; 
sqlcommand cmd=new sqlcommad(sou,conn);
int i =cmd.ExecuteScalar(); 
if(i=0)
{
messagebox.show("结果为空");
}
else
{
//你的代码
}

我很菜,可我很热心!

#13


这是多么容易的注入漏洞啊。。。

像我也能写出针对这个代码的注入语句了。。

你至少也应该替换 '''

#14


引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?

#15


引用 14 楼 yangzheng1128 的回复:
引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?

.......
..........

#16


取表的行数不就知道有没记录了

#17


引用 1 楼 code163 的回复:
ds.tables[0].rows.count==0

正解

#18


学习中啊

#19


ds.tables[0].rows.count==0

#20


引用 14 楼 yangzheng1128 的回复:
引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?


在你的Textbox1里面输入字符串  a' or 1=1--

然后你执行下试试

#21


gridview 等其它数据源控件 提供了数据为空的模版,,在里面写就行了.

#22


引用 1 楼 code163 的回复:
ds.tables[0].rows.count==0


up

#23


如果怕注入,就用raplace来替换引号咯。或者用存储过程吧。

#24


if (ds.Tables["art"].Rows.Count == 0)//art是你的表名
{
     Page.ClientScript.RegisterStartupScript(GetType(), "sf", "<script language='javascript'>alert('没有记录').focus()</script>");
}
else
{
    this.sou.DataSource = ds;
    this.sou.DataBind();
}

#25


//此句
Page.ClientScript.RegisterStartupScript(GetType(), "sf", " <script language='javascript'>alert('没有记录')</script>"); 

#26


if(ds.tables[0].rows.count==0)
{
  //没有
}

#27


int i = da.Fill(ds);本身就返回填充了多少行,直接判断i即可

#28


    /// <summary>
    /// 过滤SQL语句,防止注入
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns>true - 没有注入, false - 有注入 </returns>
    public bool filterSql(string sSql)
    {
        int srcLen, decLen = 0;
        sSql = sSql.ToLower().Trim();
        srcLen = sSql.Length;
        sSql = sSql.Replace("exec", "");
        sSql = sSql.Replace("master", "");
        sSql = sSql.Replace("truncate", "");
        sSql = sSql.Replace("declare", "");
        sSql = sSql.Replace("create", "");
        sSql = sSql.Replace("xp_", "");
        decLen = sSql.Length;
        return (srcLen == decLen);
    }

#29


mark

#30


 public static DataTable GetTable(DataSet ds)
        {
            DataTable dt;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                }
                else
                {
                    dt = null;
                }
            }
            else
            {
                dt = null;
            }
            return dt;


        }

#31


学习!

#32


本身已经使用了DataSet ds = new DataSet();
那不管查询出来有没有数据,ds不会等于null吧

#33


引用 32 楼 dongpo888 的回复:
本身已经使用了DataSet ds = new DataSet();
那不管查询出来有没有数据,ds不会等于null吧


同意!

#34


学习

#35


isnull(字段,'')

#36


首先看看有没有表
然后再看看有没有记录,呵呵

#37


if(ds.tables[0].rows.count==0)
{
  //no records
}

#38


学习了

#39


一群傻逼,人家问sql中怎么判断,没看见题目就答题

#1


ds.tables[0].rows.count==0

#2


ds的tables[0]有多少行

#3


一楼正解
ds.tables[0].rows.count

#4


你这里用DataSet 的 很好解决
ds有 tables[]属性  然后再点属性rows.count  
  楼上以回答了 。。

#5


我在想,都用了DataSet ds = new DataSet();
如何还用ds == null去判断?如果Fill有表的话,是用ds.tables[0].rows.count去判断

#6


一楼正解

#7


ds是不是为null,还有就是判断ds.table.count是不是>0

#8


 if (ds.Tables[0].Count>0 )
{}或SqlDateReader

#9


if (ds.Tables[0].Rows.Count>0){}

#10


引用 9 楼 wuyq11 的回复:
if (ds.Tables[0].Rows.Count>0){}

如果我查询的是一个字段的怎么判断呢?
比如
string sou="select name from art where name like '%张三%'";
sqlcommand cmd=new sqlcommad(sou,conn);
string name=cmd.ExecuteScalar().tostring;
如果查询出来没有张三这个名字,就是说值为空的话,上面这个代码会出什么样的问题?怎么解决?
怎么判断他的直为空?

#11


看看

#12


string sou="select name from art where name like '%张三%'"; 
sqlcommand cmd=new sqlcommad(sou,conn);
int i =cmd.ExecuteScalar(); 
if(i=0)
{
messagebox.show("结果为空");
}
else
{
//你的代码
}

我很菜,可我很热心!

#13


这是多么容易的注入漏洞啊。。。

像我也能写出针对这个代码的注入语句了。。

你至少也应该替换 '''

#14


引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?

#15


引用 14 楼 yangzheng1128 的回复:
引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?

.......
..........

#16


取表的行数不就知道有没记录了

#17


引用 1 楼 code163 的回复:
ds.tables[0].rows.count==0

正解

#18


学习中啊

#19


ds.tables[0].rows.count==0

#20


引用 14 楼 yangzheng1128 的回复:
引用 13 楼 jxyxhz 的回复:
这是多么容易的注入漏洞啊。。。
像我也能写出针对这个代码的注入语句了。。
你至少也应该替换'为''

什么意思?不懂...能告诉我哪里应该改吗?好加强安全措施?


在你的Textbox1里面输入字符串  a' or 1=1--

然后你执行下试试

#21


gridview 等其它数据源控件 提供了数据为空的模版,,在里面写就行了.

#22


引用 1 楼 code163 的回复:
ds.tables[0].rows.count==0


up

#23


如果怕注入,就用raplace来替换引号咯。或者用存储过程吧。

#24


if (ds.Tables["art"].Rows.Count == 0)//art是你的表名
{
     Page.ClientScript.RegisterStartupScript(GetType(), "sf", "<script language='javascript'>alert('没有记录').focus()</script>");
}
else
{
    this.sou.DataSource = ds;
    this.sou.DataBind();
}

#25


//此句
Page.ClientScript.RegisterStartupScript(GetType(), "sf", " <script language='javascript'>alert('没有记录')</script>"); 

#26


if(ds.tables[0].rows.count==0)
{
  //没有
}

#27


int i = da.Fill(ds);本身就返回填充了多少行,直接判断i即可

#28


    /// <summary>
    /// 过滤SQL语句,防止注入
    /// </summary>
    /// <param name="strSql"></param>
    /// <returns>true - 没有注入, false - 有注入 </returns>
    public bool filterSql(string sSql)
    {
        int srcLen, decLen = 0;
        sSql = sSql.ToLower().Trim();
        srcLen = sSql.Length;
        sSql = sSql.Replace("exec", "");
        sSql = sSql.Replace("master", "");
        sSql = sSql.Replace("truncate", "");
        sSql = sSql.Replace("declare", "");
        sSql = sSql.Replace("create", "");
        sSql = sSql.Replace("xp_", "");
        decLen = sSql.Length;
        return (srcLen == decLen);
    }

#29


mark

#30


 public static DataTable GetTable(DataSet ds)
        {
            DataTable dt;
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                }
                else
                {
                    dt = null;
                }
            }
            else
            {
                dt = null;
            }
            return dt;


        }

#31


学习!

#32


本身已经使用了DataSet ds = new DataSet();
那不管查询出来有没有数据,ds不会等于null吧

#33


引用 32 楼 dongpo888 的回复:
本身已经使用了DataSet ds = new DataSet();
那不管查询出来有没有数据,ds不会等于null吧


同意!

#34


学习

#35


isnull(字段,'')

#36


首先看看有没有表
然后再看看有没有记录,呵呵

#37


if(ds.tables[0].rows.count==0)
{
  //no records
}

#38


学习了

#39


一群傻逼,人家问sql中怎么判断,没看见题目就答题