Microsoft.ApplicationBlocks使用心得(转贴自:http://www.cnblogs.com/dannyr)

本文介绍了使用SQL进行数据库操作的几种常见技巧,包括如何查询单行ID、插入新记录并获取其ID,以及查询多行记录的方法。同时,文章推荐使用SqlParameter以提高效率。


查询返回单行ID

None.gifstring sql = string.Format("select id from account where name='{0}' and password='{1}'", name, password);
None.gif
object AID = SqlHelper.ExecuteScalar(CON_STR, CommandType.Text, sql);
None.gif
int ID = Convert.ToInt32(AID);


插入返回新记录ID
None.gifstring sql = string.Format("insert into account (name ,password) values ('{0}', '{1}'); Select CAST(@@Identity AS INTEGER)", account.Name, account.Password);
None.gif
object AID = SqlHelper.ExecuteScalar(CON_STR, CommandType.Text, sql );
None.gif
int id = Convert.ToInt32(AID);


查询返回多行记录

None.gifstring sql = string.Format("select id, name, password from account where id={0}", id);
None.gifSqlDataReader dr 
= SqlHelper.ExecuteReader(CON_STR, CommandType.Text, sql);
None.gif
if (dr.Read()) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    acc 
=  new Account(dr.GetInt32(0), dr.GetString(1), dr.GetString(2));
ExpandedBlockEnd.gif}

None.gifdr.Close();


应该用SqlParameter来构造,并且DAAB带有Cache,可以提高效率.

转载于:https://www.cnblogs.com/miaomiaoga/archive/2004/11/09/61896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值