lundi 21 novembre 2022

Having trouble creating this C# method that uses generics to insert into an sqlite table

I am getting a no mapping exists error when I try running this code. Not sure what I need to do here.

public void Create<T>(T obj) where T : IClassModel<T>
        {
            Type type = obj.GetType();
            // get the property names and values of obj
            // PropertyInfo
            PropertyInfo[] properties = type.GetProperties();

            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo property = properties[i];
                Console.WriteLine(property);
            }
            
            
            
            // build query as a string (StringBuilder)
            // 1. If you aren't on the last value or column name, you need a comma



            var command = _connection.CreateCommand();

            // use the string you built here
            command.CommandText = $"INSERT INTO {typeof(T).Name} VALUES ($Id, $FirstName, $Surname)";

            command.Parameters.AddWithValue("$Id", obj).SqliteType = SqliteType.Integer;
            command.Parameters.AddWithValue("$FirstName", obj).SqliteType = SqliteType.Text;
            command.Parameters.AddWithValue("$Surname", obj).SqliteType = SqliteType.Text;


            command.ExecuteNonQuery();

That is the create method that I am using. Please, any help is appreciated.





Aucun commentaire:

Enregistrer un commentaire