ตัวอย่างนี้จะประยุกต์ใช้กับ Form Login มีทั้ง VB.NET และ C#
frmLogin.vb
Imports System.Data.OracleClient
Imports System.Data
Public Class frmLogin
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim objConn As New OracleConnection
Dim objCmd As New OracleCommand
Dim strConnString, strSQL As String
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;"
objConn.ConnectionString = strConnString
objConn.Open()
Dim intNumRows As Integer
strSQL = "SELECT COUNT(*) FROM MEMBER WHERE USERNAME = '" & Me.txtUsername.Text & "' AND PASSWORD = '" & Me.txtPassword.Text & "' "
objCmd = New OracleCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
If intNumRows > 0 Then
Dim frm As New frmMain
frm._strUser = Me.txtUsername.Text
frm.LoadInfor()
frm.Show()
Me.Hide()
Else
MessageBox.Show("Username or Password Incorrect")
End If
objConn.Close()
objConn = Nothing
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
If MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Application.Exit()
End If
End Sub
End Class
frmMenu.vb
Imports System.Data.OracleClient
Imports System.Data
Public Class frmMain
Dim strUser As String
Public Property _strUser() As String
Get
Return strUser
End Get
Set(ByVal value As String)
strUser = value
End Set
End Property
Public Sub LoadInfor()
Dim objConn As New OracleConnection
Dim objCmd As New OracleCommand
Dim dtAdapter As OracleDataAdapter
Dim dt As New DataTable
Dim strConnString, strSQL As String
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;"
objConn.ConnectionString = strConnString
objConn.Open()
strSQL = "SELECT * FROM MEMBER WHERE USERNAME = '" & strUser & "' "
dtAdapter = New OracleDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
Me.lblUser.Text = strUser
Me.lblName.Text = dt.Rows(0)("NAME")
Me.lblEmail.Text = dt.Rows(0)("EMAIL")
End If
objConn.Close()
objConn = Nothing
End Sub
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim frm As New frmLogin
frm.Show()
Me.Hide()
End Sub
End Class
Code สำหรับภาษา C#
frmLogin.cs
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.OracleClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
OracleConnection objConn = new OracleConnection();
OracleCommand objCmd = new OracleCommand();
string strConnString = null;
string strSQL = null;
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;";
objConn.ConnectionString = strConnString;
objConn.Open();
int intNumRows = 0;
strSQL = "SELECT COUNT(*) FROM MEMBER WHERE USERNAME = '" + this.txtUsername.Text + "' AND PASSWORD = '" + this.txtPassword.Text + "' ";
objCmd = new OracleCommand(strSQL, objConn);
intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());
if (intNumRows > 0)
{
frmMain frm = new frmMain();
frm._strUser = this.txtUsername.Text;
frm.LoadInfor();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("Username or Password Incorrect");
}
objConn.Close();
objConn = null;
}
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
frmMenu.cs
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.OracleClient;
using System.IO;
namespace LoginFormCS
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
string strUser;
public string _strUser
{
get { return strUser; }
set { strUser = value; }
}
public void LoadInfor()
{
OracleConnection objConn = new OracleConnection();
OracleCommand objCmd = new OracleCommand();
OracleDataAdapter dtAdapter = default(OracleDataAdapter);
DataTable dt = new DataTable();
string strConnString = null;
string strSQL = null;
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;";
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = "SELECT * FROM MEMBER WHERE USERNAME = '" + strUser + "' ";
dtAdapter = new OracleDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
this.lblUser.Text = strUser;
this.lblName.Text = dt.Rows[0]["NAME"].ToString();
this.lblEmail.Text = dt.Rows[0]["EMAIL"].ToString();
}
objConn.Close();
objConn = null;
}
private void btnBack_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
frm.Show();
this.Hide();
}
}
}