-2

I am creating a WPF application using C# and SQL Server. To create the set up file I am using advanced installer. The SQL Server is hosted on an online server (cloud cluster). I am connecting the server using this code:

private SqlConnection connect()
{
    con = new SqlConnection();
    con.ConnectionString = " Server=mssql-31772-0.cloudclusters.net,31772 ; Database=Project;User ID=sa ;Password=123 ;Trusted_Connection=true;Integrated Security=false"; 
    return con;
}

And when I installed it on my computer that has SQL Server installed, it is working. But when I tried to use it on another computer that does not have SQL Server, it refuses to work.

Can someone tell me as to why that is happening? Do I need to add some file or something to get it to connect to online server?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
preeti
  • 13
  • 5
  • 2
    Why are you passing a username and password if you're using a trusted connection? It's one or the other. You then go on to declare integrated security off? Integrated Security and Trusted Connection is the same thing. That is a *very* confused connection string. – Larnu May 23 '21 at 12:57
  • oh okay. I will set the trusted connection to false. Thank you ^^ – preeti May 23 '21 at 12:59
  • 1
    Also, it's **strongly** recommended to *not* use the `sa` `LOGIN` for an application; create a `LOGIN` and appropriate `USER` objects with the **minimal** permissions needed and use that. – Larnu May 23 '21 at 13:00
  • I had just used sa for this post but have a different login and password – preeti May 23 '21 at 13:05
  • 1
    `Trusted connection` and `integrated security` are synonyms. Which do you want: Windows Authentication, and therefore `true`, or SQL Authentication and therefore `false`? – Charlieface May 23 '21 at 13:50
  • Try using SQL Server Management Studio to connect to the database. I do not think issue is with MSSQL being installed. I think it is a credential issue. Make sure when you attempt with SSMS that the credentials on the login window uses a username/password and not Window Credential. – jdweng May 23 '21 at 13:52

2 Answers2

0

You can try to add DBMSSOCN since your new server can use TCP/IP instead of Named Pipes. Maybe it is a problem.Also maybe you need a different syntax too

try this connection string.

"Data Source=mssql-31772-0.cloudclusters.net,31772;Network Library=DBMSSOCN;Initial Catalog=Project;User Id=sa;Password=123;"
Serge
  • 7,259
  • 2
  • 7
  • 21
0

I am not sure you can set Trusted connection/integrated security which is the same thing to true when you are connecting to Cloud based SQL from on-prem.
But try with this:
con.ConnectionString="data source=mssql-31772-0.cloudclusters.net,31772; Database=Project;User ID=sa ;Password=123;Integrated Security=false"
And it might also help if you mention what kind of SQL privider client you are using.

kabax
  • 30
  • 4
  • I tried this. it also did not work. I have SQL Server native client 11.0 and ODB Driver 17 for SQL Server. – preeti May 26 '21 at 06:53
  • Are you sure that the SQL port is opened for remote? As i understand, it works when trying locally on the same server that has SQL server? – kabax May 26 '21 at 19:15