C#链接SQLite的帮助类连接增删改查用SQLiteHelper

public static class SQLiteHelper
{
///
/// ConnectionString样例(Sqlite文件放dbug目录下):Data Source=Test.db;Pooling=true;FailIfMissing=false
///
public static string ConnectionString = “Data Source=Production.db;Pooling=true;FailIfMissing=false”;
///
/// 参数设置
///
///
///
///
///
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string sqlStr, params SQLiteParameter[] p)
{
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Parameters.Clear();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 30;
if (p != null)
{
foreach (SQLiteParameter parm in p)
{
cmd.Parameters.AddWithValue(parm.ParameterName, parm.Value);
}
}
}
catch (Exception ex)
{
return;
}
}
///
/// 查询dataSet
///
///
///
///
public static DataSet ExecuteQuery(string sqlStr, params SQLiteParameter[] p)
{
using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
{
using (SQLiteCommand command = new SQLiteCommand())
{

                DataSet ds = new DataSet();
                try
                {
                    PrepareCommand(command, conn, sqlStr, p);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(command);
                    da.Fill(ds);
                    return ds;
                }
                catch (Exception ex)
                {
                    return ds;
                }
            }
        }
    }
    /// <summary>
    /// &#x4E8B;&#x52A1;&#x5904;&#x7406;
    /// </summary>
    /// <param name="SQL">
    /// <param name="p">
    public static bool ExecSQL(string sqlStr, params SQLiteParameter[] p)
    {
        bool result = true;
        using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
        {
            using (SQLiteCommand command = new SQLiteCommand())
            {
                PrepareCommand(command, conn, sqlStr, p);
                SQLiteTransaction transaction = conn.BeginTransaction();

                try
                {
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    result = false;
                }
            }
        }
        return result;
    }

    public static int ExecuteNonQuery(string sqlStr, params SQLiteParameter[] p)
    {
        using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
        {
            using (SQLiteCommand command = new SQLiteCommand())
            {
                try
                {
                    PrepareCommand(command, conn, sqlStr, p);
                    return command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    return -99;
                }
            }
        }
    }

    public static object ExecuteScalar(string sqlStr, params SQLiteParameter[] p)
    {
        using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
        {
            using (SQLiteCommand command = new SQLiteCommand())
            {
                try
                {
                    PrepareCommand(command, conn, sqlStr, p);
                    return command.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    return -99;
                }
            }
        }
    }

以上就是帮助类。
用法:
1.添加一个按钮,查询
添加一个显示控件dataGridview

C#链接SQLite的帮助类连接增删改查用SQLiteHelper
C#链接SQLite的帮助类连接增删改查用SQLiteHelper
查询按钮代码
string strSql = “select * from AddressSteing”;
DataSet ds = SQLiteHelper.ExecuteQuery(strSql);
if (ds != null && ds.Tables.Count > 0)
{
dataGridView1.DataSource = ds.Tables[0];
}

输入查询命令我的是在addresssteing下面。
在用帮助类的查询获取数据显示在datagridview里。

Original: https://blog.csdn.net/weixin_43867834/article/details/124563992
Author: 我这人很简单
Title: C#链接SQLite的帮助类连接增删改查用SQLiteHelper

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816121/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球