public DataTable SlcClgByYear(string str)
{
try
{
str = str + "%";
DataTable dt = new DataTable();
DataSet ds = new DataSet();
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE 'str'";
SqlConnection conn = new SqlConnection(DAL.DALConfig.ConnectionString); SqlDataAdapter da = new SqlDataAdapter(sqlstr,conn);
da.Fill(ds); ds.Tables[0].TableName = "College"; return ds.Tables[0];
return dt;
}
catch(Exception exc)
{
throw exc;
}
}
执行后返回值为空,为什么会这样呢?
{
try
{
str = str + "%";
DataTable dt = new DataTable();
DataSet ds = new DataSet();
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE 'str'";
SqlConnection conn = new SqlConnection(DAL.DALConfig.ConnectionString); SqlDataAdapter da = new SqlDataAdapter(sqlstr,conn);
da.Fill(ds); ds.Tables[0].TableName = "College"; return ds.Tables[0];
return dt;
}
catch(Exception exc)
{
throw exc;
}
}
执行后返回值为空,为什么会这样呢?
少个%把
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE '"+str+"'";
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE 'str'";
要是想你这样的话,str就不时变量了,成了一个字符串了
DataTable dt = new DataTable();
DataSet ds = new DataSet();
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE '";
sqlstr += str;
sqlstr +=@"%'";
SqlConnection conn = new SqlConnection(DAL.DALConfig.ConnectionString);
的回答少了一个%,应该为:
string sqlstr = @"SELECT collegeName FROM College WHERE collegeID LIKE '"+str+"%'";