Winform 建立權限選單
這個需求實作不難
只是準備比較多
以下說明做法:
1-SQL資料庫-建立資料表,記錄登入者、選單根目錄、選單、選單權限管理
2-Winform-登入頁跟選單主頁
當使用者登入後,進到選單主頁時,依使用者的權限設定相關的選單給使用者
記錄登入者資料表
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MNU_Login](
[UserName] [nvarchar](20) NULL,
[Password] [nvarchar](20) NULL
) ON [PRIMARY]
GO
選單根目錄資料表
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MNU_PARENT](
[ID] [nvarchar](20) NOT NULL,
[MAINMNU] [varchar](20) NULL,
[STATUS] [varchar](1) NULL,
[MENUPARVAL] [int] NULL,
CONSTRAINT [PK_MNU_PARENT] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
選單資料表
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MNU_SUBMENU](
[ID] [varchar](20) NOT NULL,
[MENUPARVAL] [int] NULL,
[FRM_CODE] [varchar](50) NULL,
[FRM_NAME] [varchar](20) NULL,
[STATUS] [varchar](1) NULL,
CONSTRAINT [PK_MNU_SUBMENU] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
選單權限管理資料表
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MNU_SUBMENULogin](
[UserName] [nvarchar](20) NOT NULL,
[FRM_CODE] [varchar](50) NOT NULL,
CONSTRAINT [PK_MNU_SUBMENULogin] PRIMARY KEY CLUSTERED
(
[UserName] ASC,
[FRM_CODE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
登入頁的頁面+code
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.SqlClient;
using System.Configuration;
using System.Reflection;
namespace TKQC
{
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
#region BUTTON
private void button1_Click(object sender, EventArgs e)
{
LOGIN();
}
#endregion
#region LOGIN
public void LOGIN()
{
if (txt_UserName.Text == "" || txt_Password.Text == "")
{
MessageBox.Show("請輸入帳號、密碼");
return;
}
try
{
//Create SqlConnection
String connectionString;
SqlConnection conn;
connectionString = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Select * from MNU_Login where UserName=@username and Password=@password", conn);
cmd.Parameters.AddWithValue("@username", txt_UserName.Text);
cmd.Parameters.AddWithValue("@password", txt_Password.Text);
conn.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
conn.Close();
int count = ds.Tables[0].Rows.Count;
//If count is equal to 1, than show frmMain form
if (count == 1)
{
//MessageBox.Show("登入成功!");
FrmParent fm = new FrmParent(txt_UserName.Text.ToString());
fm.Show();
this.Hide();
}
else
{
MessageBox.Show("登入失敗!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region FUNCTION
private void txt_Password_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
LOGIN();
}
}
#endregion
}
}
選單主頁的頁面+code
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.SqlClient;
using System.Configuration;
using System.Reflection;
using System.Diagnostics;
namespace TKQC
{
public partial class FrmParent : Form
{
SqlConnection conn;
MenuStrip MnuStrip;
ToolStripMenuItem MnuStripItem;
string UserName;
public FrmParent()
{
InitializeComponent();
}
public FrmParent(string txt_UserName)
{
InitializeComponent();
UserName = txt_UserName;
shareArea.UserName = UserName;
}
//private void InitializeComponent()
//{
// throw new NotImplementedException();
//}
private void FrmParent_Load(object sender, EventArgs e)
{
// To make this Form the Parent Form
this.IsMdiContainer = true;
//Creating object of MenuStrip class
MnuStrip = new MenuStrip();
//Placing the control to the Form
this.Controls.Add(MnuStrip);
String connectionString;
connectionString = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
conn = new SqlConnection(connectionString);
String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNU_PARENT";
SqlDataAdapter da = new SqlDataAdapter(Sequel, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
MnuStripItem = new ToolStripMenuItem(dr["MAINMNU"].ToString());
SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString());
MnuStrip.Items.Add(MnuStripItem);
}
// The Form.MainMenuStrip property determines the merge target.
this.MainMenuStrip = MnuStrip;
}
public void SubMenu(ToolStripMenuItem mnu, string submenu)
{
StringBuilder Seqchild = new StringBuilder();
Seqchild.AppendFormat("SELECT FRM_NAME FROM MNU_SUBMENU ,MNU_SUBMENULogin WHERE MNU_SUBMENU.FRM_CODE=MNU_SUBMENULogin.FRM_CODE AND MNU_SUBMENULogin.UserName='{0}' AND MENUPARVAL='{1}'", UserName.ToString(), submenu.ToString());
//Seqchild.AppendFormat( "SELECT FRM_NAME FROM MNU_SUBMENU ,MNU_SUBMENULogin WHERE MNU_SUBMENU.FRM_CODE=MNU_SUBMENULogin.FRM_CODE AND MNU_SUBMENULogin.UserName='1' AND MENUPARVAL='1'");
SqlDataAdapter dachildmnu = new SqlDataAdapter(Seqchild.ToString(), conn);
DataTable dtchild = new DataTable();
dachildmnu.Fill(dtchild);
foreach (DataRow dr in dtchild.Rows)
{
ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["FRM_NAME"].ToString(), null, new EventHandler(ChildClick));
mnu.DropDownItems.Add(SSMenu);
}
}
private void ChildClick(object sender, EventArgs e)
{
// MessageBox.Show(string.Concat("You have Clicked ", sender.ToString(), " Menu"), "Menu Items Event",MessageBoxButtons.OK, MessageBoxIcon.Information);
String Seqtx = "SELECT FRM_CODE FROM MNU_SUBMENU WHERE FRM_NAME='" + sender.ToString() + "'";
SqlDataAdapter datransaction = new SqlDataAdapter(Seqtx, conn);
DataTable dtransaction = new DataTable();
datransaction.Fill(dtransaction);
Assembly frmAssembly = Assembly.LoadFile(Application.ExecutablePath);
foreach (Type type in frmAssembly.GetTypes())
{
//MessageBox.Show(type.Name);
if (type.BaseType == typeof(Form))
{
if (type.Name == dtransaction.Rows[0][0].ToString())
{
Form frmShow = (Form)frmAssembly.CreateInstance(type.ToString());
// then when you want to close all of them simple call the below code
foreach (Form form in this.MdiChildren)
{
form.Close();
}
frmShow.MdiParent = this;
frmShow.WindowState = FormWindowState.Maximized;
//frmShow.ControlBox = false;
frmShow.Show();
}
}
}
}
private void FrmParent_FormClosed(object sender, FormClosedEventArgs e)
{
//=====偵測執行中的外部程式並關閉=====
Process[] MyProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (MyProcess.Length > 0)
MyProcess[0].Kill(); //關閉執行中的程式
}
}
}
自我LV~