in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

DbProviderFactory, add parameters to DbCommand

Last post 01-26-2009 1:54 AM by jojky. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 01-25-2009 7:10 AM

    • jojky
    • Not Ranked
    • Joined on 01-25-2009
    • Posts 3

    DbProviderFactory, add parameters to DbCommand

    Hi,

    I use DbProviderFactory with System.Data.SQLite 1.0.60.0 in code like this:

    DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(ConfigurationContext.ProviderName);

    DbConnection connection = dbProviderFactory.CreateConnection();
    connection.ConnectionString = ConfigurationContext.ConnectionString;
    connection.Open();

    DbCommand command = dbProviderFactory.CreateCommand();

    command.Connection = connection;
    command.CommandText = "query with parameter";
    command.Parameters.Add(someInteger); << here exception

    System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Data.SQLite.SQLiteParameter'.

     It seems, that last line call Add(SQLiteParameter)  instead of  Add(Object).

     Something wrong in my code?

     Thanks

     

     


    Filed under:
  • 01-25-2009 8:11 AM In reply to

    Re: DbProviderFactory, add parameters to DbCommand

     Hi,

     You need to create parameters of the correct type

    A quick example

        int afsluiting = 7;

        cmd.CommandText = "SELECT * FROM Voorraad WHERE Afsluiting=@Afsluiting";

        cmd.Parameters.Add( new SQLiteParameter{ ParameterName="@Afsluiting" , Value = afsluiting });

     

     

     

    Luc Wuyts
    http://www.a-d-e.net
  • 01-25-2009 3:30 PM In reply to

    • jojky
    • Not Ranked
    • Joined on 01-25-2009
    • Posts 3

    Re: DbProviderFactory, add parameters to DbCommand

    Thanks for your answer, but you dont understand that I want. I want to create all SQLite objects by DbProviderFactory and work with them by abstract classes like DbCommand, DbConnection, etc.

     

  • 01-25-2009 4:33 PM In reply to

    Re: DbProviderFactory, add parameters to DbCommand

    Use either the command object's or the factory's "CreateParameter" method.  Not sure if the DbCommand abstract class includes that or not. If not, you'll need to use the factory's version.

  • 01-26-2009 1:54 AM In reply to

    • jojky
    • Not Ranked
    • Joined on 01-25-2009
    • Posts 3

    Re: DbProviderFactory, add parameters to DbCommand

     Thanks! I overlooked CreateParameter method of DbProviderFactory.  

Page 1 of 1 (5 items)
Powered by Community Server (Commercial Edition), by Telligent Systems