帮我看看这个ASP.NET语句··········

来源:百度知道 编辑:UC知道 时间:2024/05/30 01:09:17
public static SqlDataReader ExcuteRead(string nText, CommandType nType, SqlParameter[] paras)
{
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
try
{
...
...
}
catch (SqlException e)
{
...
}
}

public static int ExcuteNonQurey(string nText, CommandType nType, SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(strConn))
{
...
...
}
}

---------------------------
---------------------------
问题是这样的
为什么有的时候用using
有的用try呢

什么时候用哪个语句呢

因为DataReader 是连线操作的。
try和using不是同一个概念
try
catch 是做异常捕捉用的
using可以理解成自动回收的代码快
在using()中定义的变量一旦离开当前范围会被立刻强制回收。
在DataReader中这样定义的话DataReader将读取不到任何数据

using 不用也行

try 是捕获 异常 就是那个地方有可能出现 错误 +个try可以 捕获到异常情况