IDbCommand vs SQLiteCommand

2004-03-02
2004-03-19
  • Roger Hawkins

    Roger Hawkins - 2004-03-02

    hi,

    firstly - fantastic tool Im finding it really useful - thankyou.

    I was wondering if you could provide the same level of abstractness to IDbCommand as you currently provide with IDbConnection, IDbTransaction with the equivalent SQLLite methods/objects eg. (SQLiteConnection etc.)

    Currently I can quite happly extend and pass around all of the SQLLite objects using the standard ADO.NET IDb* interfaces. All except IDbCommand.

    For instance... to create a command and add parameters I cannot use...

    IDbCommand cmd....

    cmd.Paramters.Add(....

    Instead I have to use

    SQLliteCommand cmd
    cmd.Parameters[0]...

    Basically I always (at any class implementation level) seem to need to cast the command object to SQLLiteCommand to correctly associate parameters.

    Id like to have to avoid having to hardcode the implementation of ADO (in this case SQLlite) in my concrete classes ie. just do this in a base class.

    Any thoughts on when you might consider implenting the IDbCommand method of parameter association?

    Or is there another approach I am missing?

    cheers....

     
    • Tim McDaniel

      Tim McDaniel - 2004-03-02

      Can you do the following...

      IDbCommand cmd...
      cmd.Parameters[0] = ...

      The SQliteParameterCollection isn't designed to be Added to, it automatically contains the correct number of parameters based on the number of '?'s in your SQL statement.

       
      • Nobody/Anonymous

        thx Tim,

        tried it and I get the error below. When I debug and look at the SQlliteParameters in the cmd object they are as I have assigned them. For instance the 'note' field below has a value in the parameter.
        Using SQLliteCommand inline I do not have this problem.

        "
        Object reference not set to an instance of an object.
        Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

        Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

        Source Error:

        Line 257: cmd.Parameters[7]=(entity.Note.IsNull)?null:entity.Note.Value;
        Line 258:
        Line 259: cmd.ExecuteNonQuery();
        "

         
      • Nobody/Anonymous

        thx Tim,

        tried it and I get the error below. When I debug and look at the SQlliteParameters in the cmd object they are as I have assigned them. For instance the 'note' field below has a value in the parameter.
        Using SQLliteCommand inline I do not have this problem.

        "
        Object reference not set to an instance of an object.
        Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

        Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

        Source Error:

        Line 257: cmd.Parameters[7]=(entity.Note.IsNull)?null:entity.Note.Value;
        Line 258:
        Line 259: cmd.ExecuteNonQuery();
        "

         
    • Nobody/Anonymous

      thx Tim,

      tried it and I get the error below. When I debug and look at the SQlliteParameters in the cmd object they are as I have assigned them. For instance the 'note' field below has a value in the parameter.
      Using SQLliteCommand inline I do not have this problem.

      "
      Object reference not set to an instance of an object.
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

      Source Error:

      Line 257:                cmd.Parameters[7]=(entity.Note.IsNull)?null:entity.Note.Value;
      Line 258:
      Line 259:                cmd.ExecuteNonQuery();
      "

       
    • Tim McDaniel

      Tim McDaniel - 2004-03-08

      I'm sorry, I forgot the "Value" part:

      IDbCommand cmd
      cmd.Parameters[0].Value = ...

      See if that makes any difference.

       
      • Nobody/Anonymous

        thx,

        however the cmd.Parameters[0] appears not to have a '.Value' method.

        I get the following compile error
        'object' does not contain a definition for 'Value'

        It seems that the way the IDbCommand adds paramters is by dynamically adding using

        IDbcommand cmd
        cmd.Parameters.Add( ...

         
    • Tim McDaniel

      Tim McDaniel - 2004-03-08

      Try casting to IDataParameter or IDbDataParameter since the [0] indexer is really just returning an Object, which of course does not have a Value property ...

      ((IDataParameter) cmd.Parameters[0]).Value = ...

       
      • Nobody/Anonymous

        thx Tim,

        this worked fine!

        thx also for putting this approach into your latest release...

        cheers...

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks