Sql connection error in visual stuio C# - 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

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

Error when connecting to SQLite using SqlConnection, why?

I'm trying to connect to a local database (a ".db" file in my computer) but I get an error when I try to open the connection.
I'm using Sharp Develop (I can't use the connection wizard of Visual Studio to get the con. String).
String connectionString = #"Data Source=C:\Users\Adl\Documents\BBDD_Test.db";
var c = new SqlConnection(connectionString);
c.Open();
The 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
What's happening ?
You are using SqlConnection for SQLite database file(.db).
You will have to download an ADO.NET provider for SQLite or just add System.Data.SQLite.dll to your solution. Now you use certain classes of SQLite viz. SQLiteConnection, SQLiteCommand and SQLiteDataAdapter.
String connectionString = #"Data Source=D:\Users\wkhan2\Downloads\chinook\chinook.db";
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString);
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("select * from table");
cmd.Connection = conn;
conn.Open();
cmd.ExecuteScalar();
System.Data.SQLite.SQLiteDataAdapter da = new System.Data.SQLite.SQLiteDataAdapter(cmd);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
The rest is similar to ado.net i.e for binding DataSet.
Well I did not use SharpDevelop rather used visual studio, hope it works

SQL Server with C#

I am writing a program that connects to a SQL Server (IGOR-PC - SQL Server 10.50.4044 - instance Igor-PC\Igor)
The code is this:
SqlConnection myConnection = newSqlConnection("Server=.\\Igor;Database=Prueba");
myConnection.Open();
But I get this 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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
not monitored System.Data.SqlClient.SqlException
Thanks!
Try This
SqlConnection myConnection = new SqlConnection("Data Source=Igor;Initial Catalog=Prueba;Integrated Security=True");
Edit2:
For the Above Picture and for TEST_DB database the Connection String should be written as
SqlConnection myConnection = new SqlConnection("Data Source=AJMOT-PC;User ID=sa;Password=thisismypassword;Initial Catalog=TEST_DB;Integrated Security=True");
If you are using the Window authentication for sql server then you don't need to declare the User ID=sa;Password=thisismypassword section.

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;

Categories

Resources