在线跪求:c#sqlcommand搜索不到数据返回?

来源:百度知道 编辑:UC知道 时间:2024/09/22 20:36:30
string str;
SqlConnection con = new SqlConnection("server=(local);database=xwyu;integrated security=true");
SqlCommand com = new SqlCommand();
com.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
com.CommandText = "select * from mangerpwd where muser=@mname";
com.Parameters.Add("@mname", SqlDbType.VarChar);
com.Parameters["@mname"].Value = textBox1.Text;
da.SelectCommand = com;
da.Fill(ds);
DataTable dt = ds.Tables[0];
如果没有和muser=@mname匹配的行,则select * from mangerpwd where muser=@mname返回什么?ds,dt分别为什么值?若为NULL,则应执行以下:
if (ds == null) { MessageBox.Show("dsnull"); }
if (dt == null) { MessageBox.Show("dtnull"); }
但调试是不执行,那应该若没有和muser=@mname匹配的行,select * from mangerpwd

dt.rows.count=0

可以考虑 red_guitar说的.判断DT的行.如果有行则表示搜到了值.如果为0则表示结果为空

string str;
SqlConnection con = new SqlConnection("server=(local);database=xwyu;integrated security=true");
SqlCommand com = new SqlCommand();
com.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
//DataSet ds = new DataSet();
DataTable dt=new DataTable(); //直接用DataTable代替
com.CommandText = "select * from mangerpwd where muser=@mname";
com.Parameters.Add("@mname", SqlDbType.VarChar);
com.Parameters["@mname"].Value = textBox1.Text;
da.SelectCommand = com;
//da.Fill(ds);
da.Fill(dt);
//DataTable dt = ds.Tables[0];
//判断行是否为空
int row=dt.rows.count;
if (row==0)
{
MessageBox.Show("dtnull");
}

我不知道你判断ds做什么.个人感觉你使用ds无非也是想用dt来获得查询的结果.可以直接用dt代替ds的..
上面的代码是用dt代替ds的.如果ds有用.那么只需要添加"判断行是否为空"下面的代码,用来检测dt是否没有行