SQLite Help class Sqlitehelper implementation of SQLite data deletion and modification

Source: Internet
Author: User
Tags sqlite


public class SQLiteHelper
    {

        public const string sConn = "Data Source =" + @ "path";

        /// <summary>
        /// Query, return object, get the value of the first row and first column of the query result, or NUll if there is no first row and first column
        /// </ summary>
        /// <param name = "sql"> Query statement </ param>
        /// <param name = "parameters"> Optional parameters </ param>
        /// <returns> </ returns>
        public static object ExecuteScalar (string sql, params SQLiteParameter [] parameters)
        {
            return ExecuteScalar (sql, CommandType.Text, parameters);

        }

        /// <summary>
        /// Query, return object, execute SQl statement, get the first row and first column of the query result, if there is no first row, the first column returns null
        /// </ summary>
        /// <param name = "sql"> Query statement </ param>
        /// <param name = "type"> How to interpret the command string </ param>
        /// <param name = "parameters"> optional parameters </ param>
        /// <returns> </ returns>
        public static object ExecuteScalar (string sql, CommandType type, params SQLiteParameter [] parameters)
        {
            using (SQLiteConnection conn = new SQLiteConnection (sConn))
            {
                conn.Open ();
                SQLiteCommand cmd = new SQLiteCommand (sql, conn);

                cmd.CommandType = type;
                cmd.Parameters.AddRange (parameters);
                object obj = cmd.ExecuteScalar ();
                cmd.Parameters.Clear ();
                return obj;
            }
        }

      /// <summary>
      /// execute add, delete, change
      /// </ summary>
      /// <param name = "sql"> sql statement </ param>
      /// <param name = "parameters"> Optional parameters </ param>
      /// <returns> return int to get the number of affected rows </ returns>
        public static int ExecuteNonQuery (string sql, params SQLiteParameter [] parameters)
        {
            return ExecuteNonQuery (sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// execute add, delete, change
        /// </ summary>
        /// <param name = "sql"> sql statement </ param>
        /// <param name = "type"> How to interpret the command string </ param>
        /// <param name = "parameters"> Optional parameters </ param>
        /// <returns> </ returns>
        public static int ExecuteNonQuery (string sql, CommandType type, params SQLiteParameter [] parameters)
        {
            using (SQLiteConnection conn = new SQLiteConnection (sConn))
            {
                conn.Open ();
                SQLiteCommand cmd = new SQLiteCommand (sql, conn);
                cmd.CommandType = type;
                cmd.Parameters.AddRange (parameters);
                int num = cmd.ExecuteNonQuery ();
                cmd.Parameters.Clear ();
                return num;
            }
        }


        /// <summary>
        /// disconnected query, query multiple columns
        /// </ summary>
        /// <param name = "sql"> sql statement </ param>
        /// <param name = "parameters"> Optional parameters </ param>
        /// <returns> Return DataTable type </ returns>
        public static DataTable ExecuteTable (string sql, params SQLiteParameter [] parameters)
        {
            return ExecuteTable (sql, CommandType.Text, parameters);
        }

        /// <summary>
        /// disconnected query, query results can be multiple columns
        /// </ summary>
        /// <param name = "sql"> sql statement </ param>
        /// <param name = "type"> How to interpret the command string </ param>
        /// <param name = "parameters"> Optional parameters </ param>
        /// <returns> </ returns>
        public static DataTable ExecuteTable (string sql, CommandType type, params SQLiteParameter [] parameters)
        {
            SQLiteConnection conn = new SQLiteConnection (sConn);
            conn.Open ();
            SQLiteCommand cmd = new SQLiteCommand (sql, conn);
            cmd.CommandType = type;
            cmd.Parameters.AddRange (parameters);
            SQLiteDataAdapter sda = new SQLiteDataAdapter (cmd);
            DataSet ds = new DataSet ();
            sda.Fill (ds);
            conn.Close ();
            DataTable dt = ds.Tables [0];
            return dt;
        }
    } 





SQLite Help class Sqlitehelper implementation of SQLite data deletion and modification


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.