关闭

SQLiteDatabase.cs

185人阅读 评论(0) 收藏 举报
分类:
using System;
using System.Data;// DataTable
using System.Data.SQLite;// SQLiteConnection,SQLiteCommand,SQLiteDataReader

/// <summary>
/// SQLiteDatabase 的摘要说明
/// </summary>
public class SQLiteDatabase
{
    private string _dataSource = null;

    /// <summary>
    /// 默认构造函数
    /// </summary>
    public SQLiteDatabase()
    {
        _dataSource = "Data Source=recipes.s3db";
    }

    /// <summary>
    /// 可指定数据源文件的构造函数
    /// </summary>
    /// <param name="path">数据源文件的实际文件地址</param>
    public SQLiteDatabase(string path)
    {
        _dataSource = string.Format("Data Source={0}", path);
    }

    /// <summary>
    /// 数据资源属性
    /// </summary>
    public string DataSource
    {
        get { return _dataSource; }
        set { _dataSource = string.Format("Data Source={0}", value); }
    }

    /// <summary>
    /// 获取查询后的数据表
    /// </summary>
    /// <param name="sql">要执行的SQL语句</param>
    /// <returns>返回表格</returns>
    public DataTable GetDataTable(string sql)
    {
        DataTable dt = new DataTable();
        try
        {
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = sql;
            SQLiteDataReader dr = cmd.ExecuteReader();
            dt.Load(dr);
            dr.Close();
            conn.Close();
        }
        catch
        {
        }
        return dt;
    }

    /// <summary>
    /// 执行SQL语句
    /// </summary>
    /// <param name="sql">要执行的SQL语句</param>
    /// <returns>返回更新行数</returns>
    public int Perform(string sql)
    {
        int result = -1;
        try
        {
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = sql;
            result = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch
        {
        }
        return result;
    }

    /// <summary>
    /// 执行SQL语句
    /// </summary>
    /// <param name="sql">要执行的SQL语句</param>
    /// <param name="err">输出错误信息</param>
    /// <returns>返回执行结果</returns>
    public bool Perform(string sql, out string err)
    {
        bool result = false;
        err = null;
        try
        {
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            conn.Close();
            result = true;
        }
        catch (Exception ex)
        {
            err = ex.Message;
        }
        return result;
    }

    /// <summary>
    /// 检索单项
    /// </summary>
    /// <param name="sql">要执行的SQL语句</param>
    /// <returns>返回字符串</returns>
    public string Query(string sql)
    {
        string result = null;
        try
        {
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            conn.Open();
            SQLiteCommand cmd = new SQLiteCommand(conn);
            cmd.CommandText = sql;
            result = cmd.ExecuteScalar().ToString();
            conn.Close();
        }
        catch// (Exception ex)
        {
            //result = ex.ToString();
        }
        return result;
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:79609次
    • 积分:1816
    • 等级:
    • 排名:千里之外
    • 原创:102篇
    • 转载:2篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论