创建 Sql Server 身份验证帐户?

发布于 2024-12-11 19:51:19 字数 102 浏览 0 评论 0原文

我想创建一个Sql Server身份验证帐户,而不是数据库或帐户中的用户。

我的意思是我想创建一个帐户,以便在启动 sql 服务器时使用 SQL 事务或使用 C# 从中登录。

I want to create an Sql Server Authentication Account, not a user in the database or in the account.

What i mean is that i want to create an account so i can login from it when i start the sql server, using SQL Transaction or using C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-12-18 19:51:19

据我所知,您只能使用两种模式:Windows 或 SQL Server 身份验证。这意味着您只有两种选择:使用 Windows 帐户,或者创建可用于身份验证的 SQL Server 登录名。

编辑:

要以编程方式创建 sql server 登录名,您可以使用 Sql Server Management 对象。我注意到您没有说您是想从 Windows(桌面)还是 Web 应用程序执行此操作。要使用 Sql Server Management 创建登录名(或执行任何类型的“管理”),代码需要以更高的权限运行 - 例如具有足够权限来管理 SQL Server 实例的本地或域帐户。

您将需要添加对(您可以使用版本 9.0 的程序集)的引用:

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlEnum

这样,以下代码足以创建 SQL 登录并将用户添加到目标数据库

        var serverName = "."; // Your SQL Server Instance name
        var databaseName = "Test"; // Your database name
        var loginName = "testuserY"; // Your login name (should not exist - or you should add code to check if the login exists)


        Server svr = new Server(serverName);
        var db = svr.Databases[databaseName];
        if (db != null)
        {
            // You probably want to create a login and add as a user to your database
            Login login = new Login(svr, loginName);
            login.DefaultDatabase = "master"; // Logins typically have master as default database
            login.LoginType = LoginType.SqlLogin;
            login.Create("foobar", LoginCreateOptions.None); // Enter a suitable password
            login.Enable();

            User user = new User(db, loginName);
            user.UserType = UserType.SqlLogin;
            user.Login = login.Name;
            user.Create();
            // add a role
            user.AddToRole("db_owner");
        }

您将需要添加:

using Microsoft.SqlServer.Management.Smo;

您可以添加尝试{} catch{} 块和管道代码使其更加健壮,您可以在发生故障时优雅地恢复

There are only two modes that I know you can use: Windows or SQL Server Authentication. This means you are limited to only two choices: You either use a Windows account, or you create an SQL Server login that you can use to authenticate.

EDIT:

To programmatically create an sql server login, you could use Sql Server Management objects. I notice you didn't say whether you want to do this from a Windows (desktop) or Web application. To use Sql Server Management to create a login (or do any sort of "management"), the code would need to run with higher privileges - say local or domain account with sufficient privileges to administer the SQL server instance.

You will need to add references to (you could use version 9.0 of the assemblies):

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlEnum

With that, the following code is enough to create an SQL login and also add the user to your target database

        var serverName = "."; // Your SQL Server Instance name
        var databaseName = "Test"; // Your database name
        var loginName = "testuserY"; // Your login name (should not exist - or you should add code to check if the login exists)


        Server svr = new Server(serverName);
        var db = svr.Databases[databaseName];
        if (db != null)
        {
            // You probably want to create a login and add as a user to your database
            Login login = new Login(svr, loginName);
            login.DefaultDatabase = "master"; // Logins typically have master as default database
            login.LoginType = LoginType.SqlLogin;
            login.Create("foobar", LoginCreateOptions.None); // Enter a suitable password
            login.Enable();

            User user = new User(db, loginName);
            user.UserType = UserType.SqlLogin;
            user.Login = login.Name;
            user.Create();
            // add a role
            user.AddToRole("db_owner");
        }

You will need to add:

using Microsoft.SqlServer.Management.Smo;

You can add try{} catch{} blocks and plumbing code to make it more robust you would recover gracefully in case of failure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文