C# MySQL error System.Data.SqlClient.SqlException: - c#

Hello i get this error every time when i click on button. I tried running it on both local host and my remove mysql server.
Visual studio error: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)'
I tried putting this into mysql console:GRANT ALL PRIVILEGES On movedb TO root#localhost IDENTIFIED BY ''; FLUSH PRIVILEGES;
Any ideas what do do with it?
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Server=localhost;Database=movedb;Uid=root;Pwd=;");
SqlCommand cmd = new SqlCommand("SELECT usertype FROM table1", cn);
cmd.Parameters.AddWithValue("usertype", usertype.Text);
cn.Open();
string usertype123= cmd.ExecuteScalar()?.ToString();
if (usertype123 == "admin")
MessageBox.Show("yes");
else
MessageBox.Show("You can't access this part of the program. For questions call 867-5309.");
cn.Close();
}

You need install MySql.Data for MySQL by Install-Package MySql.Data
And use MySqlConnection instead of SqlConnection
Refer How to connect to MySQL Database?

Related

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

ASP.NET C# SQL Server not connecting in debug

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.

SDF Local database Connection error: 40

Hi I am trying to connect to a local SQL Server Compact database (.sdf) in a Windows forms project and have been facing this problem for quite some time. I am not allowed to use datasets for the project, all the queries and connections are written in the application.
System.Data.SqlClient.SqlException
A network-related or instance-specific error occurredwhile 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)
Code:
SqlConnection _Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString());
SqlCommand _Command = _Connection.CreateCommand();
_Connection.Open(); // <- throws exception
To connect a Sql Server Compact you need a different set of classes contained in the namespace SqlServerCe (SqlCeConnection, SqlCeCommand and so on....)
SqlCeConnection _Connection = new SqlCeConnection(ConfigurationManager.ConnectionStrings["restaurant"].ToString());
SqlCeCommand _Command = _Connection.CreateCommand();
_Connection.Open();
of course, you need to reference the assembly that contains the above mentioned classes.
System.Data.SqlServerCe.dll (ADO.NET provider)
and add the using statement
using System.Data.SqlServerCe;

Sql connection error in visual stuio C#

I am new to visual studio and trying to connect to a database "demoDB" and update the table "Table1" which has two columns UID and Name. Below is my code.
SqlConnection con = new SqlConnection("Data Source=./SqlExpress;Initial Catalog=DemoDB;Integrated Security=SSPI;");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Table1 values ('WCGC1212','Watson')", con);
int x = cmd.ExecuteNonQuery();
MessageBox.Show(x.ToString());
con.Close();
When i debug it i am getting below error
Index #0
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)
LineNumber: 0
Source: .Net SqlClient Data Provider
Procedure:
Can you please let me know how to fix this error..
Thanks in advance :)
Try: Data Source=.\SqlExpress instead of Data Source=./SqlExpress

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