/// <summary> /// 执行一个select命令并返回行数 /// </summary> /// <param name="command"></param> /// <returns></returns> public static string ExecuteScalar(DbCommand command) { //将要返回的值 string value = string.Empty; try { command.Connection.Open(); value = command.ExecuteScalar().ToString(); } catch (Exception ex) { CUtilities.LogError(ex); throw; } finally { command.Connection.Close(); } return(value); }
/// <summary> /// 执行一个update,delete,insert 命令返回影响的数据的行数 /// </summary> /// <param name="command"></param> /// <returns></returns> public static int ExecuteNonQuery(DbCommand command) { //受影响的行数 int affectedRows = -1; try { command.Connection.Open(); affectedRows = command.ExecuteNonQuery(); } catch (Exception ex) { CUtilities.LogError(ex); throw; } finally { command.Connection.Close(); } return(affectedRows); }
//执行dbcommand 并返回一个datatable public static DataTable ExecuteSelectCommand(DbCommand command) { DataTable table; try { command.Connection.Open(); DbDataReader reader = command.ExecuteReader(); table = new DataTable(); table.Load(reader); reader.Close(); } catch (Exception ex) { CUtilities.LogError(ex); throw; } finally { command.Connection.Close(); } return(table); }