Exemplo n.º 1
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
     {
     }
 }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
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();
     }
 }