简体   繁体   English

如何在C#Winform中将用户添加到SQL Server数据库

[英]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. 我正在尝试将用户插入SQL Server数据库,而不是您所想的方式。

Usually, in SQL Server Management Studio, one would enter new users through the Security - Logins wizard. 通常,在SQL Server Management Studio中,可以通过“安全性-登录”向导输入新用户。 But how can one do this in a C# Winform application? 但是,如何在C#Winform应用程序中做到这一点呢?

Basically, my Winform would have various fields to capture, but the important ones are the username field and all the selected roles 基本上,我的Winform将具有多个要捕获的字段,但重要的是用户名字段和所有选定的角色

在此处输入图片说明

In this form, once a users clicks Add, the data will be stored into a separate SQL Server table. 在这种形式下,用户单击添加后,数据将存储到单独的SQL Server表中。 I would like to add the user to the database credentials 我想将用户添加到数据库凭据中

在此处输入图片说明 在此处输入图片说明

Once they have been added to the security credentials, they have the default data: 将它们添加到安全凭证后,它们将具有默认数据:

  • Windows user/s Windows用户
  • Default schema of [dbo] [dbo]的默认架构
  • If possible, a server role of public 如果可能,将服务器角色设为public
  • The user mapping is the main area to focus on here. 用户映射是此处重点关注的主要领域。 For each role that the user must have, that must be assigned to them for that database. 对于用户必须具有的每个角色,必须为该数据库分配该角色。 So if a user's roles are Capturer and Admin, then they will have those role memberships mapped to that database for the newly created user. 因此,如果用户的角色是Capturer和Admin,则他们会将这些角色成员资格映射到新创建用户的数据库。 The roles that are selected do exist in the db, so there shouldn't be any errors here. 所选角色确实存在于数据库中,因此此处不应有任何错误。

在此处输入图片说明

Is this possible to do via C# Winforms? 通过C#Winforms可以做到吗? Would this be possible using a dataset? 使用数据集可以吗? Would this be conventional C# code ie. 这是常规的C#代码吗?

SqlConnection //the rest of the code follows here SqlConnection //其他代码如下

or would there have to be a new interface/form to utilise? 还是必须要使用新的界面/表格?

If you use SSMS, and right click on any object (including logins, users, etc), you can choose options to see the generation script: 如果使用SSMS,然后右键单击任何对象(包括登录名,用户等),则可以选择以下选项来查看生成脚本:

在此处输入图片说明

在此处输入图片说明

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. 对于诸如密码之类的事情,由于服务器无法通过脚本编写出来,因此您需要查阅CREATE LOGIN等文档。 Note that this does mean your application needs to run with enough access to manage users. 请注意,这确实意味着您的应用程序需要具有足够的访问权限才能运行以管理用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM