搜尋此網誌

2012-12-31

ASP.NET 4.0 連結 SQLite 設定

初學SQLite,筆記一下。
1.先安裝NuGet

2.用NuGet安裝System.Data.SQLite,這樣是選擇x86和x64都有的版本

3.若要使用ASP.NET的membership provider和role provider功能,可安裝TechInfoSystems.Data.SQLite
web.config設定方式與DB檔可參考此篇:SQLite Membership, Role, and Profile Providers

4.連結資料庫,若懶得自己寫code,可安裝Enterprise Library Data Access Application Block

5.若要在Visual Studio裡面直接管理SQLite DB,或需要設計介面,可到SQLite網站上下載Precompiled Binaries for .NET

6.我的web.config設定,其中有一個DbProviderFactories是參考SQLite readme設定的,不然EntLib的DAAB不會動
<?xml version="1.0" encoding="utf-8"?>
<!--
  如需如何設定 ASP.NET 應用程式的詳細資訊,請造訪
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- EnterpriseLibrary Configuration -->
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
  </configSections>
  <!-- EnterpriseLibrary defaultDatabase -->
  <dataConfiguration defaultDatabase="DefaultConnection">
    <providerMappings>
      <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="System.Data.SQLite" />
    </providerMappings>
  </dataConfiguration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|app_data.sqlite;Version=3;" providerName="System.Data.SQLite" />
  </connectionStrings>
  <system.data>
    <!-- SQLite Data Provider -->
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms" />
    <!-- Membership provider -->
    <membership defaultProvider="SQLiteMembershipProvider">
      <providers>
        <clear />
        <add applicationName="SQLite ASP.NET Provider" passwordFormat="Clear" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="2" maxInvalidPasswordAttempts="2" enablePasswordReset="true" enablePasswordRetrieval="true" passwordAttemptWindow="10" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" connectionStringName="DefaultConnection" name="SQLiteMembershipProvider" type="TechInfoSystems.Data.SQLite.SQLiteMembershipProvider, SQLiteMembershipProvider" />
      </providers>
    </membership>
    <!-- Role provider -->
    <roleManager enabled="true" cacheRolesInCookie="true" cookieProtection="Validation" defaultProvider="SQLiteRoleProvider">
      <providers>
        <clear />
        <add applicationName="SQLite ASP.NET Provider" connectionStringName="DefaultConnection" name="SQLiteRoleProvider" type="TechInfoSystems.Data.SQLite.SQLiteRoleProvider, SQLiteMembershipProvider" />
      </providers>
    </roleManager>
    <!-- Profile provider -->
    <profile defaultProvider="SQLiteProfileProvider">
      <providers>
        <clear />
        <add applicationName="SQLite ASP.NET Provider" connectionStringName="DefaultConnection" name="SQLiteProfileProvider" type="TechInfoSystems.Data.SQLite.SQLiteProfileProvider, SQLiteMembershipProvider" />
      </providers>
    </profile>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.82.0" newVersion="1.0.82.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
----- Android SQLiteDatabase

沒有留言: