LoginSignup
11

More than 5 years have passed since last update.

SQL Server 2016 Express LocalDBインストール

Last updated at Posted at 2017-03-27

Visual Studioなど開発環境がインストールされていないPCで、SQL Server 2016 Express LocalDBを使ったアプリケーションを実行できるようにしてみました。

SQL Server LocalDBを使ったアプリケーションの開発方法は以下を参考にしてください。
http://qiita.com/akabei/items/3412ed032ca325c168a0

環境

Windows 7 Professional SP1 64bit
.NET Framework 4.6.1

※今回はWindows7で問題なく動作しましたが、SQL Server 2016からWindows7はサポート外となったようです。
https://docs.microsoft.com/ja-jp/sql/sql-server/install/hardware-and-software-requirements-for-installing-sql-server

LocalDBインストーラーダウンロード

以下の公式サイトから「無料でSQL Server 2016 Expressを試す」をクリックしてインストーラーをダウンロードします。

SQL Server 2016 SP1 Express エディション
https://www.microsoft.com/ja-jp/sql-server/sql-server-editions-express

fff.png

ダウンロードしたファイル(SQLServer2016-SSEI-Expr.exe)を実行します。

aaa.png

ダウンロードするパッケージの選択で「LocalDB(44MB)」を選択してインストーラーをダウンロードします。

LocalDBインストール

ダウンロードしたインストーラー(SqlLocalDB.msi)を実行します。

bbb.png

あとはインストーラーの指示にしたがってインストールします。

アプリケーション実行

LocalDBがインストールできたらアプリケーションを実行します。
が、エラーが発生・・・!!

ggg.png

LocalDBインスタンス確認

コマンドプロンプトからsqllocaldbコマンドを使ってLocalDBのインスタンスを確認します。

コマンドプロンプト
>sqllocaldb info
MSSQLLocalDB

プログラムを作成した環境のLocalDBインスタンスは「ProjectsV13」だったのでそれが原因のようです。

接続文字列変更

アプリケーション設定ファイル(XXXXX.exe.config)をメモ帳などで開いて接続文字列を以下のように修正します。

Data Source=(localdb)\ProjectsV13;
            ↓
Data Source=(localdb)\MSSQLLocalDB;

WindowsApp1.exe.config(修正前)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsApp1.My.MySettings.SampleConnectionString" connectionString="Data Source=(localdb)\ProjectsV13;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
</configuration>
WindowsApp1.exe.config(修正後)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsApp1.My.MySettings.SampleConnectionString" connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Sample.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
    </startup>
</configuration>

アプリケーション再実行

もう一度アプリケーションを実行します。

hh.png

動いた!!!

まとめ

.NET Frameworkがインストールされている環境でも別途LocalDBをインストールする必要があるようです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11