如果是管理员,则无法读取使用身份验证登录。 [英] Login with auth can't read if admin or not.
问题描述
您好;我试图通过一个简单的登录表单检查某人是否是管理员,我的登录工作,但当我尝试检查管理员是否if语句不起作用。我有一个包含3列的用户名,密码和类型的表。我想检查它是否在类型列中写入Admin。这是我的代码:
Hi; I am trying to check if someone is admin or not with a simple login form, my login works but when i try to check if admin or not that if statement doesn't work. I have a table with 3 column with username, password and type. I want to check if in the type column it writes Admin or not. Here is my code:
private void button1_Click ( object sender , EventArgs e )
{
SqlConnection baglanti = new SqlConnection ( @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mehmet\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True;" );
SqlDataAdapter adabtor = new SqlDataAdapter ( "select Type from Log_In where Username='" + UserNametextBox1.Text + "' and Password = '" + PasswordtextBox2.Text + "'",baglanti);
DataTable tablo = new DataTable();
adabtor.Fill(tablo);
if ( tablo.Rows.Count == 1 )
{
logInToolStripMenuItem.Enabled = false;
logOutToolStripMenuItem.Enabled = true;
LogInpanel1.Visible = false;
Aboutpanel2.Visible = true;
//MessageBox.Show ( tablo.Rows [ 0 ] [ 0 ].ToString () ); i use this to see what returns from this code, and code returns the correct value like Admin in this case, i also tryed to change Admin to 1, nothing changed
if ( tablo.Rows [ 0 ] [ 0 ].ToString () == "Admin" )
{
addAUserToolStripMenuItem.Visible = true;
MessageBox.Show ( "Yes! An admin!" );
}
}
else
MessageBox.Show ( "Username or Password is wrong." );
}
推荐答案
尝试中和字符串数据可能具有的变体 -
Try neutralizing the variations that you string data can have with something like-
if ( tablo.Rows[0][0].ToString().Trim().ToLower() == "admin" )
{
修剪()将截断空格。
参考: String.Trim方法() [ ^ ]
ToLower()会将所有字符转换为小写字母。
参考: String.ToLower方法 [ ^ ]
只是一个建议:
使用列名而不是索引,例如 -
Trim() will truncate the blank spaces.
Reference: String.Trim Method ()[^]
ToLower() will convert all the characters to lower case.
Reference: String.ToLower Method[^]
Just a suggestion:
Use column name instead of index, like-
if ( tablo.Rows[0]["Type"].ToString().Trim().ToLower() == "ADMIN" )
{
希望,它有帮助:)
Hope, it helps :)
我不知道为什么它有效,什么是修剪。我改变了管理员而不是我去了1和0所以它的工作原理如下:
i have no idea why it works and what is trim. Also i changed Admin instead i went for 1 and 0 so it worked like this:
if ( Convert.ToString ( tablo.Rows [ 0 ] [ 0 ] ).Trim ().ToLower () == "1" )
{
addAUserToolStripMenuItem.Visible = true;
MessageBox.Show ( "haydi" );
}
这篇关于如果是管理员,则无法读取使用身份验证登录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!