본문 바로가기

   
Programming/ADO.NET

Winform도서관

반응형





using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

using System.Data;

using System.Data.SqlClient;

 

namespace WInform

{

       public partial class ex05 : Form

       {

             public ex05()

             {

                    InitializeComponent();

             }

 

             private void ex05_Load(object sender, EventArgs e)

             {

                    //전체 목록 -> ListView에 추가

                    con.Open();

 

                    SqlDataReader reader = cmdSelect.ExecuteReader();//전체 레코드를 가져옴

 

                    while (reader.Read())

                    {

                           //테이블의 레코드 1 -> ListView Item 1

                          ListViewItem item = new ListViewItem(reader["title_id"].ToString());

 

                           item.SubItems.Add(reader["title"].ToString());

 

                           listView1.Items.Add(item);

                    }

 

                    reader.Close();

 

             }

 

             private void ex05_FormClosed(object sender, FormClosedEventArgs e)

             {

                    //DB 종료

                    con.Close();

             }

 

             private void listView1_SelectedIndexChanged(object sender, EventArgs e)

             {

                    //책중에 1권을 선택 -> 상세보기

                    // 2 -> 4

                    // 2 -> -1 -> 4 (리스트뷰 속성의 특징)

                    if (listView1.SelectedItems.Count > 0)//필수로 이 if를 넣어주어야 한다.

                    {

                           string title_id = listView1.SelectedItems[0].Text;

 

                           //MessageBox.Show(title_id);

                          

                           //조건부 셀렉트 -> cmdSelectOne

                           cmdSelectOne.Parameters["@title_id"].Value = title_id;//조건 완료

                          

                           //실행

                           SqlDataReader reader = cmdSelectOne.ExecuteReader();

 

 

                           if (reader.Read())

                           {

                                 //reader -> 선택한 책에 커서를 위치

                                 labelTitle.Text = reader["title"].ToString();

                                 labelTitleID.Text = reader["title_id"].ToString();

                                 labelType.Text = reader["type"].ToString();

                                 labelPrice.Text = string.Format("${0:N2}", float.Parse(reader["가격"].ToString()));

                                 labelPubdate.Text = ((DateTime)reader["pubDate"]).ToShortDateString();

                                 richTextBox1.Text = reader["notes"].ToString();

 

                                 //책표지

                                 Bitmap img = new Bitmap(@"images\Title-" + reader["title_id"].ToString() + ".gif");

                                 pictureBox1.Image = img;

                           }

 

                           reader.Close();

 

                    }

             }

       }

}

 

 


반응형