日期:2014-05-18  浏览次数:20804 次

如何判断查询结果不相符,提示不存在
sql语句查询与textbox相符的结果,显示在datagridview中。
    如果没有相符的,datagridview显示空记录,弹出提示没有找到相符的,这个代码如何写呢?

------解决方案--------------------
string selectstr = String.Format( "SELECT * FROM Table WHERE Field = '{0} ' ",this.TextBox1.Text);
string ConnectionString = "你的连库字符串 ";
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = ConnectionString;
myconn.Open();
SqlDataAdapter myda = new SqlDataAdapter();
myda.SelectCommand = new SqlCommand(selectstr, myconn);
DataSet ds = new DataSet();
myda.Fill(ds);
myconn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
//有数据
}
else
{
//无
}
------解决方案--------------------
把查询出来的是数据放入一个DataSet,然后和TextBox能文本对比,筛选出数据放如另一个DataSet,判断是否为空即可.
------解决方案--------------------
if(ds.Rows.Count > 0 )
{ this.dataGridView1.DataSource = ds.Tables[0];
else
{MessageBox.Show( "没有找到符合的记录 ");}
------解决方案--------------------
if(ds.Tables[ "... "].Rows.Count==0)
{

}
else
{
} }