using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication3
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class regFrm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid sqlGrid;
		private System.Windows.Forms.TextBox idBx;
		private System.Windows.Forms.TextBox passBx;
		private System.Windows.Forms.TextBox nameBx;
		private System.Windows.Forms.TextBox snoBx;
		private System.Windows.Forms.Button insertBtn;
		private System.Windows.Forms.GroupBox inputGp;
		private System.Windows.Forms.Label idLbl;
		private System.Windows.Forms.Label passLbl;
		private System.Windows.Forms.Label nameLbl;
		private System.Windows.Forms.Label snoLbl;
		private System.Windows.Forms.Label idxLbl;
		private System.Windows.Forms.TextBox idxBx;
		private System.Windows.Forms.Button deleteBtn;
		private System.Windows.Forms.GroupBox deleteGp;

		private SqlConnection sqlConn;
		private SqlDataAdapter sqlAdp;
		private SqlCommand sqlCmd;
		private DataSet ds = new DataSet();

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public regFrm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.sqlGrid = new System.Windows.Forms.DataGrid();
			this.idBx = new System.Windows.Forms.TextBox();
			this.passBx = new System.Windows.Forms.TextBox();
			this.nameBx = new System.Windows.Forms.TextBox();
			this.snoBx = new System.Windows.Forms.TextBox();
			this.insertBtn = new System.Windows.Forms.Button();
			this.inputGp = new System.Windows.Forms.GroupBox();
			this.snoLbl = new System.Windows.Forms.Label();
			this.nameLbl = new System.Windows.Forms.Label();
			this.passLbl = new System.Windows.Forms.Label();
			this.idLbl = new System.Windows.Forms.Label();
			this.idxLbl = new System.Windows.Forms.Label();
			this.idxBx = new System.Windows.Forms.TextBox();
			this.deleteBtn = new System.Windows.Forms.Button();
			this.deleteGp = new System.Windows.Forms.GroupBox();
			((System.ComponentModel.ISupportInitialize)(this.sqlGrid)).BeginInit();
			this.inputGp.SuspendLayout();
			this.SuspendLayout();
			// 
			// sqlGrid
			// 
			this.sqlGrid.DataMember = "";
			this.sqlGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.sqlGrid.Location = new System.Drawing.Point(16, 16);
			this.sqlGrid.Name = "sqlGrid";
			this.sqlGrid.Size = new System.Drawing.Size(552, 256);
			this.sqlGrid.TabIndex = 0;
			this.sqlGrid.CurrentCellChanged += new System.EventHandler(this.sqlGrid_CurrentCellChanged);
			// 
			// idBx
			// 
			this.idBx.Location = new System.Drawing.Point(136, 320);
			this.idBx.MaxLength = 10;
			this.idBx.Name = "idBx";
			this.idBx.Size = new System.Drawing.Size(176, 20);
			this.idBx.TabIndex = 1;
			this.idBx.Text = "";
			// 
			// passBx
			// 
			this.passBx.Location = new System.Drawing.Point(136, 360);
			this.passBx.MaxLength = 10;
			this.passBx.Name = "passBx";
			this.passBx.PasswordChar = '*';
			this.passBx.Size = new System.Drawing.Size(176, 20);
			this.passBx.TabIndex = 1;
			this.passBx.Text = "";
			// 
			// nameBx
			// 
			this.nameBx.Location = new System.Drawing.Point(136, 400);
			this.nameBx.MaxLength = 4;
			this.nameBx.Name = "nameBx";
			this.nameBx.Size = new System.Drawing.Size(176, 20);
			this.nameBx.TabIndex = 1;
			this.nameBx.Text = "";
			// 
			// snoBx
			// 
			this.snoBx.Location = new System.Drawing.Point(136, 440);
			this.snoBx.MaxLength = 13;
			this.snoBx.Name = "snoBx";
			this.snoBx.Size = new System.Drawing.Size(176, 20);
			this.snoBx.TabIndex = 1;
			this.snoBx.Text = "";
			// 
			// insertBtn
			// 
			this.insertBtn.Location = new System.Drawing.Point(360, 328);
			this.insertBtn.Name = "insertBtn";
			this.insertBtn.Size = new System.Drawing.Size(160, 136);
			this.insertBtn.TabIndex = 2;
			this.insertBtn.Text = "입 력";
			this.insertBtn.Click += new System.EventHandler(this.insertBtn_Click);
			// 
			// inputGp
			// 
			this.inputGp.Controls.AddRange(new System.Windows.Forms.Control[] {
								
								
				  this.snoLbl,
								
								
				  this.nameLbl,
								
								
				  this.passLbl,
								
								
				  this.idLbl});
			this.inputGp.Location = new System.Drawing.Point(24, 288);
			this.inputGp.Name = "inputGp";
			this.inputGp.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.inputGp.Size = new System.Drawing.Size(536, 200);
			this.inputGp.TabIndex = 3;
			this.inputGp.TabStop = false;
			this.inputGp.Text = "신상정보 입력";
			// 
			// snoLbl
			// 
			this.snoLbl.AutoSize = true;
			this.snoLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.snoLbl.Location = new System.Drawing.Point(40, 152);
			this.snoLbl.Name = "snoLbl";
			this.snoLbl.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.snoLbl.Size = new System.Drawing.Size(59, 14);
			this.snoLbl.TabIndex = 3;
			this.snoLbl.Text = "주민번호";
			this.snoLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// nameLbl
			// 
			this.nameLbl.AutoSize = true;
			this.nameLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.nameLbl.Location = new System.Drawing.Point(64, 112);
			this.nameLbl.Name = "nameLbl";
			this.nameLbl.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.nameLbl.Size = new System.Drawing.Size(32, 14);
			this.nameLbl.TabIndex = 2;
			this.nameLbl.Text = "이름";
			this.nameLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// passLbl
			// 
			this.passLbl.AutoSize = true;
			this.passLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.passLbl.Location = new System.Drawing.Point(40, 72);
			this.passLbl.Name = "passLbl";
			this.passLbl.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.passLbl.Size = new System.Drawing.Size(59, 14);
			this.passLbl.TabIndex = 1;
			this.passLbl.Text = "비밀번호";
			this.passLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// idLbl
			// 
			this.idLbl.AutoSize = true;
			this.idLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.idLbl.Location = new System.Drawing.Point(80, 32);
			this.idLbl.Name = "idLbl";
			this.idLbl.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.idLbl.Size = new System.Drawing.Size(17, 14);
			this.idLbl.TabIndex = 0;
			this.idLbl.Text = "ID";
			this.idLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// idxLbl
			// 
			this.idxLbl.AutoSize = true;
			this.idxLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.idxLbl.Location = new System.Drawing.Point(64, 560);
			this.idxLbl.Name = "idxLbl";
			this.idxLbl.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.idxLbl.Size = new System.Drawing.Size(46, 14);
			this.idxLbl.TabIndex = 3;
			this.idxLbl.Text = "고유키";
			this.idxLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// idxBx
			// 
			this.idxBx.Location = new System.Drawing.Point(136, 560);
			this.idxBx.Name = "idxBx";
			this.idxBx.Size = new System.Drawing.Size(176, 20);
			this.idxBx.TabIndex = 1;
			this.idxBx.Text = "";
			// 
			// deleteBtn
			// 
			this.deleteBtn.Location = new System.Drawing.Point(368, 552);
			this.deleteBtn.Name = "deleteBtn";
			this.deleteBtn.Size = new System.Drawing.Size(152, 32);
			this.deleteBtn.TabIndex = 4;
			this.deleteBtn.Text = "삭 제";
			this.deleteBtn.Click += new System.EventHandler(this.deleteBtn_Click);
			// 
			// deleteGp
			// 
			this.deleteGp.Location = new System.Drawing.Point(24, 520);
			this.deleteGp.Name = "deleteGp";
			this.deleteGp.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.deleteGp.Size = new System.Drawing.Size(536, 88);
			this.deleteGp.TabIndex = 5;
			this.deleteGp.TabStop = false;
			this.deleteGp.Text = "신상정보 삭제";
			// 
			// regFrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(584, 637);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
									
		  this.deleteBtn,
								
								
		  this.insertBtn,
								
								
		  this.idBx,
								
								
		  this.sqlGrid,
								
								
		  this.passBx,
								
								
		  this.nameBx,
								
								
		  this.snoBx,
								
								
		  this.inputGp,
								
								
		  this.idxLbl,
								
								
		  this.idxBx,
								
								
		  this.deleteGp});
			this.Name = "regFrm";
			this.Text = "신상정보";
			this.Load += new System.EventHandler(this.regFrm_Load);
			((System.ComponentModel.ISupportInitialize)(this.sqlGrid)).EndInit();
			this.inputGp.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new regFrm());
		}

		private void regFrm_Load(object sender, System.EventArgs e)
		{
			ViewDataGrid();		
		}

		private void ViewDataGrid()
		{
			ds.Clear();

			try
			{
				sqlConn = new SqlConnection("server=ns; database=csharp; uid=csharp; pwd=csharp;");
				sqlAdp = new SqlDataAdapter("select * from neo_members", sqlConn);

				sqlAdp.Fill(ds, "neo_members");
				sqlGrid.DataSource = ds.Tables["neo_members"].DefaultView;
			}
			catch(Exception E)
			{
				MessageBox.Show(E.Message);
			}
			finally
			{
				sqlConn.Close();
			}
		}

		private void insertBtn_Click(object sender, System.EventArgs e)
		{
			try
			{
				sqlCmd = new SqlCommand("insert neo_members (m_id, m_pass, m_name, m_sno) values ('"+ idBx.Text +"', '"+ passBx.Text +"', '"+ nameBx.Text +"', '"+ snoBx.Text +"')", sqlConn);
				sqlConn.Open();
				sqlCmd.ExecuteNonQuery();

				ViewDataGrid();
			}
			catch(Exception E)
			{
				MessageBox.Show(E.Message);
			}
			finally
			{
				sqlConn.Close();
				idBx.Text = null;
				passBx.Text = null;
				nameBx.Text = null;
				snoBx.Text = null;
			}
		}

		private void deleteBtn_Click(object sender, System.EventArgs e)
		{
			try
			{
				sqlCmd = new SqlCommand("delete from neo_members where m_idx = "+ idxBx.Text, sqlConn);
				sqlConn.Open();
				sqlCmd.ExecuteNonQuery();

				ViewDataGrid();
			}
			catch(Exception E)
			{
				MessageBox.Show(E.Message);
			}
			finally
			{
				sqlConn.Close();
				idxBx.Text = null;
			}
		}

		private void sqlGrid_CurrentCellChanged(object sender, System.EventArgs e)
		{
			idxBx.Text = sqlGrid[(sqlGrid.CurrentRowIndex), 0].ToString();
		}
	}
}