2

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 ?

8
  • What is your database format? Sqlite? Commented Jul 4, 2017 at 9:48
  • 1
    The database format is SQLite 3 Commented Jul 4, 2017 at 9:57
  • 4
    Then you can't use SqlConnection, that's for Sql Server. You need to download System.Data.Sqlite and use that instead Commented Jul 4, 2017 at 9:59
  • 1
    Possible duplicate of What is the best way to connect and use a sqlite database from C# Commented Jul 4, 2017 at 10:03
  • Its not a sqllite connection "var c = new SqlConnection(connectionString);" Commented Jul 4, 2017 at 10:06

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.