Answers for "sqlite c#"

C#
1

sqlite connection c#

//Download "System.Data.Sqlite" from "Manage Nuget Packages" Section
using System.Data.SQLite;
SQLiteConnection.CreateFile("TestDB.db3") //Create a Database
SQLiteConnection conn = new SQLiteConnection(@"data source = TestDB.db3"); //Establish a connection with our created DB
conn.Open();
SQLiteCommand cmd = new SQLiteCommand("create table table1 (username TEXT, password TEXT)", conn);
cmd.ExecuteNonQuery();
Console.WriteLine("Enter the username");
string UserName = Console.ReadLine();
Console.WriteLine("Enter Password");
string PassWord = Console.ReadLine();
SQLiteCommand cmd2 = new SQLiteCommand("insert into table1 (username, password) values ('"+Username+"', '"+PassWord+"')", conn);
cmd2.ExecuteNonQuery();
Posted by: Guest on April-28-2020
0

c# sqlite query

public List<string> GetUsers()
{
	// Init return list
    List<string> users = new List<string>();

	// Set the path to sqlite file
    string solutionPath = Environment.CurrentDirectory;
	string dbPath = Path.Combine(solutionPath, "db.sqlite3");    

    SQLiteConnection connection = new SQLiteConnection(solutionPath);
    connect.Open();

	// Construct sql query
    SQLiteCommand command = connection.CreateCommand();
    command.CommandText = @"SELECT username FROM tableUsers";
    command.CommandType = CommandType.Text;

    SQLiteDataReader reader = command.ExecuteReader();

	// reader is treated like a array so call 
    while (reader.Read())
    	users.Add(Convert.ToString(reader["username"]));

	// Clean up no longer needed connection
	connection.Close();

    return users;
}
Posted by: Guest on June-30-2021

C# Answers by Framework

Browse Popular Code Answers by Language