xamarinsqlite.net

SQLite Command?


I am trying to make a login page with xamarin

public bool Login(string Username, string Password)
    {
        Users _user = new Users();
        string mySelectQuery = "SELECT Name, Password FROM Users WHERE Name='" + Username + "' AND Password='" + Password + "'";

        var user = new SQLiteCommand(mySelectQuery);

        if (user != null)
        {
            return true;
        }

        return false;
    }

At SQLiteCommand part it gives an error. The error is CS1729 Does not contain a constructor that takes 1 arguments I checked out on the internet but I didn't get it I am new at this.


Solution

  • You use SQLiteCommand class, and contain one arguments, it should be SQLiteConnection, here is sample that about this:

    public bool Login(string Username, string Password)
        {
    
            string dbName = "Data Source=searchindex.db";
            string mySelectQuery = "SELECT Count(*) FROM Users WHERE Name='" + Username + "' AND Password='" + Password + "'";
    
            SQLiteConnection con = new SQLiteConnection(dbName);
            SQLiteCommand cmd = new SQLiteCommand(con);
            cmd.CommandText = mySelectQuery;
    
            var count = cmd.ExecuteScalar<int>();
            if (count>0)
            {
                return true;
            }
    
            return false;
        }