-
DATABASE 연동 ASP.NET, MS-SQLASP.NET 2020. 3. 11. 13:25반응형
SELECT
<script runat="server"> protected void Page_Load(object sender, EventArgs e) { string source = @"Server=192.168.10.155;uid=test_idino;pwd=idino@12;database=TEST_LJH"; SqlConnection conn = new SqlConnection(source); conn.Open(); String sql = "SELECT * FROM 직원"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader reader = cmd.ExecuteReader(); //페이지 보여주기 Response.Write("<Table border=1>"); while (reader.Read()) { Response.Write("<tr>"); for (int i = 0; i < reader.FieldCount; i++) { Response.Write("<td>" + reader[i] + "</td>"); } Response.Write("</tr>"); } Response.Write("</table>"); //DB 연결 해제 reader.Close(); conn.Close(); } </script>
INSERT
protected void Insert_Test(object sender, EventArgs e) { string source = @"Server=192.168.10.155;uid=test_idino;pwd=idino@12;database=TEST_LJH"; SqlConnection conn = new SqlConnection(source); conn.Open(); SqlCommand cmd = new SqlCommand(null, conn); cmd.CommandText = "INSERT INTO 회원관리 " + "(성명,주민번호,성별,이메일,전화번호,우편번호,주소,부서,직위) "+ "VALUES" + "(@성명,@주민번호,@성별,@이메일,@전화번호,@우편번호,@주소,@부서,@직위)"; //SqlParameter 객체 생성 SqlParameter 성명파라메터 = new SqlParameter("@성명", System.Data.SqlDbType.Text, 100); SqlParameter 주민번호파라메터 = new SqlParameter("@주민번호", System.Data.SqlDbType.Text, 100); SqlParameter 성별파라메터 = new SqlParameter("@주민번호", System.Data.SqlDbType.Text, 100); SqlParameter 이메일파라메터 = new SqlParameter("@이메일", System.Data.SqlDbType.Text, 100); SqlParameter 전화번호파라메터 = new SqlParameter("@전화번호", System.Data.SqlDbType.Text, 100); SqlParameter 우편번호파라메터 = new SqlParameter("@우편번호", System.Data.SqlDbType.Text, 100); SqlParameter 주소파라메터 = new SqlParameter("@주소", System.Data.SqlDbType.Text, 100); SqlParameter 부서파라메터 = new SqlParameter("@부서", System.Data.SqlDbType.Text, 100); SqlParameter 직위파라메터 = new SqlParameter("@직위", System.Data.SqlDbType.Text, 100); //생성된 파라메터에 데이터 입력하기 성명파라메터.Value = 성명.Text; 주민번호파라메터.Value = 주민번호앞.Text+주민번호뒤.Text; 성별파라메터.Value = 주민번호뒤.Text.Substring(0,1); 이메일파라메터.Value = 이메일.Text; 전화번호파라메터.Value = 전화번호.Text; 우편번호파라메터.Value = 우편번호.Text; 주소파라메터.Value = 주소.Text; 부서파라메터.Value = 부서.Text; 직위파라메터.Value = 직위.Text; //파라메터 값 대입?? cmd.Parameters.Add(성명파라메터); cmd.Parameters.Add(주민번호파라메터); cmd.Parameters.Add(성별파라메터); cmd.Parameters.Add(이메일파라메터); cmd.Parameters.Add(전화번호파라메터); cmd.Parameters.Add(우편번호파라메터); cmd.Parameters.Add(주소파라메터); cmd.Parameters.Add(부서파라메터); cmd.Parameters.Add(직위파라메터); cmd.Prepare(); cmd.ExecuteNonQuery(); //DB 연결 해제 conn.Close(); }
데이터 입력 폼
보노보노테이블
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- Popper JS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> </head> <body> <style> .mine th{ text-align: center; } </style> <div style="float: left;"> <table class="table table-bordered mine" style="width: 500px; height: auto;" > <tr> <th style="width: 95px; text-align: center;">사번</th> <td colspan="3"></td> </tr> <tr> <th>성명</th> <td style="width: 155px;"></td> <th style="width: 95px;">주민번호</th> <td style="width: 155px;"></td> </tr> <tr> <th>이메일</th> <td></td> <th>전화번호</th> <td></td> </tr> <tr> <th>주소</th> <td></td> <td colspan="2"></td> </tr> <tr> <th>부서</th> <td></td> <th>직위</th> <td></td> </tr> </table> <div style="position: relative; left: 345px;"> <button>신규</button> <button>저장</button> <button>삭제</button> </div> </div> <div style="float: left; margin-left: 50px;"> <table class="table table-bordered" style="width: auto; height: auto; background-image: url(https://i.pinimg.com/474x/92/6f/f3/926ff3b17c5a0473ff7d5144636eb6c1.jpg); background-position: 50%; background-size: 120%; " > <tr> <th>사번</th> <th>사번</th> <th>사번</th> <th>사번</th> <th>사번</th> </tr> <tr> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> </tr> <tr> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> </tr> <tr> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> </tr> <tr> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> <td>가나다</td> </tr> </table> </div> </body> </html>
'ASP.NET' 카테고리의 다른 글
ASP.NET MVC 자습서(1) - 컨트롤러 추가 (0) 2020.03.20 [ASP.NET] 회원관리 폼 만들기 - 1 (0) 2020.03.18 ASP.NET에서 JavaScript로 C# 함수 호출하기 (0) 2020.03.16 GRIDVIEW.DataBind() (0) 2020.03.12 이메일 정규표현식 (0) 2020.03.12