appsettings.json에 db connection string을 적어두고 이를 가지고와 db connection을 맺어줄 때 사용되는 코드
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
IConfiguration configuration = builder.Build();
string defaultConnectionString = configuration.GetValue<string>("ConnectionStrings:DEVDB");
using (var conn = new SqlConnection(defaultConnectionString))
{
conn.Open();
using (var cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = @"
SELECT
M.M_USERID, M.M_USERNAME, M.M_REGDATE
, MD.MD_ADDRESS, MD.MD_EMAIL, ....., JOBCD.CDNAME AS JOBCDNAME
FROM
S_MEMBER AS M
INNER JOIN S_MEMBERDETAIL AS MD ON M.M_IDX = MD.M_IDX AND MD.MD_DEL = 0
INNER JOIN S_CODE AS JOBCD ON MD.MD_JOBCD = JOBCD.CDSUB
INNER JOIN S_CODE AS JOBSUBCD ON MD.MD_JOBSUBCD = JOBSUBCD.CDSUB
WHERE
M.M_IDX = @M_IDX
AND M.M_DEL = 0
";
cmd.Parameters.AddWithValue("@M_IDX", idx);
var reader = cmd.ExecuteReader();
reader.Read();
var member = new AdminMember();
member.Idx = idx;
member.UserId = (string)reader["M_USERID"];
member.UserName = (string)reader["M_USERNAME"];
......
member.DetailJobSubCd = (string)reader["MD_JOBSUBCD"];
member.DetailContent = (string)reader["MD_CONTENT"];
reader.Close();
return member;
}
return null;
}
'ASP.NET' 카테고리의 다른 글
쓰잘데기 없는 visual studio 2022 mouse wheel zoom in out 기능 날려버리기 (0) | 2022.12.29 |
---|---|
MVC 모델 비지니스 로직 결과를 json형태로 return해주기 (0) | 2022.12.19 |
appsettings.Development.json에 저장된 ConnectionStrings 값 가져오기 (0) | 2022.12.06 |
.net DB First 방식 Scaffold-DbContext 오류 대응 (0) | 2022.12.05 |