博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
prettyprint
阅读量:5070 次
发布时间:2019-06-12

本文共 8025 字,大约阅读时间需要 26 分钟。

1   2   3   4     
5 Prettify 6
7 8 15 22 23 24
 25     // 异步方式 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     }); 44 
45
 46 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     ///  58     /// 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 
224 225

 

转载于:https://www.cnblogs.com/piaolingzxh/p/4127104.html

你可能感兴趣的文章
[GraphQL] Reuse Query Fields with GraphQL Fragments
查看>>
Illustrated C#学习笔记(一)
查看>>
理解oracle中连接和会话
查看>>
两种最常用的Sticky footer布局方式
查看>>
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
【CF888E】Maximum Subsequence 折半搜索
查看>>
Java程序IP v6与IP v4的设置
查看>>
RUP(Rational Unified Process),统一软件开发过程
查看>>
eclipse下的tomcat内存设置大小
查看>>
数据库链路创建方法
查看>>
linux文件
查看>>
Linux CentOS6.5上搭建环境遇到的问题
查看>>
Enterprise Library - Data Access Application Block 6.0.1304
查看>>
重构代码 —— 函数即变量(Replace temp with Query)
查看>>
vmware tools 的安装(Read-only file system 的解决)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>