Good Help me find a ghost)

C # + SQL SERVER, trying to connect. The default settings, the Studio is connected to the database, and it’s a shame, to pour on the connection

string mybd_host = "."; string mybd_name = "Awr2015"; string mybd_user = ""; string mybd_pass = ""; string connetionString = "Data Source=" + mybd_host + ";Initial Catalog=" + mybd_name + ";User ID=" + mybd_user + ";Password=" + mybd_pass + ""; SqlConnection cnn = new SqlConnection(connetionString); try { try { Console.WriteLine("Connection Open !"); cnn.Open();//вот тут ошибка cnn.Close(); } catch (Exception exp) { Console.WriteLine("connection:" + exp); } } catch (Exception ex) { Console.WriteLine("Can not open connection:" + ex);//mysql } 

Connection passes, Connection Open, but when opening a connection, an error occurs, an exception is thrown: "System.Data.SqlClient.SqlException" in System.Data.dll

those. there is access, but when you open it there is no access. Gentlemen, put on the right path, what to fix to open a connection to the database?

  • 2
    user and pass are empty, is this normal? If you want to connect with windows authentication then add to the line "Integrated Security = True" - Aleksandr Zharinov
  • 7
    To get a Connection String, it's best to use SqlConnectionStringBuilder. And give the text of the exception, one type of exception is not enough to analyze the problem. - nzeemin
  • instance of the sql server default or named? Well, as said earlier: you need the full text of the exception - Konst
  • @Aleksandr Zharinov - helped! Why not in the answers? - gforce
  • @nzeemin - I will keep in mind the option on the trail. projects - gforce

1 answer 1

If you want to connect under a Windows account, you need to add the option "Integration Security = True" to the connection string. In addition, for concatenating strings in general and strings in particular, it is better to use the special classes StringBuilder and SqlConnectionStringBuilder respectively.