2010-09-15 2 views
7

Sto cercando di ottenere collegato al mio database SQLite di programmazione utilizzando C#/ASP.NET:Come posso accedere a SQLite con C#?

string requete_sql = "SELECT * FROM USERS"; 
connStr = @"Data Source=C:\LocalFolder\FooBar.db;"; 
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr)) { 
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(requete_sql,conn); 
conn.Open(); 
cmd.ExecuteNonQuery(); 
} 

Ma un'eccezione sorge (sulla linea conn.Open()) dicendo che:

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) 

Che è molto strano perché ho copiato la stringa di connessione esatta trovata nel file Web.config.

Come evitare questa eccezione?

PS: il mio obiettivo è collegarmi solo programmaticamente al database senza il file web.config.

Grazie,

Saluti.

risposta

7

Non è possibile connettersi a sqlite db utilizzando le classi SQLProvider. Sono per SQL Server. È necessario utilizzare le classi SQLite provider.

+0

L'URL non funziona più, sto usando Nuget per ottenere la libreria, in caso contrario si può fare riferimento a https://system.data.sqlite.org –

11

SqlLite in C# (richiede System.Data.SQLLite nei riferimenti)

using System.Data.SQLite; 
using System.Data.Common; 


SQLiteConnection db = new SQLiteConnection("Data Source=C:\LocalFolder\FooBar.db;FailIfMissing=True;"); 
db.Open(); 
using (SQLiteCommand comm=db.CreateCommand()) { 
    comm.CommandText = requete_sql; 
    IDataReader dr=comm.ExecuteReader(); 
    while (dr.Read()) 
    { 
    //... 
    } 
} 
+0

Forse si dovrebbe dire che questo non è solo un riferimento ma una dipendenza NuGet. – CodeMonkey