간단히 ID, Password를 입력받는 윈폼을 만들고 오라클 DB를 이용하여 로그인을 하는 예제를 만들어 보자. 로그인 성공 시 모달창으로 빈 윈도우 하나 띄움...
먼저 윈도우 폼 Application 프로젝트(이름:Login)를 하나 생성 후 아래처럼 파일을 만들어 보자. 생성되는 윈도우폼의 이름을 FrmLogin.cs로 변경을 하자.
1. 로그파일 생성을 위한 Log.cs 클래스 파일을 추가(오류로그 남기기 위해)
using System;
using System.IO;
namespace Login
{
class Log
{
//C드라이브에dotnet폴더를만들자.
public static void WriteLine(string name, string e)
{
string filename = @"c:\dotnet\" + name;
string logtime = DateTime.Now.ToString();
FileStream aFile = new FileStream(filename, FileMode.Append);
StreamWriter aWriter = new StreamWriter(aFile, System.Text.Encoding.Default);
aWriter.WriteLine("[" + logtime + "] ");
aWriter.WriteLine(e);
aWriter.Flush();
aWriter.Close();
}
}
}
2. 공통 DB용 클래스(Program.cs)
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlTypes;
using System.Windows.Forms;
using System.Collections;
namespace Login
{
public class Common_DB
{
// DataBaseConnection
public static OleDbConnection DBConnection()
{
OleDbConnection Conn;
//아래는오라클용접속문자열, data source 애는tnsnames.ora 파일에있는Alias명을넣으면됩니다.
string ConStr = ("Provider=MSDAORA;data source=onj;User ID=scott;Password=tiger");
Conn = new OleDbConnection(ConStr);
return Conn;
}
// DataSelect
public static OleDbDataReader DataSelect(string sql, OleDbConnection Conn)
{
try
{
OleDbCommand myCommand = new OleDbCommand(sql, Conn);
return myCommand.ExecuteReader();
}
catch (Exception ex)
{
//Log File에출력
Log.WriteLine("FrmLogin", ex.ToString());
MessageBox.Show(sql + "\n" + ex.Message, "DataSelect");
return null;
}
finally { }
}
}
}
3. FrmLogin.cs
[C#코드]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Login
{
public partial class FrmLogin : Form
{
private OleDbConnection LocalConn;
public FrmLogin()
{
InitializeComponent();
}
static void Main() { Application.Run(new FrmLogin()); }
private void button1_Click(object sender, EventArgs e)
{
OleDbDataReader myReader;
string sql = null;
try
{
LocalConn = Common_DB.DBConnection();
LocalConn.Open();
if (txtID.Text == "" || txtPWD.Text == "")
{
MessageBox.Show("ID 또는Password를입력하세요...");
return;
}
sql = "select pwd from member ";
sql += " where id = " + "'" + txtID.Text + "'";
myReader = Common_DB.DataSelect(sql, LocalConn);
if (myReader.Read())
{
if (txtPWD.Text != myReader["pwd"].ToString())
{
MessageBox.Show("Password가맞지않습니다...");
return;
}
}
else
{
MessageBox.Show("등록되지않은ID 입니다.");
return;
}
//-----------ID가PWD가맞는경우
Form f = new Form();
f.Text = "로그인OK";
f.ShowDialog();
}
catch (Exception e1)
{
Log.WriteLine("FrmLogin", e1.ToString());
MessageBox.Show(e1.ToString() + sql, "FrmLogin :: 로그인오류!");
}
}
//패스워드를입력하고엔터키를입력해도로그인디되도록
private void txtPWD_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(sender, e);
}
}
}
}
[디자인 코드]
namespace Login
{
partial class FrmLogin
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtID = new System.Windows.Forms.TextBox();
this.txtPWD = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(48, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(16, 12);
this.label1.TabIndex = 0;
this.label1.Text = "ID";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(48, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(31, 12);
this.label2.TabIndex = 1;
this.label2.Text = "PWD";
//
// txtID
//
this.txtID.Location = new System.Drawing.Point(103, 13);
this.txtID.Name = "txtID";
this.txtID.Size = new System.Drawing.Size(179, 21);
this.txtID.TabIndex = 2;
//
// txtPWD
//
this.txtPWD.Location = new System.Drawing.Point(103, 47);
this.txtPWD.Name = "txtPWD";
this.txtPWD.Size = new System.Drawing.Size(179, 21);
this.txtPWD.TabIndex = 3;
this.txtPWD.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtPWD_KeyUp);
//
// button1
//
this.button1.Location = new System.Drawing.Point(321, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(86, 47);
this.button1.TabIndex = 4;
this.button1.Text = "로그인";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// FrmLogin
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(448, 105);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtPWD);
this.Controls.Add(this.txtID);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "FrmLogin";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtID;
private System.Windows.Forms.TextBox txtPWD;
private System.Windows.Forms.Button button1;
}
}
댓글 없음:
댓글 쓰기