1. 开启资源应注意尽早释放,关闭
第一种方式:
Try
{
Conn.open();
具体处理部分
}
Catch(sqlException ex)
{
}
Finally
{
Conn.close();
}
2. 使用using 语句块
Using (sqlconnection conn = new sqlconnection( connectstring) )
{
Conn.open() ;
具体处理语句;
}
最好以上两种方式结合使用;
3.利用事务块处理
Using (Transactionscope scope = new TransactionScope( TransactionScopeOption.Required))
{
具体处理语句;
Scope.complete () ;
}
3. 调用存储过程
Create procedure regionupdate (@regionid interger , @regiondescription nchar(50)) as
Set nocount off
Update region
Set regiondescription = @regiondescription
Where region = @regionid
Go
Sqlcommand cmd = new sqlcommand(“过程名” , conn);
cmd.commandtype = commandtype.storedprocedure ;
cmd.parameters.addwithvalue(“@tegionid”,23);
cmd.parameters.addwithvalue(“@tegiondescription”,”something”);
,23);
nid"ters.addwithvalue(".storedprocedure ;egiondescription Option.Required))
cmd.executenonquery();