简体   繁体   English

C#SQL Server连接问题

[英]C# SQL Server Connection Issue

I am having problems connecting to a SQL Server database from C#. 我从C#连接到SQL Server数据库时遇到问题。

The exception that returns is the login has failed for the specified user, which is clear enough. 返回的异常是指定用户的登录失败,这很清楚。 However, I am not sure why it fails as the username and password are definitely correct. 但是,我不确定为什么会失败,因为用户名和密码绝对正确。 Are there any settings I need to enable on the SQL Server to allow this to happen, as it is a default express install, 我是否需要在SQL Server上启用任何设置以允许这种情况发生,因为它是默认的快速安装,

Thanks, 谢谢,

Below is my connection code if I'm missing anything obvious. 如果缺少任何明显的信息,下面是我的连接代码。

static void Main(string[] args) {
    try
    {
        SqlConnection con = new SqlConnection(@"Data Source = .\SQLEXPRESS;Initial Catalog=furniture_display;User ID=login;Password=login");
        con.Open();
        Console.WriteLine("all ok");
        con.Close();
    }
    catch (SqlException err)
    {
        Console.WriteLine(err);
    }
}

According to your code Data Source = .\\SQLEXPRESS , you'r trying to connect to a local server. 根据您的代码Data Source = .\\SQLEXPRESS ,您正在尝试连接到本地服务器。 If so you don't need any ID and Password . 如果是这样,则不需要任何ID密码 And be aware of using Catalog , it's somehow tricky and I hate it. 并且要知道使用Catalog ,这有点棘手,我讨厌它。 To know how it's working, check this out. 要知道它是如何工作的,检查出。

Actually I'm using this code and it works like a charm: 实际上,我正在使用此代码,它的工作原理很像:

SqlConnection con = new SqlConnection(@"Server = localhost; Database = furniture_display; Integrated Security=True;");

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

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