[개발/C#] - [C#] 1. ASP .NET MVC 프로젝트 생성에 이어
.NET Framework 4.7.2 MVC 패턴을 사용한 ASP WebApplication에서 MSSQL을 연결 해보겠습니다.
Controller -> Service -> Dao -> DBHelper 순으로 호출이 이어집니다.
역순으로 만들어보겠습니다.
Model -> 우클릭 -> 추가 -> 새 폴더(D) -> Common, Dao, Service 폴더 추가
Common -> 우클릭 -> 추가 -> 새 항목(W)... -> DBHelper.cs 클래스 추가/편집
using System.Data.SqlClient; //추가
namespace WebApplication1.Models.Common
{
public class DBHelper
{
private static string SERVER = "<000.000.000.000>";
private static string UID = "<로그인>";
private static string PWD = "<암호>";
private static string DATABASE = "<데이터베이스 명>";
private string CONNECTIONSTRING = $"server={SERVER};uid={UID};pwd={PWD};database={DATABASE};";
private SqlConnection connection { get; set; }
public SqlConnection GetConnection()
{
if (connection != null)
{
return connection;
}
else
{
return new SqlConnection(CONNECTIONSTRING);
}
}
}
}
Dao -> SampleDao.cs 클래스 추가/편집
using Dapper;
using System.Data;
using System.Linq;
using WebApplication1.Models.Common;
namespace WebApplication1.Models.Dao
{
public class SampleDao: DBHelper
{
public string GetDbVersion()
{
string rtnVal = null;
using(var conn = base.GetConnection())
{
string sql = "SELECT @@VERSION";
var p = new DynamicParameters(); // 'Dapper' 패키지 설치
conn.Open();
rtnVal = conn.Query<string>(sql, p, commandType: CommandType.Text).FirstOrDefault();
}
return rtnVal;
}
}
}
Service -> SampleService.cs 클래스 추가/편집
using WebApplication1.Models.Dao;
namespace WebApplication1.Models.Service
{
public class SampleService
{
private SampleDao sampleDao = new SampleDao();
public string GetDbVersion()
{
return sampleDao.GetDbVersion();
}
}
}
Controller -> HomeController.cs 편집
using System.Web.Mvc;
using WebApplication1.Models.Service;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
SampleService _sampleService = new SampleService();
public ActionResult Index()
{
object rtnVal = _sampleService.GetDbVersion();
return View(rtnVal);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Views>Home -> Index.cshtml 편집 // @Model 추가
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more »</a></p>
<p>@Model</p>
</div>
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p>
</div>
</div>
DB 버전 정보 출력
[ASP .NET] razor Url parameter 값 사용하기 (0) | 2021.11.24 |
---|---|
[ASP .NET] C# List<string>을 javascript Array로 변환 (0) | 2021.09.27 |
[ASP .NET] 1. ASP .NET Framework MVC 프로젝트 생성 (0) | 2021.09.16 |
[ASP .NET] @Scripts.Render("~/bundles/bootstrap") NullReferenceException (1) | 2021.08.31 |
[ASP .NET] HttpServerUtility.MapPath, 실제 파일 경로 반환 (0) | 2021.08.19 |
댓글 영역