zoukankan      html  css  js  c++  java
  • C# SQLiteHelper

     1 public class SQLiteHelpers
     2     {
     3         /// <summary>          
     4         /// ConnectionString样例:Datasource=Test.db3;Pooling=true;FailIfMissing=false  
     5         /// </summary>          
     6         public static string ConnectionString { get; set; }
     7         private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p)
     8         {
     9             if (conn.State != ConnectionState.Open)
    10                 conn.Open();
    11             cmd.Parameters.Clear();
    12             cmd.Connection = conn;
    13             cmd.CommandText = cmdText;
    14             cmd.CommandType = CommandType.Text;
    15             cmd.CommandTimeout = 30;
    16             if (p != null)
    17             {
    18                 foreach (object parm in p)
    19                     cmd.Parameters.AddWithValue(string.Empty, parm);
    20             }
    21         }
    22         public static DataSet ExecuteQuery(string cmdText, params object[] p)
    23         {
    24             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
    25             {
    26                 using (SQLiteCommand command = new SQLiteCommand())
    27                 {
    28                     DataSet ds = new DataSet();
    29                     PrepareCommand(command, conn, cmdText, p);
    30                     SQLiteDataAdapter da = new SQLiteDataAdapter(command);
    31                     da.Fill(ds);
    32                     return ds;
    33                 }
    34             }
    35         }
    36         public static int ExecuteNonQuery(string cmdText, params object[] p)
    37         {
    38             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
    39             {
    40                 using (SQLiteCommand command = new SQLiteCommand())
    41                 {
    42                     PrepareCommand(command, conn, cmdText, p); 
    43                     return command.ExecuteNonQuery();
    44                 }
    45             }
    46         }
    47         public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p)
    48         {
    49             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
    50             {
    51                 using (SQLiteCommand command = new SQLiteCommand())
    52                 {
    53                     PrepareCommand(command, conn, cmdText, p); 
    54                     return command.ExecuteReader(CommandBehavior.CloseConnection);
    55                 }
    56             }
    57         }
    58         public static object ExecuteScalar(string cmdText, params object[] p)
    59         {
    60             using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
    61             {
    62                 using (SQLiteCommand command = new SQLiteCommand())
    63                 {
    64                     PrepareCommand(command, conn, cmdText, p); 
    65                     return command.ExecuteScalar();
    66                 }
    67             }
    68         }
    69     }
    70 }
  • 相关阅读:
    Gist
    Gist
    Gist
    汉字编码与其16进制对照
    Horizon组件安装详解
    Github目录生成器
    MVC模式网站编写经验总结
    Java多线程小结
    JGit与远程仓库链接使用的两种验证方式(ssh和https)
    Peterson算法与Dekker算法解析
  • 原文地址:https://www.cnblogs.com/ygd-boke/p/4398389.html
Copyright © 2011-2022 走看看