まさるblog

越後在住子持ちプログラマー奮闘記 - Author:まさる(高野 将、TAKANO Sho)

目次

Blog 利用状況

ニュース

著書

2010/7発売


Web掲載記事

@IT

.NET開発を始めるVB6プログラマーが知るべき9のこと

CodeZine

実例で学ぶASP.NET Webフォーム業務アプリケーション開発のポイント

第1回 3層データバインドを正しく活用しよう(前編)

ブログパーツ


書庫

日記カテゴリ

コミュニティ

わんくまサイトでSQL Server Compact 4.0を使いたい ~その3~

中さんからtwitterで

@masaru_b_cl web.config拝見しましたが、参照設定がされてませんね

http://twitter.com/#!/nakawankuma/status/30183277180489729

と指摘されたので、いろいろと見直して、コンパイルエラーが解消し、実行時エラーとなるところまで持って行けたのでメモ。

 

プロジェクトの構成

次のような形になります。

image

ポイント:

  • App_DataフォルダにSQL Server Compact DBファイル(*.sdf)を配置する。
  • Binフォルダの配下に、SQL Server Compact 4.0のインストールフォルダのPrivateフォルダ(%ProgramFiles%\Microsoft SQL Server Compact Edition\v4.0\Private)にある、以下のファイルをコピーする。
    • System.Data.SqlServerCe.dll
    • JA\System.Data.SqlServerCe.resources.dll(※エラーメッセージなどの日本語化のため。無理していれなくてもいい)
    • x86フォルダの中身全部
      • SQL Server Compact 4.0のランタイム?

 

Web構成ファイル(Web.config)の設定

次にweb.configに次のように記述します。

<?xml version="1.0"?>
<configuration>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0"/>
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe"/>
    </DbProviderFactories>
  </system.data>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.SqlServerCe"/>
      </assemblies>
    </compilation>
    <customErrors mode="Off" />
  </system.web>
</configuration>

ポイント:

  • system.web ? compilation ? assemblies要素の中に、add要素でSQL Server Compact 4.0のアセンブリ名を追加する。
    • Version, Culture, PublicKeyTokenを指定すると、GACを探しに行ってしまうのか、どうもうまくいかなかったので、アセンブリ名のみとしています。
  • DbProviderFactoriesのtypeNameには、上記addで指定したアセンブリ名と同じ名前を指定する。

 

Webページ

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
  </div>
  </form>
</body>
</html>

Default.aspx.vb

Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlServerCe

Partial Class _Default
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim factory = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0")
    Using conn = factory.CreateConnection()
      conn.ConnectionString = "Data Source=|DataDirectory|\Database.sdf"
      Using da = factory.CreateDataAdapter()
        Using cmd = factory.CreateCommand()
          cmd.Connection = conn
          cmd.CommandText = "select 1"
          da.SelectCommand = cmd

          Dim dt As New DataTable()

          da.Fill(dt)

          GridView1.DataSource = dt
        End Using
      End Using
    End Using

    Page.DataBind()

  End Sub
End Class

ポイント:

  • Imports句で、「System.Data.SqlServerCe」名前空間をインポートする。
  • DbProviderFactoryのコンストラクタで、web.configで記述した”System.Data.SqlServerCe.4.0”を指定する。
  • ConnectionStringは"Data Source=|DataDirectory|\Database.sdf"

実行

ローカルIISに配置したら問題なく動作して、次のような画面が表示されました。

image

 

うまくいったので、わんくまサイトにFTPでコピーして動作させてみたところ、次のような実行時エラーとなりました。

image

 

「Access to the database file is not allowed.」なので、アクセス権の問題かなーと思うんですが、FTPの属性変更では変更できないようで、ここで行き詰まってしまいました。

 

とりあえず、大分前進した感じですが、どげすんべやといったところです。

投稿日時 : 2011年1月26日 23:55

Feedback

# re: わんくまサイトでSQL Server Compact 4.0を使いたい ~その3~ 2011/02/01 13:55 中博俊

通常外から見えるところにmdfファイル置いて書き込みOKのような形にはしないので、どこか別の所に別の方法でデプロイが現実的なんですよね。
ちょっとどうするか私と初音さん宛にメールで相談しましょうか・・・

# re: わんくまサイトでSQL Server Compact 4.0を使いたい ~その3~ 2011/02/01 14:25 まさる

今すぐにSQLCE4.0を使えないと困る、というわけではないので、必要になったときにdeploy方法を相談させてください。

今回は、どのようにランタイム、アセンブリを配置したらSQLCE4.0を動作させることができるかわかっただけでも収穫ですので。

# re: わんくまサイトでSQL Server Compact 4.0を使いたい ~その3~ 2011/04/15 16:09 まりん

環境違うけど、Windows Azure で SQL Server Compact 4.0 を動かすことができました。
反則技なので、発行のたびやる必要ありますけど。。。

・Azureにリモートデスクトップでログイン。
・SQL Server Compactインストーラ(SSCERuntime_x64-JPN.exe)を実行。
・以下処理を行うスタンドプログラムを作って、Azureで実行。
{{
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
FileSecurity dbSec = File.GetAccessControl(dbFile);
dbSec.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow));
File.SetAccessControl(dbFile, dbSec);
}}

# re: わんくまサイトでSQL Server Compact 4.0を使いたい ~その3~ 2011/04/15 16:10 まりん

環境違うけど、Windows Azure で SQL Server Compact 4.0 を動かすことができました。
反則技なので、発行のたびやる必要ありますけど。。。

・Azureにリモートデスクトップでログイン。
・SQL Server Compactインストーラ(SSCERuntime_x64-JPN.exe)を実行。
・以下処理を行うスタンドプログラムを作って、Azureで実行。
{{
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
FileSecurity dbSec = File.GetAccessControl(dbFile);
dbSec.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow));
File.SetAccessControl(dbFile, dbSec);
}}

# What's up friends, good paragraph and good urging commented at this place, I am in fact enjoying by these. 2019/05/12 14:00 What's up friends, good paragraph and good urging

What's up friends, good paragraph and good urging commented at this place,
I am in fact enjoying by these.

# It's actually very difficult in this active life to listen news on Television, so I just use web for that reason, and take the newest news. 2019/05/15 20:59 It's actually very difficult in this active life t

It's actually very difficult in this active life to listen news on Television, so I just
use web for that reason, and take the newest
news.

# Definitely believe that which you stated. Your favorite reason seemed to be on the internet the easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they just do not know about. You managed to hit the 2019/09/02 0:49 Definitely believe that which you stated. Your fav

Definitely believe that which you stated. Your favorite reason seemed to
be on the internet the easiest thing to be aware of.
I say to you, I certainly get annoyed while people think about worries that they just do not
know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take
a signal. Will likely be back to get more. Thanks

タイトル
名前
Url
コメント