使用C#操作SQLite数据库的技巧和方法

功能

  1. 对SQLite数据库进行连接
  2. 对数据库中的数据进行增删改查并更新到DataGridView控件中

一、创建数据库连接

#region 变量
private string dbUrl = null;                //数据库文件的地址
private SQLiteConnection connection;        //数据库连接对象
#endregion

#region 数据库工具类相关的函数
/******************************************************************************************************************
 * name        : public DBHelper(string DBName)
 * Function    : 指定数据库文件的地址
 * Parameters  : DBName :数据库的名称
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       :
 * Date        : 2021-3-21
******************************************************************************************************************/
public DBHelper(string DBName)
{
    dbUrl = Application.StartupPath + "\\" + DBName + ".db";
}

/******************************************************************************************************************
 * name        : public SQLiteConnection Connection
 * Function    : 创建并返回数据的连接
 * Parameters  : DBName :数据库的名称
 * Returns     : NULL
 * Note        :
 * Author      : 那些你很冒险的梦
 * Check       :
 * Date        : 2021-3-21
******************************************************************************************************************/
//创建连接
public SQLiteConnection Connection
{
    get
    {
        //判断数据库文件是否存在
        if (!File.Exists(dbUrl))
        {
            SQLiteConnection.CreateFile(dbUrl);
        }
        //判断连接是否存在
        if (connection == null)
        {
            connection = new SQLiteConnection("Data Source=" + dbUrl + ";Version=3;");
        }
        return connection;
    }
    set
    {
        connection = value;
    }
}

/******************************************************************************************************************
 * name        : public void OpenConnection()
 * Function    : 打开数据库
 * Parameters  : void
 * Returns     : NULL
 * Author      :那些你很冒险的梦
 * Check       :
 * Date        : 2021-3-21
******************************************************************************************************************/
public void OpenConnection()
{
    //如果连接关闭
    if (connection.State == System.Data.ConnectionState.Closed)
    {
        connection.Open();
    }
    //如果连接损坏
    if (connection.State == System.Data.ConnectionState.Broken)
    {
        connection.Close();
        connection.Open();
    }
}

/******************************************************************************************************************
 * name        : public void OpenConnection()
 * Function    : 关闭数据库连接
 * Parameters  : void
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       :
 * Date        : 2021-3-21
******************************************************************************************************************/
public void CloseConnection()
{
    //判断连接是否打开或者损坏
    if (connection.State == System.Data.ConnectionState.Open || connection.State == System.Data.ConnectionState.Broken)
    {
        connection.Close();
    }
}

二、对数据库中的数据实现增删改查

#region 对数据库中数据的增上改查
/******************************************************************************************************************
 * name        : public void getMessage(string sql,DataGridView dataGridView)
 * Function    : 从数据库中获取信息并填充到DataGridView控件中
 * Parameters  : sql:订制SQL语句    dataGridView:指定数据填充的控件的名称
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       :
 * Date        : 2021-3-21
******************************************************************************************************************/
public void getMessage(string sql, DataGridView dataGridView)
{
    DataSet ds = new DataSet();
    try
    {
        SQLiteDataAdapter adapter = new SQLiteDataAdapter(string.Format(@sql), Connection);
        OpenConnection();
        adapter.Fill(ds, "mark");
        dataGridView.DataSource = ds.Tables["mark"];
    }
    catch (Exception ex)
    {
        MessageTip.ShowError("数据库操作错误");
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        CloseConnection();
    }
}
/******************************************************************************************************************
 * name        : public void addMessage(string sql)
 * Function    : 向数据库中插入数据
 * Parameters  : sql:订制SQL语句
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       : 
 * Date        : 2021-3-21
******************************************************************************************************************/
public void addMessage(string sql1, string sql2,DataGridView dataGridView)
{
    try
    {
        string sql = string.Format(@sql1);
        //创建连接
        SQLiteCommand command = new SQLiteCommand(sql1, Connection);
        //打开连接
        OpenConnection();
        //执行SQL语句
        int result = command.ExecuteNonQuery();
        //判断是否更新成功
        if (result == 0)
        {
            MessageTip.ShowWarning("插入数据失败");
            //MessageBox.Show("插入数据失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            //重新向datagridview中填充数据
            //UserDev.userInstance.showUserList();
            getMessage(sql2, dataGridView);
            MessageTip.ShowOk("插入数据成功");
            //MessageBox.Show("插入数据成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
    catch (Exception ex)
    {
        MessageTip.ShowError("数据库操作错误");
        MessageBox.Show(ex.ToString());
        //MessageBox.Show("数据库操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        CloseConnection();
    }
}

/******************************************************************************************************************
 * name        : public void deleteMessage(string sql)
 * Function    : 从数据库中删除数据
 * Parameters  : sql:订制SQL语句
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       : 
 * Date        : 2021-3-21
******************************************************************************************************************/
public void deleteMessage(string sql1, string sql2, DataGridView dataGridView)
{
    try
    {
        string sql = string.Format(@sql1);
        SQLiteCommand command = new SQLiteCommand(sql, Connection);
        OpenConnection();
        int result = command.ExecuteNonQuery();
        if (result != 0)
        {
            getMessage(sql2, dataGridView);
            MessageTip.ShowOk("删除成功");
            //MessageBox.Show("删除成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageTip.ShowWarning("删除失败");
            //MessageBox.Show("删除失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception ex)
    {
        MessageTip.ShowError("数据库操作错误");
        MessageBox.Show(ex.ToString());
        //MessageBox.Show("数据库操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
        CloseConnection();
    }
}

/******************************************************************************************************************
 * name        : public void updateMessage(string sql)
 * Function    : 更新数据库中的数据
 * Parameters  : sql:订制SQL语句
 * Returns     : NULL
 * Author      : 那些你很冒险的梦
 * Check       : 
 * Date        : 2021-3-21
******************************************************************************************************************/
public void updateMessage(string sql1, string sql2, DataGridView dataGridView)
{
    try
    {
        string sql = string.Format(@sql1);
        SQLiteCommand command = new SQLiteCommand(sql, Connection);
        OpenConnection();
        int result = command.ExecuteNonQuery();
        if (result == 0)
        {
            MessageTip.ShowWarning("修改数据失败");
        }
        else
        {
            //重新向datagridview中填充数据 UserDev.userInstance.showUserList();
            getMessage(sql2, dataGridView);
            MessageTip.ShowOk("插入数据成功");
        }
    }
    catch (Exception ex)
    {
        MessageTip.ShowError("数据库操作错误");
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        CloseConnection();
    }
}
#endregion

欢迎一起讨论技术问题,求关注!

物联沃分享整理
物联沃-IOTWORD物联网 » 使用C#操作SQLite数据库的技巧和方法

发表评论