Ejemplo n.º 1
0
        public static void GetSQLiteDataReader(string sql, SQLiteParameter[] sqlParam, IDbDataReaderCallBack callback)
        {
            try
            {
                SQLiteConnection con = MyConnection.GetSQLiteConnection();
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    cmd.Connection  = con;
                    cmd.CommandText = sql;

                    using (SQLiteDataReader read = cmd.ExecuteReader())
                    {
                        if (null != read)
                        {
                            callback(read);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                //MyLog.DalSqlite.Print.Error("Exception GetSQLiteDataReader" + command.SqlText, ex);
            }
            finally
            {
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 获取首行首列值
 /// </summary>
 /// <param name="command">
 /// 数据操作设置
 /// </param>
 /// <returns>
 /// </returns>
 public static object ExecuteScalar(string sql, SQLiteParameter[] sqlParam)
 {
     try
     {
         using (SQLiteConnection con = MyConnection.GetSQLiteConnection())
         {
             using (SQLiteCommand cmd = new SQLiteCommand())
             {
                 cmd.CommandText = sql;
                 cmd.Connection  = con;
                 if (null != sqlParam && sqlParam.Length > 0)
                 {
                     cmd.Parameters.AddRange(sqlParam);
                 }
                 if (con != null && con.State == ConnectionState.Open)
                 {
                     return(cmd.ExecuteScalar());
                 }
                 else
                 {
                     return(null);
                 }
             }
         }
     }
     catch (Exception e)
     {
         //MyLog.DalSqlite.Print.Error("Exception ExecuteScalar" + command.SqlText, e);
         return(null);
     }
     finally
     {
         //command.Dispose();
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 获取DataTable
 /// </summary>
 /// <param name="key">
 /// 数据库连接标示
 /// </param>
 /// <param name="sql">
 /// sql语句
 /// </param>
 /// <param name="paras">
 /// 参数
 /// </param>
 /// <returns>
 /// </returns>
 public static DataTable GetTable(string sql, SQLiteParameter[] sqlParam)
 {
     try
     {
         using (SQLiteConnection conn = MyConnection.GetSQLiteConnection())
         {
             using (SQLiteCommand cmd = new SQLiteCommand())
             {
                 cmd.CommandText = sql;
                 cmd.Connection  = conn;
                 if (null != sqlParam)
                 {
                     cmd.Parameters.AddRange(sqlParam);
                 }
                 using (SQLiteDataAdapter da = new SQLiteDataAdapter())
                 {
                     da.SelectCommand = cmd;
                     using (DataTable ds = new DataTable())
                     {
                         if (conn != null && conn.State == ConnectionState.Open)
                         {
                             da.Fill(ds);
                             return(ds);
                         }
                         else
                         {
                             return(null);
                         }
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         //MyLog.DalSqlite.Print.Error("Exception GetSQLiteDataReader" + command.SqlText, e);
         return(null);
     }
     finally
     {
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="command">
        /// 数据操作设置
        /// </param>
        /// <returns>
        /// </returns>
        public static bool ExecuteNonQuery(List <string> sql, SQLiteParameter[] sqlParam)
        {
            SQLiteTransaction tran = null;

            try
            {
                using (SQLiteConnection con = MyConnection.GetSQLiteConnection())
                {
                    tran = con.BeginTransaction();
                    using (SQLiteCommand cmd = new SQLiteCommand())
                    {
                        cmd.Connection = con;
                        foreach (var item in sql)
                        {
                            cmd.CommandText = item;
                            if (null != sqlParam)
                            {
                                cmd.Parameters.AddRange(sqlParam);
                            }
                            if (cmd.Connection != null && cmd.Connection.State == ConnectionState.Open)
                            {
                                cmd.ExecuteNonQuery();
                            }
                        }
                        tran.Commit();
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                tran.Rollback();
                return(false);
            }
            finally
            {
                //command.Dispose();
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 执行SQL语句
 /// </summary>
 /// <param name="command">
 /// 数据操作设置
 /// </param>
 /// <returns>
 /// </returns>
 public static int ExecuteNonQuery(string sql, SQLiteParameter[] sqlParam)
 {
     try
     {
         int resu = 0;
         using (SQLiteConnection con = MyConnection.GetSQLiteConnection())
         {
             using (SQLiteCommand cmd = new SQLiteCommand())
             {
                 cmd.CommandText = sql;
                 cmd.Connection  = con;
                 if (null != sqlParam)
                 {
                     cmd.Parameters.AddRange(sqlParam);
                 }
                 if (cmd.Connection != null && cmd.Connection.State == ConnectionState.Open)
                 {
                     resu = cmd.ExecuteNonQuery();
                 }
                 else
                 {
                     resu = 0;
                 }
             }
         }
         return(resu);
     }
     catch (Exception e)
     {
         //MyLog.DalSqlite.Print.Error("Exception ExecuteNonQuery" + command.SqlText, e);
         return(0);
     }
     finally
     {
         //command.Dispose();
     }
 }