<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prettify</title>
<link href="prettify.min.css" rel="stylesheet">
<script src="prettify.min.js"></script>
<style>
pre.prettyprint
{
border: 2px dashed #;
background: #ddd;
}
</style>
<script>
window.onload = function ()
{
// Load Prettify
prettyPrint();
}
</script>
</head>
<body>
<pre class="prettyprint lang-java">
// 异步方式
// 结果以事件回调方式返回
Command command = new Command("/system/bin/ping",
"-c", "", "-s", "",
"www.baidu.com");
Command.command(command, new Command.CommandListener() {
@Override
public void onCompleted(String str) {
Log.i(TAG, "onCompleted:\n" + str);
}
@Override
public void onCancel() {
Log.i(TAG, "onCancel");
}
@Override
public void onError() {
Log.i(TAG, "onError");
}
});
</pre>
<pre class="prettyprint lang-C#">
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace Justin.FrameWork.Helper
{
/// <summary>
/// Sql访问数据库帮助类
/// </summary>
public abstract class SqlHelper
{ /// <summary>
/// 如"data source=.;initial catalog=PrivilegeDB;integrated security=True;multipleactiveresultsets=True;"
/// </summary>
public static string ConnStr = "";
public static int CommandTimeout = ; private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = ;
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
} catch
{
connection.Close();
throw;
}
}
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
} }
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
} public static DataTable ExecuteDataTable(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{ SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
MyAdapter.Fill(ds);
cmd.Parameters.Clear();
DataTable table = ds.Tables[];
ds.Dispose();
return table;
}
public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(); try
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
MyAdapter.Fill(ds);
cmd.Parameters.Clear();
DataTable table = ds.Tables[];
ds.Dispose();
connection.Close();
return table;
}
catch
{
connection.Close();
throw;
}
}
} public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
public static SqlParameter[] GetCachedParameters(string cacheKey)
{ SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = , j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection connection, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (connection.State != ConnectionState.Open)
connection.Open();
cmd.Connection = connection;
cmd.CommandText = cmdText;
cmd.CommandTimeout = CommandTimeout;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm); } } public static SqlConnection GetConnection(string connectionString)
{
return new SqlConnection(connectionString);
}
}
} </pre>
</body>
</html>