2021年3月29日星期一

Getting System.Data.SqlClient.SqlException when trying to connect to MySQL with C#

I'm trying to get a very simple connection to my local SQL server running on my machine with C# Windows Form App in Visual Studios 2019. It's just a simple form with a single button that connects to my server with a message box that opens when the connection is successful but I getting the same error message when I try to run it.

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.

Code for the button:

using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Data.SqlClient;  using System.Drawing;  using System.Linq;  using System.Text;  using System.Threading.Tasks;  using System.Windows.Forms;    namespace CapstoneC  {      public partial class Form1 : Form      {          public Form1()          {              InitializeComponent();          }            private void connectbtn_Click(object sender, EventArgs e)          {              string connetionString;              SqlConnection cnn;                connetionString = @"Data Source=localhost;Initial Catalog=runningdb;User ID=root;Password=56785678Cl%!";                cnn = new SqlConnection(connetionString);                cnn.Open();              MessageBox.Show("Connection Open");                cnn.Close();          }      }  }  

The error always comes up at

cnn.Open():  

I have checked all of the stuff in the connection string and it's all correct so I'm not sure what's going on.

https://stackoverflow.com/questions/66864254/getting-system-data-sqlclient-sqlexception-when-trying-to-connect-to-mysql-with March 30, 2021 at 11:04AM

没有评论:

发表评论