2016-01-13 20 views
0

我目前正在Visual Studio 2012中开发一个Windows窗体应用程序,该应用程序有两种不同的登录方式,基于用户是客户端还是管理员。下图显示了sql数据库的登录表。C#SQL如何让用户只能在登录时在datagridview中看到他们的详细信息

Picture of image

正如你可以看到这是一个有3列这是用户名,密码和角色(确定用户是否是管理员或客户端)一个非常简单的登录系统。 我遇到的问题是我想创建一个表单,允许其角色是客户端的用户通过使用datagridview从登录表中查看他们自己的用户名和密码信息。

目前我有一个按钮,它将显示datagridview中登录表中的每个用户的所有信息,如何编辑我的代码以便datagridview只显示当前登录用户的登录表详细信息?而不是像现在这样显示所有用户的登录信息。

以下是datagrid视图和视图按钮的图像。

Basic concept image of datagridview and view button

下面是在DataGridView和视图按钮的代码。从.NET 2.0

{ public ClientAnalyzeyourowninformation() 
    { 
     InitializeComponent(); 
    } 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Data.mdf;Integrated Security=True"); 
    private void button1_Click(object sender, EventArgs e) 
    { 
     // this is the code for the view button of admin menu 
     con.Open(); 
     SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Login", con); 
     DataTable DATA = new DataTable(); 
     sda.Fill(DATA); 
     dataGridView1.DataSource = DATA; 
     con.Close(); 

    } 
+1

你应该看看WHERE条款......此外,你应该*永远不会*以纯文本存储密码。 – Siyual

+2

这么多事情不对,我在流泪的眼泪。 .NET内置了认证和角色库 - 请使用其中之一。 – Hogan

回答

3

使用会员/角色:

private void button1_Click(object sender, EventArgs e) 
{ 
    // this is the code for the view button of admin menu 
    List<MembershipUser> Users; 
    if (Roles.IsInRole("Admin")) 
     Users=Membership.GetAllUsers(); 
    else 
     Users=new List<MembershipUser>() {Membership.GetUser()}; 
    dataGridView1.DataSource = Users; 
} 

如果你坚持滚动自己:

con.Open(); 
var sda = new SqlDataAdapter("SELECT * FROM Login WHERE [email protected]",con); 
sda.SelectCommand.Parameters.AddWithValue("@Username", lblUserName.Text); 
DataSet DATA = new DataSet(); 
sda.Fill(DATA); 
dataGridView1.DataSource = DATA; 
con.Close(); 
+0

这是一篇关于如何配置以及一些实现提示的小文章:http://social.technet.microsoft.com/wiki/contents/articles/3855.how-to-use-a-membership-provider-of- asp-net-in-winform-application.aspx –

+0

你也可以使用[SimpleMembership](https://msdn.microsoft.com/en-us/library/webmatrix.webdata.simplemembershipprovider(v = vs.111).aspx ),[ASP.NET身份](http://www.asp.net/identity)或任何其他安全软件包更有可能做到这一点。 –

+0

这是一种更好的方式,那么我现在正在做的方式,但我不能用你的方式,因为我已经完成登录系统,不想用你的方法重新编写代码。我基本上只需要一个简单的方法来显示当前登录的用户的登录细节,我并不在乎它的基本原理。 – reddevil54

-2

存储用户名到某个级别,当你把数据进行登录。

lblUserName.Text=txtUserName.Text; 

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0 AttachDbFilename=|DataDirectory|Data.mdf;Integrated Security=True"); 
private void button1_Click(object sender, EventArgs e) 
{ 

    con.Open(); 
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Login WHERE username='"+lblUserName.Text+"'", con); 
    DataSet DATA = new DataSet(); 
    sda.Fill(DATA); 
    dataGridView1.DataSource = DATA; 
    con.Close(); 

} 
+0

对不起,我迟到的回应。我编辑了代码... –

+0

请在您的用户名框中输入“'或''=''并观看它转储所有记录。我希望我可以倒下1000次这个答案。您刚刚使任何用户都可以获取整个系统中每个人的用户名和密码。恭喜。 –

相关问题