Nicholas Aysen
Nicholas Aysen

Reputation: 580

How to add users to a SQL Server database in a C# Winform

I am trying to insert users into a SQL Server database, but not the way you are thinking.

Usually, in SQL Server Management Studio, one would enter new users through the Security - Logins wizard. But how can one do this in a C# Winform application?

Basically, my Winform would have various fields to capture, but the important ones are the username field and all the selected roles

enter image description here

In this form, once a users clicks Add, the data will be stored into a separate SQL Server table. I would like to add the user to the database credentials

enter image description here enter image description here

Once they have been added to the security credentials, they have the default data:

enter image description here

Is this possible to do via C# Winforms? Would this be possible using a dataset? Would this be conventional C# code ie.

SqlConnection //the rest of the code follows here

or would there have to be a new interface/form to utilise?

Upvotes: 3

Views: 2694

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

If you use SSMS, and right click on any object (including logins, users, etc), you can choose options to see the generation script:

enter image description here

enter image description here

This will show you what commands you need to execute to create that object. Remember that you need both the login (to the server) and user (to the database) entries. So: just issue equivalent commands! For some things like passwords, you many need to consult the CREATE LOGIN etc documentation, since the server is unable to script those out. Note that this does mean your application needs to run with enough access to manage users.

Upvotes: 4

Related Questions