0

I am using switch..case to display different things on my page depending on the product id:

            switch (productID)
            {
                case 1:
                    string theSQL = "SELECT * FROM products WHERE id = " + theCurrentCase + "ORDER BY id DESC";
                    DataTable theDT = dbConnectionAndQuery.dbResults(theSQL, ConfigurationManager.ConnectionStrings["mainConn"].ConnectionString);
                    break;
            }

Is there any way to accessing the case value (i.e. 1 in the example above)?

4

2 回答 2

7

Of course, use productID :)

filler

filler

于 2013-09-01T17:49:43.860 回答
2

productID would match 1 if that block of code was to be executed so you know already know what the case value is (it would be productID).

On a side note, don't concatenate your SQL statement like that as it leaves you vulnerable to SQL injections. Use Parameters instead. In this case it would have to match 1 to execute but other cases may not be safe (e.g. possibly in your default case if you have one). It's good practice to use Parameters when creating SQL queries.

于 2013-09-01T17:50:09.040 回答