Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ADO CommandText with parameter values.

ADO CommandText with parameter values.

Scheduled Pinned Locked Moved C#
databasetutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrusM
    wrote on last edited by
    #1

    I need to get sql command text where parameters are replaced by values. I tried code below but displayed sql statement contains parameter names, not actual values. How to obtain command where parameters are replaced by their values ? Andrus.

    static IDataReader ExecuteReader(string command
    , out IDbConnection connection
    , CommandBehavior behavior
    , params IDbDataParameter[] dataParams)
    {
    connection = ...
    connection.Open();
    IDbCommand cmd = new SqlCommand(command, connection as
    SqlConnection);
    foreach (IDbDataParameter p in dataParams)
    cmd.Parameters.Add(p);
    // How to display replaced parameter values ?
    MessageBox.Show(cmd.CommandText);
    return cmd.ExecuteReader(behavior |
    CommandBehavior.CloseConnection);
    }

    Andrus

    G S 2 Replies Last reply
    0
    • A AndrusM

      I need to get sql command text where parameters are replaced by values. I tried code below but displayed sql statement contains parameter names, not actual values. How to obtain command where parameters are replaced by their values ? Andrus.

      static IDataReader ExecuteReader(string command
      , out IDbConnection connection
      , CommandBehavior behavior
      , params IDbDataParameter[] dataParams)
      {
      connection = ...
      connection.Open();
      IDbCommand cmd = new SqlCommand(command, connection as
      SqlConnection);
      foreach (IDbDataParameter p in dataParams)
      cmd.Parameters.Add(p);
      // How to display replaced parameter values ?
      MessageBox.Show(cmd.CommandText);
      return cmd.ExecuteReader(behavior |
      CommandBehavior.CloseConnection);
      }

      Andrus

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      As far as I know you can't get the actual command text that is executed programmatically. With sql server you can use sql server profiler to see the command being executed.

      Giorgi Dalakishvili #region signature my articles My blog[^] #endregion

      1 Reply Last reply
      0
      • A AndrusM

        I need to get sql command text where parameters are replaced by values. I tried code below but displayed sql statement contains parameter names, not actual values. How to obtain command where parameters are replaced by their values ? Andrus.

        static IDataReader ExecuteReader(string command
        , out IDbConnection connection
        , CommandBehavior behavior
        , params IDbDataParameter[] dataParams)
        {
        connection = ...
        connection.Open();
        IDbCommand cmd = new SqlCommand(command, connection as
        SqlConnection);
        foreach (IDbDataParameter p in dataParams)
        cmd.Parameters.Add(p);
        // How to display replaced parameter values ?
        MessageBox.Show(cmd.CommandText);
        return cmd.ExecuteReader(behavior |
        CommandBehavior.CloseConnection);
        }

        Andrus

        S Offline
        S Offline
        snifozao
        wrote on last edited by
        #3

        There is a tricky way to do it. ADO not seems to help providing the SQL query with the proper values prior running. Here my code snippet that will do the job and may be easily converted any other .NET variant. It's a simple example but may be a start. Some basic checks were done regarding quotes and backslashes but for sure this code need some care before use on production enviroment. Note: You will need to change the type SQLiteCommand/SQLiteParameter to the one that matches your ADO methods set.

        public static string getQueryFromCommand(SQLiteCommand cmd)  
        {  
            string CommandTxt = cmd.CommandText;  
          
            foreach (SQLiteParameter parms in cmd.Parameters)  
            {  
                string val = String.Empty;  
                if (parms.DbType.Equals(DbType.String) || parms.DbType.Equals(DbType.DateTime))  
                    val = "'" + Convert.ToString(parms.Value).Replace(@"\", @"\\").Replace("'", @"\'") + "'";  
                if (parms.DbType.Equals(DbType.Int16) || parms.DbType.Equals(DbType.Int32) || parms.DbType.Equals(DbType.Int64) || parms.DbType.Equals(DbType.Decimal) || parms.DbType.Equals(DbType.Double))  
                    val = Convert.ToString(parms.Value);  
                string paramname = "@" + parms.ParameterName;  
                CommandTxt = CommandTxt.Replace(paramname, val);  
            }  
            return (CommandTxt);  
        }  
        

        Bruno Ratnieks CTO Sniffer.net bruno@sniffer.net

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups