guangod 2010-04-25
代码如下:
create procedure procNewsSelectNewNews as begin select top 10 n.id,n.title,n.createTime,c.name from news n inner join category c on n.caId=c.id order by n.createTime desc end
代码如下:
public DataTable test(string procName) { DataTable dt=new DataTable(); cmd=new SqlCommand(procName,GetConn()); //数据库连接和连接开闭,都放在了GetConn()方法中 cmd.CommandType=CommandType.StoredProcedure; //定义SQL语句命令类型为存储过程 using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection));//方法将SQL语句发送给SqlConnection并生产一个SqlDataReader类对象,该SqlDataReader对象包含SQL命令返回的数据 { dt.Load(sdr); //load查询dataread查询的结果 } return dt; }
代码如下:
public DataTable ExecuteQuery(string sqlText,CommandType ct); //不仅传入SQL语句还传入一个命令类型 { DataTable dt=new DataTable(); cmd=new SqlCommand(sqlText,GetConn()); cmd.CommandType=ct; using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr);} return dt; }
代码如下:
public DataTable SelectNewNews() { return sqlhelper.ExecuteQuery(“存储过程名”,CommandType.StoredProcedure) }
代码如下:
public DataTable SelectAll() { DataTable dt=new DataTable(); string sql=”select * from news”; dt=sqlhelper.ExecuteQuery(sql,CommandType.Text);・ return dt }