自己封裝的ASP.NET的SQLITE資料庫的操作類

來源:互聯網
上載者:User
代碼

/*
 * 作者: 牛腩
 * 建立時間: 2010-1-20 10:43:14
 * Email: 164423073@qq.com
 * 說明: 對SQLite資料庫的增刪查改操作的封裝類
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Web.Configuration;
using System.Web;

namespace Niunan.Tg029.Hr.Utility
{
    public class SQLiteHelper
    {
        private SQLiteConnection conn = null;
        private SQLiteCommand cmd = null;
        private SQLiteDataReader sdr = null;

        public SQLiteHelper()
        {
           //string connStr = WebConfigurationManager.ConnectionStrings["connStr"].ToString();
            string connStr = "Data Source=" + HttpContext.Current.Server.MapPath("~/test/test.sqlite") ;
            conn = new SQLiteConnection(connStr);
        }

        /// <summary>建立Command對象
        /// 
        /// </summary>
        /// <param name="sql">SQL語句</param>
        public void CreateCommand(string sql) {
            conn.Open();
            cmd = new SQLiteCommand(sql, conn);        
        }

        /// <summary>添加參數
        /// 
        /// </summary>
        /// <param name="paramName">參數名稱</param>
        /// <param name="value">值</param>
        public void AddParameter(string paramName,object value) {
            cmd.Parameters.Add(new SQLiteParameter(paramName,value));
        }

        /// <summary>執行不帶參數的增刪改SQL語句
        ///  
        /// </summary>
        /// <param name="cmdText">增刪改SQL語句</param>
        /// <param name="ct">命令類型</param>
        /// <returns></returns>
        public bool ExecuteNonQuery()
        {
            int res;
            try
            {
                res = cmd.ExecuteNonQuery();
                if (res > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return false;
        }

        /// <summary>執行查詢SQL語句
        ///  
        /// </summary>
        /// <param name="cmdText">查詢SQL語句</param>
        /// <returns></returns>
        public DataTable ExecuteQuery()
        {
            DataTable dt = new DataTable();
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }

        /// <summary>返回查詢SQL語句查詢出的結果的第一行第一列的值
        /// 
        /// </summary>
        /// <returns></returns>
        public string ExecuteScalar()
        {
            string res = "";
            try
            {
                object obj = cmd.ExecuteScalar();
                if (obj != null)
                {
                    res = obj.ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return res;
        }

    }

使用樣本看附件吧,蠻簡單的一個封裝,特此記錄,以備不時之需!!!
附件下載:http://niunan.net/download/sqlitetest.7z

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.