1 2 3 4 5Prettify 6 7 8 15 22 23 2425 // 异步方式 26 // 结果以事件回调方式返回 27 Command command = new Command("/system/bin/ping", 28 "-c", "4", "-s", "100", 29 "www.baidu.com"); 30 Command.command(command, new Command.CommandListener() { 31 @Override 32 public void onCompleted(String str) { 33 Log.i(TAG, "onCompleted:\n" + str); 34 } 35 @Override 36 public void onCancel() { 37 Log.i(TAG, "onCancel"); 38 } 39 @Override 40 public void onError() { 41 Log.i(TAG, "onError"); 42 } 43 }); 444546 using System; 47 using System.Collections; 48 using System.Collections.Generic; 49 using System.Configuration; 50 using System.Data; 51 using System.Data.SqlClient; 52 using System.Linq; 53 using System.Text; 54 55 namespace Justin.FrameWork.Helper 56 { 57 ///224 22558 /// Sql访问数据库帮助类 59 /// 60 public abstract class SqlHelper 61 { 62 63 ///64 /// 如"data source=.;initial catalog=PrivilegeDB;integrated security=True;multipleactiveresultsets=True;" 65 /// 66 public static string ConnStr = ""; 67 public static int CommandTimeout = 600; 68 69 private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); 70 71 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) 72 { 73 SqlCommand cmd = new SqlCommand(); 74 using (SqlConnection connection = new SqlConnection(connectionString)) 75 { 76 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 77 int val = cmd.ExecuteNonQuery(); 78 cmd.Parameters.Clear(); 79 return val; 80 } 81 } 82 public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) 83 { 84 SqlCommand cmd = new SqlCommand(); 85 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 86 int val = cmd.ExecuteNonQuery(); 87 cmd.Parameters.Clear(); 88 return val; 89 } 90 public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) 91 { 92 SqlCommand cmd = new SqlCommand(); 93 cmd.CommandTimeout = 600; 94 PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); 95 int val = cmd.ExecuteNonQuery(); 96 cmd.Parameters.Clear(); 97 return val; 98 } 99 100 public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)101 {102 SqlCommand cmd = new SqlCommand();103 try104 {105 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);106 SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);107 cmd.Parameters.Clear();108 return rdr;109 }110 111 catch112 {113 connection.Close();114 throw;115 }116 }117 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)118 {119 SqlCommand cmd = new SqlCommand();120 using (SqlConnection connection = new SqlConnection(connectionString))121 {122 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);123 object val = cmd.ExecuteScalar();124 cmd.Parameters.Clear();125 return val;126 }127 128 }129 public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)130 {131 SqlCommand cmd = new SqlCommand();132 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);133 object val = cmd.ExecuteScalar();134 cmd.Parameters.Clear();135 return val;136 }137 138 public static DataTable ExecuteDataTable(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)139 {140 141 SqlCommand cmd = new SqlCommand();142 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);143 SqlDataAdapter MyAdapter = new SqlDataAdapter();144 MyAdapter.SelectCommand = cmd;145 DataSet ds = new DataSet();146 MyAdapter.Fill(ds);147 cmd.Parameters.Clear();148 DataTable table = ds.Tables[0];149 ds.Dispose();150 return table;151 }152 public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)153 {154 using (SqlConnection connection = new SqlConnection(connectionString))155 {156 SqlCommand cmd = new SqlCommand();157 158 try159 {160 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);161 SqlDataAdapter MyAdapter = new SqlDataAdapter();162 MyAdapter.SelectCommand = cmd;163 DataSet ds = new DataSet();164 MyAdapter.Fill(ds);165 cmd.Parameters.Clear();166 DataTable table = ds.Tables[0];167 ds.Dispose();168 connection.Close();169 return table;170 }171 catch172 {173 connection.Close();174 throw;175 }176 }177 }178 179 180 public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)181 {182 parmCache[cacheKey] = commandParameters;183 }184 public static SqlParameter[] GetCachedParameters(string cacheKey)185 {186 187 SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];188 if (cachedParms == null)189 return null;190 SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];191 for (int i = 0, j = cachedParms.Length; i < j; i++)192 clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();193 return clonedParms;194 }195 private static void PrepareCommand(SqlCommand cmd, SqlConnection connection, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)196 {197 if (connection.State != ConnectionState.Open)198 connection.Open();199 cmd.Connection = connection;200 cmd.CommandText = cmdText;201 cmd.CommandTimeout = CommandTimeout;202 if (trans != null)203 cmd.Transaction = trans;204 cmd.CommandType = cmdType;205 if (cmdParms != null)206 {207 foreach (SqlParameter parm in cmdParms)208 cmd.Parameters.Add(parm);209 210 }211 212 }213 214 215 public static SqlConnection GetConnection(string connectionString)216 {217 return new SqlConnection(connectionString);218 }219 }220 }221 222 223