LoginSignup
3
6

More than 5 years have passed since last update.

ASP.net MVC + VB.net Oracle

Last updated at Posted at 2015-12-21

概要

ASP.net MVC5 実践プログラミングの書籍を勉強してて、VBで記述する場合どうやるの?
Oracleへの接続はどうやるの?って疑問に思って。忘れないように。

環境

Windows7 SP1 32bit
Oracle11g
Visual Studio2013 Professional
IE11

前提

データベース上にテーブルが作成されていること

Members

DDL
CREATE TABLE MEMBERS 
(   
    "ID" NUMBER NOT NULL, 
    "NAME" VARCHAR2(30 BYTE), 
    "EMAIL" VARCHAR2(80 BYTE), 
    "BIRTH" DATE, 
    "MARRIED" CHAR(1 BYTE), 
    "MEMO" VARCHAR2(120 BYTE)
)

Insertは省く

手順

※基本的には本に従って進める(Chapter2)
1.プロジェクトを作成 + ビルド プロジェクト名:WebApplication7

2.Modelsフォルダに 新規EDM(ASP.NET Entity Data Model) を追加 + ビルド
モデル名:MembersModel Entities名:ModelEntities
(データベースから EF Designer)

3.Controllesフォルダに コントロール追加 + ビルド コントローラ名:BeginController
(MVC5 コントローラ名(空))

4.BeginController.vbに以下を追記

BeginController.vb
Public Class BeginController
Inherits Controller
Private db As New ModelEntities  ’この行
Function Index() As ActionResult
Return View(db.MEMBERS.ToList()) ’この行
End Function

5.Index()メソッドを右クリックして ビューの追加 + ビルド ビュー名:Index 他デフォルト

6.Index.vbhtmlに以下を追記

Index.vbhtml
@ModelType IEnumerable(Of MEMBERS)
@Code
ViewData("Title") = "Index"
End Code
<h2>Index</h2>

以下、全部追記

Index.vbhtml
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.NAME)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.EMAIL)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.BIRTH)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.MARRIED)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.MEMO)
        </th>
    </tr>
    @For Each item In Model
        @<tr>
            <td>
                @Html.DisplayFor(Function(modelItem) item.NAME)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) item.EMAIL)
            </td>
             <td>
                 @Html.DisplayFor(Function(modelItem) item.BIRTH)
             </td>
             <td>
                 @Html.DisplayFor(Function(modelItem) item.MARRIED)
             </td>
             <td>
                 @Html.DisplayFor(Function(modelItem) item.MEMO)
             </td>
        </tr>
    Next
</table>

7.実行

8.結果
結果.png

9.最後に
Scaffolding機能で 追加・編集・削除もできる。この本は 僕にとって とても役にたつ!

10.履歴
2016/10/07 コード部分をMarkdown記法に合わせて修正。

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