} comm.CommandType=CommandType.StoredProcedure ; comm.CommandText =procName; da.SelectCommand =comm; da.Fill(ds); } catch(Exception e) { throw new Exception(e.Message); } finally { comm.Parameters.Clear(); closeConnection(); } } /// <summary> /// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox /// </summary> /// <param name="sqlstr">传入的Sql语句</param> /// <returns>object 返回值 </returns> public static object ExecuteScalar(string sqlstr) { object obj=new object(); try { openConnection(); comm.CommandType =CommandType.Text ; comm.CommandText =sqlstr; obj=comm.ExecuteScalar(); } catch(Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } return obj; } /// <summary> /// 执行Sql查询语句,同时进行事务处理 /// </summary> /// <param name="sqlstr">传入的Sql语句</param> public static void ExecuteSqlWithTransaction(string sqlstr) { SqlTransaction trans ; trans=conn.BeginTransaction(); comm.Transaction =trans; try { openConnection(); comm.CommandType =CommandType.Text ; comm.CommandText =sqlstr; comm.ExecuteNonQuery(); |