希望这个问题有意义。
基本上,我正在为单人作业制作手术系统。
我已经用用户名和密码等制作了一个基于服务的数据库和用户表。
登录名已全部排序。控制台会打印正确的RoleType并将用户登录到。
我疯了一个名为RoleType的枚举,我试图根据用户在
中的角色来更改这是我目前为止的位置...
登录表单
//Declare an enum to store roletypes
public enum RoleTypes
{
practiceManager,
doctor,
receptionist
}
private void btnLogin_Click(object sender, EventArgs e)
{
//Try and open a connection with database and run the code
try
{
//Create new instance of sql connection, pass in the connection string for BayOneSurgerySystem.mdf to connect to database.
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\davie\Documents\UniWork\Software Engineering\SurgerySystem\SurgeryDatabase\BayOneLoginSystem.mdf;Integrated Security=True;Connect Timeout=30");
//Create new instance of SQlCommand and pass in a query to be called to retrieve table data for username and passwords aswell as the connection object.
SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Username = @username and Password = @password", conn);
//This passes user input into @username and @password
cmd.Parameters.AddWithValue("@username", txtBoxUsername.Text);
cmd.Parameters.AddWithValue("@password", txtBoxPassword.Text);
//Open connection with database
conn.Open();
//Create new instance of dataSet to hold the data retrieved from sql query
DataSet ds = new DataSet();
//Create new instance of DataAdpater to retrieve the data pass in Sql command
SqlDataAdapter da = new SqlDataAdapter(cmd);
//using DataAdapter fill in dataSet wiht data if user input and stored data matches
da.Fill(ds);
//Close the connection now data table is filled with username and password
conn.Close();
//declare bool, true if there is a match with database and user input
bool loginSuccess = (ds.Tables[0].Rows.Count == 1);
//if login success is true then open menu
if (loginSuccess)
{
//Change state of enum RoleTypes bases on result from dataSet Role column.
Console.WriteLine(ds.Tables[0].Rows[0]["Role"].ToString());
try
{
switch (ds.Tables[0].Rows[0]["Role"])
{
case "Doctor":
{
RoleTypes roleType = RoleTypes.doctor;
Console.WriteLine("Role type chnage to" + roleType.ToString());
}
break;
case "Practice Manager":
{
RoleTypes roleType = RoleTypes.practiceManager;
Console.WriteLine("Role type chnage to" + roleType.ToString());
}
break;
case "receptionist":
{
RoleTypes roleType = RoleTypes.receptionist;
Console.WriteLine("Role type chnage to" + roleType.ToString());
}
break;
default:
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Logged in.");
FrmMenu menu = new FrmMenu();
this.Close();
menu.Show();
}
else
{
MessageBox.Show("Invalid username or password.", "Error!", MessageBoxButtons.RetryCancel);
Console.WriteLine("Not logged in");
}
}
//If connection cant be opened diplsay error message and catch exception and print to console
catch(Exception ex)
{
Console.WriteLine(ex);
MessageBox.Show("Sorry can't connect");
}
}
}
}
这个想法是,可以在FrmMenu中引用公共枚举,并且将基于该枚举显示不同的控件。
它只是使用switch语句忽略了tryCatch,没有捕获任何异常?知道为什么吗?还是有一种更有效的方法?
预先感谢!
答案 0 :(得分:1)
我已经做了类似的事情。我已经创建了角色和用户角色表。
CREATE TABLE [dbo].[Role](
[RoleID] [nvarchar](10) NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
[Memo] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_SYS_Role] PRIMARY KEY NONCLUSTERED
(
[RoleID] 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
UserRole表
CREATE TABLE [dbo].[UserRole](
[UserRoleID] [nvarchar](10) NOT NULL,
[UserID] [nvarchar](10) NOT NULL,
[RoleID] [nvarchar](10) NOT NULL,
CONSTRAINT [PK_UserRole] PRIMARY KEY CLUSTERED
(
[UserRoleID] 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
ALTER TABLE [dbo].[UserRole] WITH CHECK ADD CONSTRAINT [FK_UserRole_REF_Role] FOREIGN KEY([RoleID])
REFERENCES [dbo].[Role] ([RoleID])
GO
ALTER TABLE [dbo].[UserRole] CHECK CONSTRAINT [FK_UserRole_REF_Role]
GO
ALTER TABLE [dbo].[UserRole] WITH CHECK ADD CONSTRAINT [FK_UserRole_REF_USER] FOREIGN KEY([UserID])
REFERENCES [dbo].[User] ([UserID])
GO
ALTER TABLE [dbo].[UserRole] CHECK CONSTRAINT [FK_UserRole_REF_USER]
GO
在您的表单上,根据您的登录用户调用查询