4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IISに配置したWebアプリからSQL Serverへ接続できなくなったときの対処

Posted at

背景

SQL Server 2019をインストールした

  • ここで「既定のインスタンス」と「名前付きインスタンス」を選択するところがある
  • ずっと「規定のインスタンス」かつLocalDBにして作業していた
  • IIS EXPRESSではうまくいく。だけど。IIS(本番)にしたらエラー続出:frowning2:
  • SQL Serverのエラーコードは主に5026
  • SSMSでログイン権限を変えてみたり、IIS ManagerでAppPoolのIDをいじってみたりしたけど、むりぽ。

SQL Serverを再インストールした

  • こんどは名前付きで SQLExpress
  • IISだとLocalDBにアクセスするときにコケてたから、なんとなくNetworkDBにあるほうがうまくいきそうな気がした
    SSMS-1.png

最後はできた!

VS2019のプロジェクトにSQLEXPRESSを追加し、PSでコマンドをうつ!

PoworShell
Add-Migration Initial
Update-Database

すると勝手に空だったtestDbにテーブルができている!

vs-db.png

appsettings.json
"ConnectionStrings": {
    "WorkContext": "Data Source=.\\SQLEXPRESS;Initial Catalog=testDb;Integrated Security=True"

    ずっと苦戦してたlocalDb時代のはこれ↓
    "WorkContext": "Server=(localdb)\\mssqllocaldb;Database=WorkContext-2;Trusted_Connection=True;"
  
}

データソースだとかサーバだとか、カタログだとか、、、同じものを違う言葉で言ってるだけなように見える。

web.config
  <connectionStrings>
    <add name="WorkContext" connectionString="Data Source=.\\SQLEXPRESS;Initial Catalog=testDb;Integrated Security=True" />
  </connectionStrings>

IIS配置後

app poop1.png

カスタムアカウントのところに、WindowsOSログイン時に使用したユーザIDとパスワードを入力

(逆にデータベースのほうにIISの資格を与えてもいいのかは、知らない。)
(推奨されなてないけどSQL認証のほうがデータベース接続っぽくてかっこいい)

まとめ

  • IISに配置するならlocalDBじゃなくてSQL EXPRESSにする
  • Windows認証ならIIS配置後にIDも変える

いろいろ無知なので、ご指導・ご鞭撻のほどよろしくお願いいたします。

おまけ

冒頭に書いたチュートリアル(InMemory)ではID重複をPUTするとStatus500が返ってたけど
データベースにしたらStatus409が返ってくるようになったから、フロントの方も変更しなくちゃならなかった:point_up:
omake.png

4
3
0

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?