NT SQL Connecting - c#

how can i connect win nt sqlexpress?
i am using this code to connect.it works with any version and kind of sql.
internal string GetConnectionString()
{
return "Data Source=" + "MyPC\SQLExpress" +
";Initial Catalog=Master;User ID=" + username +
";Password=" + password;
}
when connectiong with express ===>>>
Login failed for user 'NT AUTHORITY'. The user is not associated with a trusted SQL Server connection.
or
Login failed for user 'sa'. The user is not associated with a trusted SQL Server connection.
or
Login failed for user 'sysadmin'. The user is not associated with a trusted SQL Server connection.

You need to add the following to the connection string:
Trusted_Connection=yes;
If you are trying to use windows credentials you will need to set:
Integrated Security=true;
This will use Windows authentication rather than SQL authentication. If you don't spesify a user name and password, the currently logged in user will be used implicitly.
See http://www.connectionstrings.com/sql-server-2005 for more details.

Related

SqlConnection works with "Integrated Security =True" but I cannot connect with my login details

I have some working code that is currently sitting on our test dev server that connects to an SQL server using Integrated Security/Trusted_Connection at the moment.
I will need to deploy this to a clients server and it will be scheduled to run every hour regardless of if a user is logged on or not.
as the user may not be logged in I assume that the Integrated security will not work.
The credentials I am putting in are the same as those I use to connect to the dev server. I have tried both with the domain "organisation\username" however this does not work either.
SqlConnection myConnection = new SqlConnection("User id=username;" + "Password=xxxxxx; + "Data Source=datasrc;" + "Initial Catalog=catalog; " + "connection timeout=30");
myConnection.Open();
The above code does not work and results in the program 'hanging' before running myConnection.Open()
However putting "Integrated Security=True;" and removing the "User id" and "Password" sections of the string works.
When I try to use "User id=name;" + "Password=xxxxxx;" it does not work and just hangs.
Below is an error message from trying to login with username and password (windows authentication works fine) on the SQL Server 2014 Management Studio
"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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
The network path was not found"
Integrated Security=True; means that the current credential being used to run the application pool will also be used to login to the SQL database. It doesn't make sense to use Integrated Security=True; and a User ID + Password. One or the other.
So, if your IIS instance is using an application pool logged in by User123, and the .NET application sitting on that IIS instance connects to a SQL database, then that .NET application will attempt to login to the SQL database using User123, so long as Integrated Security=True;. Typically people do this to ensure no plaintext passwords are present in the web.config, for security reasons. EDIT: you can see which user the application pool is using by right clicking the application pool in IIS manager and going to 'Advanced Settings'.
If it only works when you have Integrated Security=True; then that means only that user has permissions to access the SQL database. I would open up SSMS, navigate to the SQL database, and review the Logins under the Security navigation tree. Keep in mind there is a Login for the database server, as well as for the database itself, so you may have to configure this in two places.

SQL Connection String appsettings.json

This is my connection string
{
"Data": {
"PhotoGalleryConnection": {
"ConnectionString": "Server=WINDOWS-B7MJR5T\\SQLEXPRESS;User Id=sa;password=allah;Database=PhotoGallery;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
And I am facing this exception
An exception of type 'System.Data.SqlClient.SqlException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: Cannot open database "PhotoGallery" requested
by the login. The login failed.
It should work after You delete Trusted_Connection=True; from connection string.
If you specify either Trusted_Connection=True; or Integrated
Security=SSPI; or Integrated Security=true; in your connection string
THEN (and only then) you have Windows Authentication happening. Any
user id= setting in the connection string will be ignored. Found in here: When using Trusted_Connection=true and SQL Server authentication, will this effect performance?
Please search services in the window menu, it will show you the local services then go to SQL server services and refresh all, then go to the SQL server management studio and by using SQL authentication please provide username and password, you will be able to use the SQL server. for appsetting.json update, trusted_connection is used for window authentication not for SQL server authentication by using username and password

Cannot connect to my linux server MySQL DB

My MySQL connect code below is 'Catching' this error: "Unable to connect to any of the specified MySQL hosts."
My DB is on my linux website and I have remote access enabled using % so anyone can access the DB.
My code is .NET C# and I have a winform.
string server = "http://www.mywebite.net";
string database = "mywebsite_app";
string uid = "admin";
string password = "Password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection dbConn;
try
{
dbConn = new MySqlConnection(connectionString);
dbConn.Open();
if (dbConn.State.ToString() != "Open")
{
this.Text = "could not open database connection";
}
else
{
this.Text = "database connection opened";
}
}
catch (Exception ex) // catch on general exceptions, not specific
{
this.Text = ex.Message;
}
Thanks for any help...
If you mysql database is on remote hosting, then you usually out of luck try to connect to that database remotely.
The hosting provider usually blocked remote mysql access.
Try using ssh-tunneling.
download putty
http://www.chiark.greenend.org.uk/~sgtatham/putty/
open the command line, and execute putty from the installation directory
putty username#remote_mysqlhost -L 3306:localhost:3306
or you could use the gui, fill the address with your mysql remote, but dont open connection yet.
go to ssh tunnel, add new forwarded port 3306, and the destination as localhost:3306.
as of why the destination is localhost, it is because the destination is what the ssh session see. because you already connected to your remote server in your ssh session, thus, the mysql host is local.
this could also be a set up for dmz.
you can create a dmz gateway using putty to tunnel trough any computer within the dmz network. but the computer behind the gateway, wont publicly expose to the internet.
dont forget to klik the add button to make sure the configuration is added to tunnel list.
open the connection and use your password (ssh user and password, usually the same as webhosting control panel user and password
Now, instead connecting to your remote mysql, you can connect to mysql trough localhost. it would be tunneled by putty to your remote mysql.
try to connect using mysql administration tool first. to test wheter or not the connection is success.
the way ssh tunnel work is almost the same as vpn, from user view.
but without the hassle of setting up vpn server on remote host.

Why I cant connect to the database by connection string?

I have a database on a local network.
I can connect to the database in SSMS:
But when I want to connect to the database by this connection string in c#:
"Data Source=192.168.0.3,14330;Network Library=DBMSSOCN;Initial Catalog=master;Integrated Security=True;User ID=sa;Password=123456789;"
I get the following error:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Your are telling sql server to use integrated security, ie windows security, however you trying to use a username and password
turn integrated security off, see the below example
"Data Source=192.168.0.3,14330;Network Library=DBMSSOCN;Initial Catalog=master;Integrated Security=False;User ID=sa;Password=123456789;"
Pertinent information
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.
You can find more information on connection strings here, SqlConnection.ConnectionString Property
Update
maybe this question may help you more How to get the connection String from a database
Update 2
For more in depth troubleshooting, there is a great resource here How to Troubleshoot Connecting to the SQL Server Database Engine

C# SQL Server Login Credentials

This is my first time connecting to SQL Server via C#, apologies in advance.
I'm trying to connect to my SQL Server to get some information. The code I'm using is:
SqlConnection myConnection = new SqlConnection("server=server;" +
"Trusted_Connection=yes;" +
"database=DB; " +
"connection timeout=30;"+
"User ID=SQLUser;" + "Password= SQLPass;");
myConnection.Open();
However, when I try to connect, I get the error:
Login failed for user 'Company\Personal ID'.
These are not the credentials I intended to use (sqluser & sqlpass). If anyone knows where I'm going wrong, I'd appreciate it.
Simply get rid of Trusted_Connection=yes;. It's ignoring the User ID and Password parameters because you're telling it to use your domain credentials.
Now, the error itself is saying that your domain user doesn't have a Login to the SQL Server.

Categories