Can't Connect to Local SQL Database - c#

This:
SqlConnection myConnection = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\\Users\\Joe\\Documents\\Visual Studio 2012\\Projects\\FileIO\\FileIO\\Database.mdf';Integrated Security=True");
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Returns this error:
A first chance exception of type 'System.Data.SqlClient.SqlException'
occurred in System.Data.dll System.Data.SqlClient.SqlException: 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)
Any Ideas?

You're missing the double backslash on the data source. It's reading the \v as a vertical tab.
Or you can use a leading # before the whole string to treat it as a verbatim string (if you do this then you'll need to clear out the extra backslashes on your filepath).

(Copied from error occurred while establishing a connection to SQL Server)
The problem was related to the application not running in .net version 4.0
According to the following page:
http://www.connectionstrings.com/sql-server-2012#sqlconnection
You need .net 4.0 and up to use named pipes:
The Server=(localdb) syntax is not supported by .NET framework
versions before 4.0.2. However the named pipes connection will work to
connect pre 4.0.2 applications to LocalDB instances.
SqlLocalDB.exe create MyInstance
SqlLocalDB.exe start MyInstance
SqlLocalDB.exe info MyInstance
The info provides the named pipe then this can be used in the connection string:
<add name="conn"
providerName="System.Data.SqlClient"
connectionString="Server=np:\\.\pipe\LOCALDB#B1704E69\tsql\query"/>

Related

how to use pm> Update-Database command?

i am trying to run the command Update-Database in package manager console. the following errors appears.
this is the connection string I tried using
"ConnectionStrings": { "sqlserver": "nvlohuqx:YXGg4spm1KAvIidbFufyLMt9HeCZMZDD#surus.db.elephantsql.com:5432/nvlohuqx" }
it showed this error
“Format Of The Initialization String Does Not Conform To Specification
Starting At Index X”
and then after I changed the format for the connection string to the following:
"sqlserver": "Server=surus.db.elephantsql.com;Database=nvlohuqx;User Id=nvlohuqx;Password=YXGg4spm1KAvIidbFufyLMt9HeCZMZDD;"
I get this new error:
"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)"
I recommend use Ip instead of domain on Server = ""

Problems with SQL Server connection in console app

The SQL server is located on a remote machine.
I can connect to it with MVC app.
I can connect to it with SSMS.
But I can not do it in console app.
Neither entity nor SqlConnection is working.
Connection string is the same as in console and mvc app (which works). What can it be? Any ideas?
Error I am getting
System.Data.SqlClient.SqlException: '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)'
What can it be?
<add name="DBContext" connectionString="data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient";
cn.Open();
Console.WriteLine(cn.State.ToString());
The problem is that console app uses 445 and 139 port. In terms of security. But why MVC app doesn't use it. And how I can I use it without opening this port. Even dividing db part in different library didn't work.

Unable to connect to MySql Server in Visual C#

I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error saying
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)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/

C# WebSite connection to a mySQL database

I want to connect my WebSite to a local database.
My connection String is the following:
<connectionStrings>
<add name ="localhost"
connectionString="server=localhost;user id=root;persistsecurityinfo=True;database=doctorappointments"
providerName="MySql.Data.MySqlClient"
/>
I can connect from MySql Workbench and also from Visual Studio if I add a new connection in Server Explorer. The connection String from above is the same as the one from server Explorer.
I checked that my server is running, I also put a firewall rule for the port that it connects to.
This how I am using the connectionString:
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localhost"].ConnectionString))
{
con.Open();
Response.Redirect("MainPage.aspx");
}
And on the con.Open() it gives me this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in >System.Data.dll but was not handled in user code
Additional information: 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)
and also:
ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
I am new to this and I have tried everything I found online. I do not know what am I doing wrong...
SqlConnection is for Sql Server.
Make sure you have installed MySql.Data via nuget package and use MySqlConnection instead.

Can't access MYSQL server from c# forms application - error in connection string

myConnection = new SqlConnection("user id=champion3_test;" +
"password=test;server=10.168.1.58;" +
"Trusted_Connection=true;" +
"database=champion3_sabdb; " +
"connection timeout=30");
This code gives the error:
Thrown: "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)" (System.Data.SqlClient.SqlException) Exception Message = "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)", Exception Type = "System.Data.SqlClient.SqlException", Exception WinRT Data = ""
Here are some screenshots that might help with debugging of my server's settings:
http://imgur.com/a/PeRby
I know that 'champion3_test' is a valid user id
'test' is a valid password for champion3_test
the database name is 'champion3_sabdb'
SQL server isn't MySQL. If you are using MySQL, download the MySQL .NET Connector and use their connection class, MySqlConnection.
To learn how to use the MySQL Connector, you can start here.
You can download the MySql Connector for .Net here.
Once you've installed the driver, you can connect using the MySql Connector classes:
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);

Categories