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
我的代码如下
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
解决方案 »
- 如何向后台传值?
- Coalesys.PanelBar 求助.怎么绑定数据.. 急
- 命名空间??
- ASP.NET里如何控制线程安全?
- C#&ASP.NET
- 如果超出一点的字数(好像最多300),就无法插入。如果字数少于这个数目,就能正常插入。
- 新手问使用datagird显示数据库中的数据问题?在线等待!先谢谢了!!
- 紧急求救!在asp.net/c#下调用标准DLL. 在本机器上可正常运行,其他机器上则出现容量错误.
- 这种需求做成Winform or Webform大家说!
- 急!!"Web"不受信任的Web服务器上
- 如何在asp.net中发送一个取消会议的outlook邮件?
- 谁有比较小的asp.net项目需求
ds.tables[0].rows.count
ds有 tables[]属性 然后再点属性rows.count
楼上以回答了 。。
如何还用ds == null去判断?如果Fill有表的话,是用ds.tables[0].rows.count去判断
{}或SqlDateReader
比如
string sou="select name from art where name like '%张三%'";
sqlcommand cmd=new sqlcommad(sou,conn);
string name=cmd.ExecuteScalar().tostring;
如果查询出来没有张三这个名字,就是说值为空的话,上面这个代码会出什么样的问题?怎么解决?
怎么判断他的直为空?
sqlcommand cmd=new sqlcommad(sou,conn);
int i =cmd.ExecuteScalar();
if(i=0)
{
messagebox.show("结果为空");
}
else
{
//你的代码
}我很菜,可我很热心!
在你的Textbox1里面输入字符串 a' or 1=1--然后你执行下试试
{
Page.ClientScript.RegisterStartupScript(GetType(), "sf", "<script language='javascript'>alert('没有记录').focus()</script>");
}
else
{
this.sou.DataSource = ds;
this.sou.DataBind();
}
Page.ClientScript.RegisterStartupScript(GetType(), "sf", " <script language='javascript'>alert('没有记录')</script>");
{
//没有
}
/// 过滤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);
}
{
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;
}
那不管查询出来有没有数据,ds不会等于null吧
{
//no records
}