How to connect SqliteDB using BLToolkit on ASP.NET?

拥有回忆 提交于 2020-06-28 14:33:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!