问题
Hi i got an error while connection to my sqlitedb.
I create my sqlitedb using Firefox Sqlite Addon.
MyConn : (webconfig)
<connectionStrings>
<add name="TARKMANCAS_CONNECTION" connectionString="Data Source=C:/TARKMANCAS_DB.sqlite;"/>
</connectionStrings>
MyClass:
public TarkBaseDb()
: base("TARKMANCAS_CONNECTION")
{
}
// start
//
public Table<TarkBaseSchema.KadroGrubuCls> KadroGrubu { get { return GetTable<TarkBaseSchema.KadroGrubuCls>(); } }
TarkBaseSchema:
[TableName("EGITIM_KADROSU_GRUBU_TAB")]
public class KadroGrubuCls
{
private TarkBaseDb db = new TarkBaseDb();
#region Contructors
public KadroGrubuCls()
{
using (db)
{
var qry = from x in db.KadroGrubu
select x;
foreach (var rec_ in qry)
{
KadroGrubuId = rec_.KadroGrubuId;
KadroGrubu = rec_.KadroGrubu;
}
}
}
#endregion
#region Data Items
[MapField("KADRO_GRUBU_ID")]
[PrimaryKey, NotNull]
public int KadroGrubuId { get; set; }
[MapField("KADRO_GRUBU")]
public string KadroGrubu { get; set; }
#endregion
#region Relations
#endregion
#region Public Methods
public KadroGrubuCls Get()
{
return (new KadroGrubuCls());
}
#endregion
}
And try a call:
TarkBaseSchema.KadroGrubuCls _tarkKadro = _tarkKadro.Get();
I got 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
回答1:
The error message shows that it's trying to connect to SQL Server, not SQLite.
Change the connection string to select the correct driver.
(And you have two spaces between "Data" and "Source".)
回答2:
I think it is an answer for me;)
Linq2db
The best solution - use linq2db
BLToolkit
If you don't want to use linq2db, follow this steps to connect to sqlite db with bltookit.
Install libs
Install nuget packages (or download it manually)
PM> Install-Package BLToolkit.SQLite
PM> Install-Package System.Data.SQLite.Core
Modify web.config / app.config
<configuration>
<!-- 1 : Add providers to BLToolkit -->
<bltoolkit>
<dataProviders>
<add type="BLToolkit.Data.DataProvider.SQLiteDataProvider, BLToolkit.Data.DataProvider.SQLite.4" />
</dataProviders>
</bltoolkit>
<!-- 2 : Add connection string with the appropriate provider field -->
<appSettings>
<add key="ConnectionString.SQLite.localdb" value="Data Source=C:/TARKMANCAS_DB.sqlite;Version=3;Pooling=True;Max Pool Size=10;" />
</appSettings>
</configuration>
You could find more information about connection strings here: http://www.connectionstrings.com/sqlite/
Create an instance of DbManager
You have used MyClass
, so it should looks like:
public class TarkBaseDb: DbManager
{
public TarkBaseDb()
: base("SQLite", "TARKMANCAS")
{
// definition of your tables
}
}
来源:https://stackoverflow.com/questions/12521390/how-to-connect-sqlitedb-using-bltoolkit-on-asp-net