SQLDataReader Vs. DataSet(好多人弄不清楚,这回大家可以看看了)
2024-07-21 02:23:50
供稿:网友
sqldatareader vs datasetsubmitted byuser leveldate of submissionarnold parkintermediate05/11/2001
objective:
to compare and contrast sqldatareader and sqldatasetcommand
target audience
ado.net programmer
environment
sql2000, visual studio .net beta 1
ibuyspy database
structure of table
create table [dbo].[orderdetails] (
[orderid] [int] not null ,
[productid] [int] not null ,
[quantity] [int] not null ,
[unitcost] [money] not null
) on [primary]
go
create table [dbo].[products] (
[productid] [int] identity (1, 1) not null ,
[categoryid] [int] not null ,
[modelnumber] [nvarchar] (50) collate korean_wansung_ci_as null ,
[modelname] [nvarchar] (50) collate korean_wansung_ci_as null ,
[productimage] [nvarchar] (50) collate korean_wansung_ci_as null ,
[unitcost] [money] not null ,
[description] [nvarchar] (3800) collate korean_wansung_ci_as null
) on [primary]
stored procedure
/* procedure name:net_popularproduct_selectmaster
* objective :weekly best items top 5
*/
create procedure net_popularproduct_selectmaster
as
begin
select top 5 o.productid,sum(o.quantity) as total,p.modelname
from orderdetails o,products p
where o.productid=p.productid
group by o.productid,p.modelname
order by total desc
end
*****************source code
1)using sqldatasetcommandconn=new common.dbconn();
sqldatasetcommand comm=new sqldatasetcommand("net_popularproduct_selectmaster",conn);
comm.selectcommand.commandtype=commandtype.storedprocedure;
dataset ds=new dataset();
comm.filldataset(ds,"popitems");
return ds;
2)using sqldatareaderconn=new common.dbconn();
sqlcommand comm=new sqlcommand("net_popularproduct_selectmaster",conn);
comm.commandtype=commandtype.storedprocedure;
conn.open();
comm.execute(out reader);
datatable table=new datatable("top5");
table.columns.add(new datacolumn("product_id",typeof(system.int32)));
table.columns.add(new datacolumn("quantity",typeof(system.int32)));
table.columns.add(new datacolumn("product_name",typeof(system.string)));
while(reader.read())
{
datarow=table.newrow();
datarow[0]=reader.getint32(0);
datarow[1]=reader.getint32(1);
datarow[2]=reader.getstring(2);
table.rows.add(datarow);
}
conn.close();
return table;