ASP.NET C# SQL Server not connecting in debug - c#

Whenever I debug my ASP.NET Web Form, I get the following error:
System.Data.SqlClient.SqlException occurred HResult=0x80131904
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)
Source=.Net SqlClient Data Provider
StackTrace:
<Cannot evaluate the exception stack trace>
Inner Exception 1:
Win32Exception: The network name cannot be found
I researched a lot of stuff relating to error 40, and tried everything recommended. Enabled TCP/IP, set the port to 1433, created firewall rules allowing connections through TCP/IP port 1433, restarted the server after each change, and then restarted the computer. Basically, everything from this thread. The SQL Server is on the same machine that I am testing from, so it's not a remote connection issue, I wouldn't think. There is the Inner Exception about Win32Exception, but everything I'm running is 64bit. Maybe this has something to do with it?
Running Windows 10, Visual Studio 2017 Community Edition, SQL Server 2016 Express, and the relevant code is below:
String IacNum = txtIacNum.Text;
String CheckResult;
String queryString = "SELECT ExpirationDate FROM [dbo].[IACList] WHERE ApprovalNumber = #IacNum";
String connectionString = "Data Source=PCNAME/SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("#IacNum", IacNum);
command.Connection.Open(); //Exception pops up here
var reader = command.ExecuteScalar();
if(reader != null)
{
CheckResult = "Valid. Expires" + reader;
}
else
{
CheckResult = "Invalid";
}
command.Connection.Close();
MessageBox.Show(this, CheckResult);

Try viewing the SQL Server from Server Explorer\Data Connections. If you can create a connection, you will see a connection string property. You can than compare that and see if you the same values -- you may have a typo.

Related

Connection string to SQL Server Express only works from the same server but not from other clients

I have this connection string and working well when I test my aplication from the same SQL Express, I have an instance called DOKUSTAR.
using (SqlConnection conn = new SqlConnection(#"server=.\DOKUSTAR;Database=RdaDB10;Trusted_Connection=Yes"))
using (SqlCommand comm = new SqlCommand(#"select top(1) pais, clase, operation from Document_Class where codigoafip = #codafip", conn))
The problem is when I tried to connect from outside of the server does not works the connection and show me the following messages:
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).
Any suggestions will be greatly appreciated.
.\DOKUSTAR refers to an instance of DOKUSTAR on the current machine.
You need to change it to MACHINE_NAME\DOKUSTAR

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

I am running a local mysql database on my computer using wampserver. I have made a c# console application that I want to use to update values in a table. However I am having issues connecting to the database. My password is an empty string.
string user = "John Doe";
string queryString = "UPDATE users SET is_awesome=1 WHERE user=#a1";
string connectionString = "Server=localhost;Database=mydatabase;User Id=root;Password=;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = new SqlCommand(queryString, connection);
cmd.Parameters.Add("a1", user);
cmd.ExecuteNonQuery();
However when I attempt to run this code I get the following error:
System.Data.SqlClient.SqlException
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)'
Inner Exception
Win32Exception: The system cannot find the file specified
I believe my database allows remote connections because I am able to connect to it using a different php application I am running. How can I connect to a sql database from c# and run my query?
You must connect to MySql witch is specific for MySql Like MySqlConnection for more look this tutorial
or this

LocalDb "No process on the end of the pipe"

I've made a copy of my database (which is on Azure) on local SQLExpress server. On this server I have user "Tester" that has db_owner role for that database.
I can easily connect to it using Microsoft SQL Server Management Studio using server name: (LocalDb)\MSSQLLocalDB, SQL Server Authentication and "Tester" user credentials. but when I connect to it in my code with connection string like:
const string ConnectionString =
"Server=(LocalDb)\\MSSQLLocalDB;" +
"Initial Catalog=Integration-Tests;" +
"Persist Security Info=False;" +
"User ID=Tester;" +
"Password=secretPassword;" +
"MultipleActiveResultSets=False;" +
"Encrypt=True;" +
"TrustServerCertificate=False;" +
"Connection Timeout=30;";
using (var connection = new SqlConnection(ConnectionString))
{
try
{
connection.Open(); // Exception thrown
/* some more code that doesn't matter */
}
finally
{
connection.Close();
}
}
I get following exception thrown:
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: Named Pipes Provider, error: 0 - No process is on the other end of the pipe.)
Earlier I was getting something else, but I guess it could be a simple typo since it stopped happening, I'm including it just in case it helps:
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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.
As stated earlier, everything works fine both on Azure, and I can connect to my LocalDb through SSMS.
Does anyone have any idea what could be cause of this error?
I've found the answer, posting it for anyone who ever has same problem:
I realized there was no verifiable server certificate, and according to Microsoft documentation, with Encrypt=true in my connection string:
Encryption occurs only if there is a verifiable server certificate, otherwise the connection attempt fails.
Indeed, the error code could be a little more descriptive... but all I had to do, was to remove Encrypt part from connection string.

Visual Studio connects to database, but application within using the same connection string cannot

Using SSMS and Visual Studio 2015's Server Explorer tab under Data Connections, I can execute queries on remote server KOSH without issue. Why is the MVC application running locally in Visual Studio/IIS Express unable to do the same?
Using VS2015's connection Properties, I get its connection string:
Data Source=123.456.78.9;Persist Security Info=True;User ID=Foo;Password=Bar
Using that connection string, the MVC application is greeted with:
"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)"
Inner exception message:
"The network path was not found"
I know the server/firewall/network settings are correct. That leaves the application.
using(var connection = Database.Connection(conInfo.ConnectionString, provider)) {
// rowCountSql: SELECT SUM(rows) TM_ROWCOUNT FROM sys.partitions
// WHERE object_id = object_id(#tableName) and index_id IN (0,1)
// Any other SQL yields same error
var countTask = connection.QueryAsync(rowCountSql, new { tableName = Editor.TableName });
}
Async is used because the Oracle version of the query can take several seconds to return.
The application should be correct because it connects to the same server from home (also the server's location) and to identical databases in remote data centers running 2008R2 to 2014 without a problem.
Database.Connection() is:
// This is unlikely to be the problem as it is very well tested
public static DbConnection Connection(string connectionString, string databaseProvider) {
DbProviderFactory databaseFactory = DbProviderFactories.GetFactory(databaseProvider);
DbConnection connection = databaseFactory.CreateConnection();
if(connection != null)
connection.ConnectionString = connectionString;
return connection;
}
I bet I am missing something simple, but I would be grateful for help on what that could be.
As I said in comments, maybe the piece of your code:
conInfo.ConnectionString
does not contains the correct connection string you want.
Check it out

Connect to External SQL Database in C#

I've got an SQL server and database setup on an external server (let's call the domain name "hello.com" for the purposes of this), and I want to connect to this server via a C# program. So far I have this (All server/database details are different to the real ones):
private static void SetupSQL()
{
string connectionString = "server=hello.com; database=db1; uid=user1; pwd=xxxxx;";
connection = new SqlConnection();
connection.ConnectionString = connectionString;
try
{
connection.Open();
Console.WriteLine("Connected");
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
This is giving me an error 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)
I have checked all the connection string, and I am allowed remote access, as I have SQLWorkbench open querying the database right now on the same computer.
Any ideas?
You'll need the MySQL driver:
http://dev.mysql.com/downloads/connector/net/
You can then use the the MySqlConnection connection class to connect.
MySqlConnection connection = new MySqlConnection(connectionString);
http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
You can't use SqlConnection object to connect to MySQL database, you should use MySqlConnection instead after you import its dll

Categories

Resources