How to access Azure SQL database with a Connection String in C#? - c#

I can log into the server explorer to work on my database.
Yet I fail to access it with a Connection String.

is your connection string in the format of Server=tcp:{server}.database.windows.net,1433;Initial Catalog={databaseName};Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
double check the password and firewall for your IP

This is ADO.NET Connection String that I use for my Azure SQL C# project:
Server=tcp:YOUR_SERVER,1433;
Initial Catalog=YOUR_DB_NAMR;
Persist Security Info=False;
User ID=YOUR_LOGIN;Password=YOUR_PASSWORD};
MultipleActiveResultSets=False;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;
And as #Krunal mentioned, double check your login credentials
and make sure your IP is whitelisted on the server side.
You can set a 0.0.0.0 to 250.250.250.250 range as a whitelisted range, for testing purposes.

Related

SQL Server login failed for user client computer

I am using SQL Server r2 database as a remote server having mixed mode sql authentication.
When run my c# program from client computer to access database with following connection string:
"Data Source=192.168.1.101\SQLEXPRESS,1433;Initial Catalog=Login; Integrated Security=true;Connection timeout=30; User ID=sa;Password=23456"
and facing an error:
Login failed for user 'VAIO\Guest'.
VAIO is remote PC.
you are using Integrated Security=true which denotes that sql will try to login using Windows authentication. It seems that this windows user is not a valid login for sql server. If you want to use user and password set:
Integrated Security=false
it worked for me. use Sa user id and password...
"Data Source=192.168.8.106\PEBB, 1433;Initial Catalog=d2;Integrated Security=False;user id=sa;password=123;Connect Timeout=30"```
Add one space before port no.
Data Source=192.168.1.101\SQLEXPRESS, 1433

How to use establish a secure connection in code behind application page SharePoint 2010

I need to solve the double hop issue for SharePoint 2010 application page. The ASP (C# code behind) page must connect securely to SQL server instance on another host. The secure store service appears to me to only work for a webpart. This can not be accomplished for the application page. Is there a secure alternative?
Current connection string is similar to:
private const string CONNECT_STR = #"Data Source=10.X.X.X,1433
;Initial Catalog=TestCatalogName ; Integrated Security=false;User
ID=testID;Password=********";
SqlConnection con = new SqlConnection(CONNECT_STR)
This seems like a common problem in SharePoint so I hope there is a corresponding common solution.
Only A webpart can use the secure store service in SharePoint 2010. The solution in place uses a SharePoint application page. I am connecting SQL Server and a viable alternative is to use a certificate for the encrypted connection.
See Enabling Encryption section in Using Encryption Without Validation in the client as in my case and concerned only with encryption over the connection. Both the client and server hosts are trusted.
See Selectively using secure connection to SQL Server. The table provides a good explanation of how to achieve the result of a my client using encrypted connection without impacting the SQL server and other connections to it.
The updated connection string is:
private const string CONNECT_STR = #"Data Source=10.X.X.X,1433 ;Initial Catalog=CM_ReleaseManagement ; Integrated Security=false; TrustServerCertificate=true; Encrypt=true; User ID=testID;Password=********";
See "Encrypting Connections to SQL Server" (insufficient reputation to post link) for instructions on configuring SQL Server per Microsoft when you need to encrypt all communications and higher level of security is required. No PII/PHI data involved.

Local Connection String

Any one help me out in making a string. i want to connect with local database, my local db has no password. I m using below string to connect with database but getting error unable to open connection for user
string _connectionString = "Data Source=(local);Initial Catalog=CRM;Integrated Security=False;User Id=Ali-Pc;Password= ? ;connection Timeout=0";
You don't mention what you're trying to connect to, but this site is very useful for finding
the connection strings to most databases
Connection Strings
You probably want integrated security set to true and no password

Writing a connection string to access a remote SQL Server database

My database is hosted on a remote server. I need to write the connection string which I have to include in the web.config file.
server name -- abcs.efgh.ed-1.eee.sss.com,1433 (it also contains a port as well)
username -- a
password -- a
db name -- mydb
I know how to connect to a local database and I use to refer to connectionstring.com for reference but, connecting to a remote database is a problem to me. Help please
UPDATE:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient"
connectionString="abcs.efgh.ed-1.eee.sss.com,1433;Integrated Security=True;User ID=a;Password=a" />
</connectionStrings>
Exception I get is :
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Actually, it's not Windows authentication that I want to use. It's SQL Server authentication which I want.
You are encountering the error because you are using "integrated security=true" to connect. Use this website to construct your connection string. http://www.developerfusion.com/tools/sql-connection-string/
I used the website to generate this connection string using your inputs:
Data Source=abcs.efgh.ed-1.eee.sss.com,1433;Initial Catalog=mydb;Integrated Security=False;User ID=a;Password=a
Put Integrated security =false. in connection string
When false, User ID and Password are specified in the connection. When
true, the current Windows account credentials are used for
authentication. Recognized values are true, false, yes, no, and sspi
(strongly recommended), which is equivalent to true. If User ID and
Password are specified and Integrated Security is set to true, the
User ID and Password will be ignored and Integrated Security will be
used. SqlCredential is a more secure way to specify credentials for a
connection that uses SQL Server Authentication (Integrated
Security=false).
More at Here

ASP.NET beginner question: How to get rid of this error?

I am trying to create a simple login page in ASP.NET C# but am getting some error.
I googled about the error, and tried all the solutions but to no avail.
What am I doing wrong? The error is:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
I think it's something to do with my connection string which is as follows:
<add name="cn"
connectionString="server=HCL;DataBase=GS;Integrated Security=True" />
"HCL" is another machine connected to my PC through the LAN and this database, "GS", resides on it. Is the problem with the value I am giving in "Server" attribute of the above code? If not what else may I be doing wrong?
I can tell all solutions I tried. Let me know if that's required.
I don't think "Server" is a valid property for a connection string. Try "Data Source" instead. "Integrated Security" should also be set to SSPI:
<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;Integrated Security=SSPI"/>
UPDATE:
I just noticed that you have also used "Database". This should be "Initial Catalog".
UPDATE2:
There is a neat trick for creating connection strings by using .udl files. If you create an empty file called "something.udl" and double click it, Windows will open a nice dialog for defining connections. To create a connection string for a SQL Server, choose "Microsoft OLEDB Provider for SQL Server" on the "Provider" tab and then fill in your server name, login credentials, and database name on the "Connection" tab. Finish by testing the connection and click "OK".
After the dialog is closed, you can drag the .udl file into Notepad or Visual Studio and you will see that the .udl file actually contains a connection string ready for you to use (note that if you wish to use the connection string with SqlConnection in .NET you must remove the "Provider=SQLOLEDB.1" part of the string).
The error is definitely connectivity. Usually in firewall rules, remote connections disabled or just general connectivity issues.
However using integrated security is probably your issue. If you are in a domain/workgroup this could work,w ith properly set permissions, but if not then you probably need to pass your credentials. Integrated security will pass the credentials of your logged in account and is the normal method if the database in on the same PC as the application.
<add name="cn" connectionString="Data Source=HCL;Initial Catalog=GS;User Id=YOURSQLUSERNAME;Password=YOURSQLPASSWORD;"/>
See http://www.connectionstrings.com/ for more connection string examples.
Try adding "Integrated Security=SSPI;" or specify your UID, Pwd for connecting to the server. You might be getting struck at the authentication.
Open management studio and connect with SQL Server using SQL Server Authentication. Enter userId and password to connect. Use that userid and password in your connection string.
connectionString="Data Source=HCL;Initial Catalog=GS;User Id=UserId;Password=password;
User Id -> UserId you want to login
using. like 'sa'
Password -> password of you user.
If you get error Login failed for user" 'yourusername' then this link will help. This is to enable user sa.
SQL Server 2005 "Login failed for user" sa
EDIT:
connectionString="Data Source=HCL;Initial Catalog=GS;Persist Security Info=True;User ID=sa;Password=pass"
This link might help
sql-network-interfaces-error-26-error-locating-server-instance-specified
I think you should try trusted_connection = true instead of integrated security in your connectionstring.
See here: http://connectionstrings.com/sql-server-2005
Second reason could be that you have several instances of your database which you have to write into the connectionstring.

Categories

Resources