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 ?

Liam
  • 27,717
  • 28
  • 128
  • 190
Ralk
  • 443
  • 1
  • 6
  • 17
  • What is your database format? Sqlite? – Pikoh Jul 04 '17 at 09:48
  • 1
    The database format is SQLite 3 – Ralk Jul 04 '17 at 09:57
  • 4
    Then you can't use `SqlConnection`, that's for Sql Server. You need to download [System.Data.Sqlite](https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki) and use that instead – Pikoh Jul 04 '17 at 09:59
  • 1
    Possible duplicate of [What is the best way to connect and use a sqlite database from C#](https://stackoverflow.com/questions/26020/what-is-the-best-way-to-connect-and-use-a-sqlite-database-from-c-sharp) – Liam Jul 04 '17 at 10:03
  • Its not a sqllite connection "var c = new SqlConnection(connectionString);" – Vinothkk Jul 04 '17 at 10:06
  • The Database is SQLite. That's the crux of the OPs problem @Vinothkk. No use telling them how to connect to Sqlserver, they don't have SQL server. – Liam Jul 04 '17 at 10:07
  • refer this article https://www.codeproject.com/Articles/22165/Using-SQLite-in-your-C-Application – Vinothkk Jul 04 '17 at 10:14
  • see this answer it can help you solve your problem in a step by step manner https://stackoverflow.com/a/65482510/6078802 – ali asghar tofighian Dec 28 '20 at 22:29

1 Answers1

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

Wasim
  • 61
  • 4