在类里面写的
public static readonly string constring=configurationmanager.connectionstrings["con"].connectionstring;
private static void fillparameter(sqlparameter[] paramss,sqlcommand sqlcmm)
{
foreach(sqlparameter parm in paramss)
{
sqlcmm.parameters.add(parm);
}
}
public static DataTable ExecuteTable(string sqltext, params sqlparameter[] paramss)
{
using(sqlconnection con=new sqlconnection(constring))
{
using(sqlcommand cmd=con.createcommand())
{
cmd.commandtext=sqltext;
fillparameter(paramss, cmd);
con.open();
using(sqldatareader reader=cmd.executereader())
{
datatable dt=new datatable();
dt.load(reader);
return dt;
}
}
}
}
ASP代码
string sql="select username,case power when 10 then '新闻管理' when 20 then '新闻编辑' end as power from users where power>0";
datatable dt=sqlhelp.executetable(sql);
保持初学者的心态!!!