我无法连接到我的本地SQL Server

发布于 2025-02-13 08:17:33 字数 1169 浏览 0 评论 0原文

我正在尝试连接到我的SQL Server本地数据库:

builder.Services.AddDbContextPool<UserDbContext>(options =>
{
        options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString"), o =>
        {
               o.EnableRetryOnFailure();
        });
});

API Controller:

[ApiController]
[Route("[controller]")]
public class UserController : Controller
{
    private readonly UserDbContext userDbContext;

    public UserController(UserDbContext dbContext) => userDbContext = dbContext;

    [HttpGet]
    [Route("all")]
    public ActionResult<IEnumerable<User>> GetAllUsers()
    {
        IEnumerable<User> allUsers = userDbContext.Users;
        return Ok(allUsers);
    }
}

这是我的连接字符串:

"ConnectionString": "server=(localdb)\\MSSQLLocalDB;Database=todoapp;User ID=todouser;Password=1234"

现在,当我尝试发送get request时,我会得到响应:

microsoft.data.sqlclient.sqlexception(0x80131904):用户'toduser'的登录失败。

使用SQL Server Management Studio,我可以无问题地连接到数据库。服务器身份验证设置为:“ SQL Server和Windows身份验证模式”,但我也无法与Windows身份验证联系。

I am trying to connect to my SQL Server local database:

builder.Services.AddDbContextPool<UserDbContext>(options =>
{
        options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString"), o =>
        {
               o.EnableRetryOnFailure();
        });
});

API controller:

[ApiController]
[Route("[controller]")]
public class UserController : Controller
{
    private readonly UserDbContext userDbContext;

    public UserController(UserDbContext dbContext) => userDbContext = dbContext;

    [HttpGet]
    [Route("all")]
    public ActionResult<IEnumerable<User>> GetAllUsers()
    {
        IEnumerable<User> allUsers = userDbContext.Users;
        return Ok(allUsers);
    }
}

And here is my connection string:

"ConnectionString": "server=(localdb)\\MSSQLLocalDB;Database=todoapp;User ID=todouser;Password=1234"

Now, when I am trying to send GET request, I get response:

Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'todouser'.

With SQL Server Management Studio, I can connect to the database without any problems. Server authentication is set to: "SQL Server and Windows authentication mode", but I cannot connect with Windows authentication as well.

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

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

发布评论

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

评论(2

高冷爸爸 2025-02-20 08:17:33

尝试更改连接弦以使用Windows身份验证:

"ConnectionString": "server=(localdb)\\MSSQLLocalDB;Database=todoapp;Integrated Security=true"

Try changing the connection-string to use Windows Authentication:

"ConnectionString": "server=(localdb)\\MSSQLLocalDB;Database=todoapp;Integrated Security=true"
爱的那么颓废 2025-02-20 08:17:33

如果您想与招待连接,则必须首先创建它:

  1. 通过win+x在左图中的win+x访问计算机管理,请访问localuser和groups,然后在右图中使用用户名toduser和password添加新用户1234
  2. 接下来,您必须转到MSSQL MS,然后转到安全&gt;右键单击右图默认DB上的todouser&gt;在用户映射中检查ToDoApp数据库。

if you want to connect with todouser you must first create it:

  1. go to computer management via win+x next in left panel go to LocalUser and Groups and next in right panel add new user with username todouser and password 1234
  2. next you must go to MSSQL MS and go to security>login>right click on todouser>properties on right set default db to todoapp -- in User Mapping check the todoapp database.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文