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 }
  • 相关阅读:
    runlevel=$(set -- $(runlevel); eval "echo $$#" )
    MPLS
    sql server 查询存储过程返回值
    sql 游标的关闭和释放
    sql 查询某一列最大的数据
    flex label如何通过AS3实现颜色设置
    sql server 字符串拆分
    Linux centos 解决"不在 sudoers 文件中。此事将被报告"的问题
    Flex String拼接
    flex 判断对象的类型
  • 原文地址:https://www.cnblogs.com/ygd-boke/p/4398389.html
Copyright © 2011-2022 走看看