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="";
}
如果不存在就返回给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="";
}
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="";
}
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
{
myconn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from post where postID='" + TextBox1.Text + "'", myconn);
int count =Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
return true;
}
else
{
return false;
}
}
if(jugle(textbox.text))
{}