最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net数据提交判断数据是否存在
时间:2022-06-25 06:01:36 编辑:袖梨 来源:一聚教程网
大概思路是
abc=select 姓名 from login 姓名='name'
rs.open abc,conn,1
if not rs.EOF then
有记录
else
无记录
end if
具体方法
asp.net中如何查询一条记录是否在一张表中如果存在就把信息返回给Grideview
如果不存在就返回给Lable1.Text="你查询的数据不存在!";if(这个括号里面应该怎么写?)//记录存在
{
SqlDataAdapter myda = new SqlDataAdapter("select * from post where postID like'" + TextBox1.Text + "'", myconn);
DataSet myds = new DataSet();
myda.Fill(myds, "table");
myconn.Close();
DataSet ds = myds;
GridView1.DataSource = ds;
GridView1.DataBind();
}
else//记录不存在
{
Lable1.Text="你查询的数据不存在!";
TextBox1.Text="";
}
解决方案 »
select count(*) from post where postID like'" + TextBox1.Text + " 判断一下 数据行数 就好啦
这个是常用的方法。ds.Tables[0].Rows.Count>0就是有数据,反之没有。
SqlDataAdapter myda = new SqlDataAdapter("select * from post where postID like'" + TextBox1.Text + "'", myconn);
DataSet myds = new DataSet();
myda.Fill(myds, "table");
myconn.Close();
DataSet ds = myds;
if(ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else//记录不存在
{
Lable1.Text="你查询的数据不存在!";
TextBox1.Text="";
}
select count(0) from table where ....返回结果为0就是没数据,否则有几行则返回数字。
string mySel="SELECT count(*) as iCount from gly where g_name='"+TextBox1.Text+"'";
SqlCommand myCmd1=new SqlCommand(mySel,myConn);
myCmd1.Connection.Open();
SqlDataReader Dr1=myCmd1.ExecuteReader();
Dr1.Read();
string Count=Dr1["iCount"].ToString();
Dr1.Close();
if(Count!="0")
{
//记录存在
}
else//记录不存在
{
Lable1.Text="你查询的数据不存在!";
TextBox1.Text="";
}
SqlDataAdapter myda = new SqlDataAdapter("select * from post where postID like'" + TextBox1.Text + "'", myconn);
DataSet myds = new DataSet();
myda.Fill(myds, "table");
myconn.Close();
if (myds.Tables[0].Rows == 0)
{
Lable1.Text="你查询的数据不存在!";
TextBox1.Text="";
}
else
{
GridView1.DataSource = myds;
GridView1.DataBind()
}
SqlDataAdapter myda = new SqlDataAdapter("select * from post where postID like'" + TextBox1.Text + "'", myconn);
DataSet myds = new DataSet();
myda.Fill(myds, "table");
myconn.Close(); if(myds.Tables[0].Rows.Count>0)//记录存在
{
GridView1.DataSource = myds;
GridView1.DataBind();
}
else//记录不存在
{
Lable1.Text="你查询的数据不存在!";
TextBox1.Text="";
}不要生成多余的DataSet,占资源的,你最好优化一下,用Datareader
相关文章
- 王者万象棋终测新角色曝光 王者万象棋终极测试英雄阵容与技能详解 04-01
- 江城创业记投壶玩法详解 江城创业记高命中率投壶技巧与实操指南 04-01
- 生存33天AK老奶玩法解析 生存33天AK老奶技能机制与实战技巧 04-01
- 白银之城男角色介绍 白银之城主要男性角色全解析 04-01
- 星布谷地测试时间安排 星布谷地最新测试详情一览 04-01
- 烽烟手游精灵推荐 烽烟手游最强精灵阵容与培养攻略 04-01