Adding an SQLParameter to my SQLCommand - Getting error

I am trying to make a .NET application that turns off the "Enforce password policy" and "Enforce password expiration" options for a SQL user in SQL Server 2005.  This script works in my application (where "tester" is the username):

USE master ALTER LOGIN tester WITH CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

However, when I try to use a SQL Parameter instead of manually building the string, I get an error that says "Incorrect syntax near '@UserName'."  Here is my updated script:

USE master ALTER LOGIN @UserName WITH CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

and here is the .NET code where I am adding my SQLParameter:

            cmd = New SqlCommand(RESETPASSWORDSCRIPT, conn.SqlConn)
            cmd.CommandType = CommandType.Text

            With cmd.Parameters
                .Add("@UserName", SqlDbType.NVarChar).Value = userName
            End With

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

Can anybody see where I might be doing something wrong?  RESETPASSWORDSCRIPT is equal to the script I posted.
RAFUserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pollock_dCommented:
what does the cmd.commandtext look like just before cmd.ExecuteNonQuery() ??
0
RAFUserAuthor Commented:
I copied and pasted it right from the Watch window:
USE master ALTER LOGIN @UserName WITH CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

If I look at the Parameters collection in my command I can see the one parameter successfully added.
0
mastooCommented:
I don't think you can parameterize username on that statement.  You can either build the string dynamically in sql from the parameter and then invoke it via Exec(), or...

Dim sSql as string = "USE master ALTER LOGIN '" + @UserName + "' WITH CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF"

and then execute that.  If you're worried about injection you need to substitute for single quotes in @UserName.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RAFUserAuthor Commented:
Naw, I will just build the string dynamically.  I was trying to dabble with using parameters with command text instead of using Stored Procs like I usually do.

Now that I look at the statement closely I can see why I wouldn't be able to parameterize it - It would be like parameterizing a table name in a CRUD query.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.