Using the SQLite database helper

Source: Internet
Author: User
Tags sqlite sqlite database

public class SQLiteHelper
    {
        // public static String ConnectionString = "Data Source = H: /SQLite/DB/minWeiGanBu.db; Pooling = true; FailIfMissing = false";
        // public static String ConnectionString = "Data Source = H: /SQLite/DB/aid/minWeiGanBu.db; Pooling = true; FailIfMissing = false";
        static string ConnectionString = ConfigurationManager.ConnectionStrings ["ConnStr"]. ConnectionString.ToString (). Replace ("| path |", Tools.binPath + @ "\ DB");
 
        private static void PrepareCommand (SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params SQLiteParameter [] p)
        {
            if (conn.State! = ConnectionState.Open)
                conn.Open ();
            cmd.Parameters.Clear ();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = CommandType.Text;
            cmd.CommandTimeout = 30;
            if (p! = null)
            {
                foreach (object parm in p)
                    //cmd.Parameters.AddWithValue(string.Empty, parm);
                    cmd.Parameters.Add (parm);
            }
        }
        public static DataSet ExecuteQuery (string cmdText, params SQLiteParameter [] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection (ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand ())
                {
                    DataSet ds = new DataSet ();
                    PrepareCommand (command, conn, cmdText, p);
                    SQLiteDataAdapter da = new SQLiteDataAdapter (command);
                    da.Fill (ds);
                    return ds;
                }
            }
        }
        public static int ExecuteNonQuery (string cmdText, params SQLiteParameter [] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection (ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand ())
                {
                    PrepareCommand (command, conn, cmdText, p);
                    return command.ExecuteNonQuery ();
                }
            }
        }
 
        public static SQLiteDataReader ExecuteReader (string cmdText, params SQLiteParameter [] p)
        {
            // using (SQLiteConnection conn = new SQLiteConnection (ConnectionString))
            // {
               SQLiteConnection conn = new SQLiteConnection (ConnectionString);
                using (SQLiteCommand command = new SQLiteCommand ())
                {
                    PrepareCommand (command, conn, cmdText, p);
                    return command.ExecuteReader (CommandBehavior.CloseConnection);
                }
            //}
        }
 
        public static object ExecuteScalar (string cmdText, params SQLiteParameter [] p)
        {
            using (SQLiteConnection conn = new SQLiteConnection (ConnectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand ())
                {
                    PrepareCommand (command, conn, cmdText, p);
                    return command.ExecuteScalar ();
                }
            }
        }
    }
Use helper of sqlite database

label:

Original address: http://www.cnblogs.com/wygm/p/5668166.html

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.