日期:2014-05-17  浏览次数:20416 次

查询数据库,如果查询记录为空,显示对话框”对不起没有数据“该怎么写
  string Sql1 = "select T1 from book where booktime='" + TextBox1.Text + "'";
            SqlCommand cmd1 = new SqlCommand(Sql1, connstring);
            SqlDataReader dr1 = cmd1.ExecuteReader();
            if (dr1.Read())
            {
                this.Label14.Text = dr1[0].ToString();
            }

            string st4 = Label14.Text.Trim();
            try
            {
                Response.Write(Convert.ToDateTime(st4).ToString());

                DateTime dt3 = Convert.ToDateTime(st4);
如果查询的T1是空值,直接显示对话框”对不起没有数据“,然后程序停止执行,停留在当前页面

------解决方案--------------------
 if (dr1.Read())
 {
     this.Label14.Text = dr1[0].ToString();
 }
 else
{
   MessageBox.Show("对不起没有数据");
   return;
}