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

用asp.net三层架构在dal层写个查询的方法怎么写
我想在textbox输入查询字段显示gridview上

C# code
public static List<NewsClass> GetAllNewsClass()
        {
            string sql = "select * from NewsClass";
            DataTable dt = DBHelper.GetDataSet(sql);
            List<NewsClass> list = new List<NewsClass>();
            foreach (DataRow dr in dt.Rows)     
            {
                NewsClass newsclass = new NewsClass();
                newsclass.NewsCId = (int)dr["NewsCId"];
                newsclass.NewsCName = dr["NewsCName"].ToString();
                list.Add(newsclass);
            }
            return list;
        }


------解决方案--------------------
public static List<NewsClass> GetAllNewsClass(string keyWord)
{
string sql = "select * from NewsClass where Name like '%"+keyWord+"%'";
DataTable dt = DBHelper.GetDataSet(sql);
List<NewsClass> list = new List<NewsClass>();
foreach (DataRow dr in dt.Rows)
{
NewsClass newsclass = new NewsClass();
newsclass.NewsCId = (int)dr["NewsCId"];
newsclass.NewsCName = dr["NewsCName"].ToString();
list.Add(newsclass);
}
return list;
}
------解决方案--------------------
public static List<NewsClass> GetAll()
{
string sql = "select * from EmployeeInfo";
DataTable dt = SqlHelper.GetDataTable(sql);
List<NewsClass> list = new List<NewsClass>();
foreach (DataRow dr in dt.Rows)
{
NewsClass newsclass= new NewsClass();
newsclass.NewsCId = (int)dr["NewsCId"];
newsclass.NewsCName = dr["NewsCName"].ToString();
list.Add(newsclass);
list.Add(newsclass);
}
return list;
}
------解决方案--------------------
C# code
public static List<NewsClass> GetAllNewsClass(string txtName)
        {
            string sql = "select * from NewsClass where name = txtName";
            DataTable dt = DBHelper.GetDataSet(sql);
            List<NewsClass> list = new List<NewsClass>();
            foreach (DataRow dr in dt.Rows)     
            {
                NewsClass newsclass = new NewsClass();
                newsclass.NewsCId = (int)dr["NewsCId"];
                newsclass.NewsCName = dr["NewsCName"].ToString();
                list.Add(newsclass);
            }
            return list;
        }

写个带参的方法,参数就是文本框的值

调用的时候
GetAllNewsClass(this.TxtName.text);

------解决方案--------------------
up 1l



string sql = "select * from NewsClass where name like '%"+Text.Text+"%'";
主要是这句
------解决方案--------------------
C# code

public DataSet GetList(string strWhere)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM News ");
            if(strWhere.Trim()!="")
            {
                strSql.Append(" where " + strWhere + " order by id desc");
            }
            return DbHelperSQL.Query(strSql.ToString());
        }