Error when connecting to SQLite using SqlConnection, why? - c#

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

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

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;

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

windows Application Database Connection c#

Im facing problem with database connection on windows Application Database Connection c#... here is my connection ,,, PC2 is PCNAme
private static SqlConnection con = new SqlConnection(
"Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True"
);
when I run the form I get this unhandled exception on con.Open();
sqlException was unhandeled:
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)
hope some one can help Im trying to solve it for many hours but not working.
Check this trouble shoot steps on MSDN:
The SQL Server client cannot connect to the server MSDN
And much better if you separate your connection string to your ado.net sql connection.
private const string _thisConnectionString = "Data Source=PC2\\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=SSPI";
Use the "Using" Statement ensure that you always close your Connection.
Best Practices for Using ADO.NET MSDN
using (var conn = new SqlConnection(_thisConnectionString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Your SQL Query STuff";
conn.Open();
}
Regards
Try This:
private static SqlConnection con = new SqlConnection(
"Data Source=PC2\SQLEXPRESS; Initial Catalog=Database1;Integrated Security=True"
);
Go to SQL Server Configuration Management -> Click on SQL Server Services
Verify SQL Server and SQL Server browser both are running. you can see a Greed play button there. if it's not running right click on the service which shows Red, go to properties, Click Built in Account and Press Apply then OK, There try to start by right click.

Categories

Resources