Results 1 to 3 of 3

Thread: [RESOLVED] Parameterized query

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Resolved [RESOLVED] Parameterized query

    I have this code I would like to change to a parameterized query.
    Code:
            public String GetLoginRecordID(String User, String Password, String result,String connString)
            {
                String strCmd = "SELECT RecordID FROM SystemLogins WHERE (UserID = '" + User + "')" + 
                        " AND (UserPassword = '" + Password + "') AND (Result = 'VALID') ORDER BY RecordID;";
                Int32 intRecID = 0;
                try
                {
                    using (OleDbConnection oleConn = new OleDbConnection(connString))
                    {
                        using (OleDbCommand oleComm = new OleDbCommand(strCmd, oleConn))
                        {
                            oleConn.Open();
                            using (OleDbDataReader reader = oleComm.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    intRecID = reader.GetInt32(0);
                                    return intRecID.ToString();
                                }
                            }
                        }
                    }
                    return intRecID.ToString();
                }
                catch (Exception ex)
                {
                    return intRecID.ToString();   
                }
            }
    Is this what I do?
    Code:
            public String GetLoginRecordID(String User, String Password, String result,String connString)
            {
                String strCmd = "SELECT RecordID FROM SystemLogins WHERE (UserID = ?)" + 
                        " AND (UserPassword = ?) AND (Result = 'VALID') ORDER BY RecordID;";
                Int32 intRecID = 0;
                try
                {
                    using (OleDbConnection oleConn = new OleDbConnection(connString))
                    {
                        using (OleDbCommand oleComm = new OleDbCommand(strCmd, oleConn))
                        {
    //change to this?
                            oleComm.Parameters.Add(User);      
                            oleComm.Parameters.Add(Password);
                            oleConn.Open();
                            using (OleDbDataReader reader = oleComm.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    intRecID = reader.GetInt32(0);
                                    return intRecID.ToString();
                                }
                            }
                        }
                    }
                    return intRecID.ToString();
                }
                catch (Exception ex)
                {
                    return intRecID.ToString();   
                }
            }

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Re: Parameterized query

    Or is this a better way to do this?
    Code:
            public string ValidUser(string connString, string User, string Pass)
            {
                OleDbConnection oleConn = new OleDbConnection(connString);
                OleDbCommand oleCmd = new OleDbCommand("SELECT COUNT(*) FROM SystemUsers01 WHERE Userid = @UserID AND Password = @PassWord", oleConn);
                oleCmd.Parameters.AddWithValue("@UserID", User);
                oleCmd.Parameters.AddWithValue("@PassWord", Pass);
                oleConn.Open();
                if ((int)oleCmd.ExecuteScalar() == 0)
                {
                    //not found invalid user
                    return "ERROR";
                }
                else
                {
                    return "GOOD";
                }
            }

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Parameterized query

    well, I'd recommend using the nested using clauses as you did initially ...

    other than that, the parameter forms (? vs @SomeName) is usually dictated by the database in question... SQL Server uses the named parameter format (@SomeName) while Access typically uses the positional format (?) ... but I think you can still use named parameters, but I've never tried personally... so I guess the answer is, find what parameter format works and run with it. If it turns out both work, then Bonus! In that case I'd go with Named Parameters simply because it makes things easier.

    FYI - even if you use positional parameters (?) ... you can still name it when you add it...
    c# Code:
    1. OleDbConnection oleConn = new OleDbConnection(connString);
    2.             OleDbCommand oleCmd = new OleDbCommand("SELECT COUNT(*) FROM SystemUsers01 WHERE Userid = ? AND Password = ?", oleConn);
    3.             oleCmd.Parameters.AddWithValue("UserID", User);
    4.             oleCmd.Parameters.AddWithValue("PassWord", Pass);

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width