1

I'm trying to write function like this below to Insert records to table in my data base. I'm using SQLite for this. I have a problem in this 3 lines:

 insertSQL.Parameters.Add(newUser.Username);
 insertSQL.Parameters.Add(newUser.Password);            
 insertSQL.Parameters.Add(newUser.Email);

The content of errors:

Error CS1061 'SQLiteCommand' does not contain a definition for >'Parameters' and no accessible extension method 'Parameters' accepting a >first argument of type 'SQLiteCommand' could be found (are you missing a >using directive or an assembly reference?)

I have usings like "using SQLite; using static SQLite.SQLiteCommand;" The Microsoft docs and many guides are including property "Parameters" and similar code.

     public static SQLiteConnection non_async_db;     

     public void AddUser(User newUser, string login, string password, string email)
        {

            SQLiteCommand insertSQL = new SQLiteCommand(non_async_db);
            insertSQL.CommandText = "INSERT INTO User(Username, Password, Email) VALUES(" + login + ", " + password + ", " + email + ")";
            insertSQL.Parameters.Add(newUser.Username);
            insertSQL.Parameters.Add(newUser.Password);
            insertSQL.Parameters.Add(newUser.Email);
        }

I have no idea where the problem is. Maybe this way is outdated or simply wrong?

1

1 Answer 1

0

It appears it's because you're using the ORM version of SQLite, and those classes are from the ADO version. If you require the ORM version, there may be other ways to achieve this. As an example, see Xamarin Android C# SQLite Parameter Query:

You may want to consider using the ADO version of SQLite, which has support for those classes you've cited. Here's an example for what you're trying to achieve (notice that the "using" is different than yours).:

using System.Data;
using Mono.Data.Sqlite; // Requires reference to:  C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\Mono.Data.Sqlite.dll

//...

var cmd = new SqliteCommand(Conn) {
    CommandText = "INSERT INTO MyTable (Setting, Value) VALUES (@SETTING, @VALUE)",
    CommandType = CommandType.Text
};
cmd.Parameters.Add(new SqliteParameter("@SETTING", "My Setting"));    
cmd.Parameters.Add(new SqliteParameter("@VALUE", "My Value"));

var nRowsProcessed = cmd.ExecuteNonQuery();
Log.Info("MyApp", $"Rows Processed: {nRowsProcessed}");

BTW, one way you can differentiate ORM vs ADO in examples is that the ORM classes generally have a capitalized "SQ" in the type names, whereas ADO uses "Sq" (e.g., "SQLiteConnection" vs "SqLiteConnection")

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

The information about ADO/ORM is really helpful! The code example not so much but I checked the ADO/ORM differences and figure it out on my own. Thanks a lot!
You're welcome, glad it helped. Hopefully other folks who come across this post find it helpful as well. BTW, welcome to StackOverflow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.