public class SQLiteHelper
{
public const string sConn = "Data Source =" + @ "path";
/// <summary>
/// Query, return object, get the value of the first row and first column of the query result, or NUll if there is no first row and first column
/// </ summary>
/// <param name = "sql"> Query statement </ param>
/// <param name = "parameters"> Optional parameters </ param>
/// <returns> </ returns>
public static object ExecuteScalar (string sql, params SQLiteParameter [] parameters)
{
return ExecuteScalar (sql, CommandType.Text, parameters);
}
/// <summary>
/// Query, return object, execute SQl statement, get the first row and first column of the query result, if there is no first row, the first column returns null
/// </ summary>
/// <param name = "sql"> Query statement </ param>
/// <param name = "type"> How to interpret the command string </ param>
/// <param name = "parameters"> optional parameters </ param>
/// <returns> </ returns>
public static object ExecuteScalar (string sql, CommandType type, params SQLiteParameter [] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection (sConn))
{
conn.Open ();
SQLiteCommand cmd = new SQLiteCommand (sql, conn);
cmd.CommandType = type;
cmd.Parameters.AddRange (parameters);
object obj = cmd.ExecuteScalar ();
cmd.Parameters.Clear ();
return obj;
}
}
/// <summary>
/// execute add, delete, change
/// </ summary>
/// <param name = "sql"> sql statement </ param>
/// <param name = "parameters"> Optional parameters </ param>
/// <returns> return int to get the number of affected rows </ returns>
public static int ExecuteNonQuery (string sql, params SQLiteParameter [] parameters)
{
return ExecuteNonQuery (sql, CommandType.Text, parameters);
}
/// <summary>
/// execute add, delete, change
/// </ summary>
/// <param name = "sql"> sql statement </ param>
/// <param name = "type"> How to interpret the command string </ param>
/// <param name = "parameters"> Optional parameters </ param>
/// <returns> </ returns>
public static int ExecuteNonQuery (string sql, CommandType type, params SQLiteParameter [] parameters)
{
using (SQLiteConnection conn = new SQLiteConnection (sConn))
{
conn.Open ();
SQLiteCommand cmd = new SQLiteCommand (sql, conn);
cmd.CommandType = type;
cmd.Parameters.AddRange (parameters);
int num = cmd.ExecuteNonQuery ();
cmd.Parameters.Clear ();
return num;
}
}
/// <summary>
/// disconnected query, query multiple columns
/// </ summary>
/// <param name = "sql"> sql statement </ param>
/// <param name = "parameters"> Optional parameters </ param>
/// <returns> Return DataTable type </ returns>
public static DataTable ExecuteTable (string sql, params SQLiteParameter [] parameters)
{
return ExecuteTable (sql, CommandType.Text, parameters);
}
/// <summary>
/// disconnected query, query results can be multiple columns
/// </ summary>
/// <param name = "sql"> sql statement </ param>
/// <param name = "type"> How to interpret the command string </ param>
/// <param name = "parameters"> Optional parameters </ param>
/// <returns> </ returns>
public static DataTable ExecuteTable (string sql, CommandType type, params SQLiteParameter [] parameters)
{
SQLiteConnection conn = new SQLiteConnection (sConn);
conn.Open ();
SQLiteCommand cmd = new SQLiteCommand (sql, conn);
cmd.CommandType = type;
cmd.Parameters.AddRange (parameters);
SQLiteDataAdapter sda = new SQLiteDataAdapter (cmd);
DataSet ds = new DataSet ();
sda.Fill (ds);
conn.Close ();
DataTable dt = ds.Tables [0];
return dt;
}
}
SQLite Help class Sqlitehelper implementation of SQLite data deletion and modification