SQLite执行多个参数(SQLiteParameter)

首次发布:2017-05-25 23:45
2022-02-15 更新
以下是核心代码
public static string ConnectionString = "Data Source= data.db;Pooling=true;FailIfMissing=false; ";

public static int ExecuteSql(string SQLString, params SQLiteParameter[] p)
{//原创来自 http://www.luofenming.com
    using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
    {
        SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
        cmd.Parameters.AddRange(p);
        try
        {
            connection.Open();
            int rows = cmd.ExecuteNonQuery();
            return rows;
        }
        catch (Exception ex)
        {
            return 0;
        }
        finally
        {
            cmd.Dispose();
            connection.Close();
        }
    }
}

/// <summary>
/// 增加一条数据
/// </summary>
public static void Add(string OSVersion, string Browser, string IPAddress, string upDate)
{
    StringBuilder strSql = new StringBuilder();
    strSql.Append("insert into UserInfo(");
    strSql.Append("OSVersion,Browser,IPAddress,upDateTime)");
    strSql.Append(" values (");
    strSql.Append("@OSVersion,@Browser,@IPAddress,@upDateTime)");
    SQLiteParameter[] parameters = {
            new SQLiteParameter("@OSVersion", OSVersion),
            new SQLiteParameter("@Browser", Browser),
            new SQLiteParameter("@IPAddress", IPAddress),
            new SQLiteParameter("@upDateTime", upDate)};
    ExecuteSql(strSql.ToString(), parameters);
}