重构sqlconnection/sqlcommand/sqldatareader/datatable
时间:2010-11-13 来源:雨中的失落
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
public int ExecuteNonQuery(string sql)
{
int res;
try
{
cmd = new SqlCommand(sql, GetConn());
cmd.CommandTimeout = 60;
res = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
public DataTable ExecuteQuery(string sql)
{
try
{
dt = new DataTable();
cmd = new SqlCommand(sql, GetConn());
rd = cmd.ExecuteReader();
dt.Load(rd);
rd.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return dt;
}
}
}