Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:44:49+00:00 2026-06-08T05:44:49+00:00

I am using ASP.NET (VB.NET) with SQL Server and wish to allow a user

  • 0

I am using ASP.NET (VB.NET) with SQL Server and wish to allow a user to create server logins and database users from a web form.

I have gotten this working by executing commands that create the login and user:

Dim cmd As New OleDb.OleDbCommand(
    "CREATE LOGIN [" & login & "] FROM WINDOWS WITH DEFAULT_DATABASE = Db; " & _
    "CREATE USER " & username & " FOR LOGIN [" & login & "]; ",
    connection)
cmd.ExecuteNonQuery()

The login and username are user inputs that I am sanitizing (as best I can) and inserting directly in the CommandText. While this is working, I don’t feel as if I should be doing it this way.

I’m hoping for something similar to parameterized queries, like so:

Dim cmd As New OleDb.OleDbCommand(
    "CREATE LOGIN [@login] FROM WINDOWS WITH DEFAULT_DATABASE = Db; " & _
    "CREATE USER @username FOR LOGIN [@login]; ",
    connection)
cmd.Parameters.AddWithValue("@login", login)
cmd.Parameters.AddWithValue("@username", username)
cmd.ExecuteNonQuery()

However, this does not work because CREATE LOGIN and CREATE USER do not seem to allow parameterized values, as mentioned by podiluska:

Incorrect syntax near ‘@username’.

Is there a better way of creating logins and users from user input?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:44:52+00:00Added an answer on June 8, 2026 at 5:44 am

    One possible option is to use a stored procedure, which I had previously avoided due to sp_addlogin and sp_adduser being deprecated (as mentioned by podiluska).

    However, it seems possible to use CREATE LOGIN and CREATE USER in a stored procedure by building them in a string with EXEC, which is described in this question. The parameterized query code snippet included in the question can then be adapted to use stored procedures with minor changes:

    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "sp_storedprocname"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 as my database engine in a VS2010, C# ASP.NET
I have an asp.net web application written in C# using a SQL Server 2008
I maintain a legacy ASP.Net Web application (using .Net 2.0 + SQL Server 2005
What are the licences required for an asp.net (MVC) web application using sql server
We are developing a web application using asp.net and sql server. We are required
I am implementing a SaaS application using ASP.Net MVC 2 and SQL Server database.
I'm using ASP.net membership system with SQL Server 2000 database. What I want to
I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
Hai guys, I ve developed a web application using asp.net and sql server 2005
I am using Asp.net, C#.net and sql server. my Issue is user enter the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.