关于登陆中的验证问题

zhewky 2006-05-29 06:46:35
此程序中当验证用户名错误时,跳过了,小弟不才,不知道如何来验证,还有就是当我使用代码中注释掉的相对路径引用时,也会提示错误,不知道怎么办?希望高手们指教.本人初学C#,很想入门,如果小弟有幸认识各位,小疵视为上帝所宠
MyQQ:253530133,谢谢各位的回复!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication1
{
public partial class MyTest : Form
{
public MyTest()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=e:\\exam.mdb");
// OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source='" + Application.StartupPath + "'\\exam.mdb");
OleDbCommand comm = new OleDbCommand("select * from admin WHERE userid='"+this.textBox1.Text.Trim()+"'", conn);

conn.Open();

OleDbDataReader dr = comm.ExecuteReader();

while(dr!=null)
//if(dr.Read()!=null)
{
if(dr["userid"].ToString()==this.textBox1.Text.ToString())
//if (this.textBox1.Text.Equals(dr["userid"].ToString().Trim()))
{
if (dr["password"].ToString() == this.textBox2.Text.ToString())
//if (this.textBox2.Text.Equals(dr["password"].ToString().Trim()))
{
MessageBox.Show("你是合法用户");
}
else
{
MessageBox.Show("密码错误!");
this.textBox2.Focus();
this.textBox2.SelectAll();
}

}
else
{
MessageBox.Show("此用户不存在,是否注册!");
//this.textBox1.SelectAll();
}
}
conn.Close();

}
catch(Exception ee)
{

MessageBox.Show(AX.Message);
}


}
}
}
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhewky 2006-06-02
  • 打赏
  • 举报
回复

高歌,我怎么不能给你分哦!~
提示:你给的分的总和不对....
不好意思,这时才结贴
zhewky 2006-06-01
  • 打赏
  • 举报
回复
高歌.首先谢谢你,那两天不是我的浏览器问题还是什么原因.我登陆不上来了,提示不正确的验证,可我的ID和密码及验证码都是正确的,不知道,我把浏览器又重新搞了一下.麻烦死了.说我的工作吧,我把你写的代码复制过去之后,它提示说MyTest前少了partial
我加上之后,又出现了七处错误说:按钮和文本已经声明?
(我把我的代码注释了)
如果方便留下您的联系方式吧(以消息形式发给我)或加我QQ:253530133谢谢,以后向你多请教
amandag 2006-05-29
  • 打赏
  • 举报
回复
1.拼Application.StartupPath 的时候,前后不该加单引号
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\exam.mdb");

2.判断OleDbDataReader的对象dr是否还有记录用的是dr.Read()方法

3.只有遍历了数据库所有的记录,才能判断出用户是否存在,所以
MessageBox.Show("此用户不存在,是否注册!");应该放在循环之外

其他的小问题还很多,就不一一细说了
amandag 2006-05-29
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace WindowsApplication1
{
public class MyTest : Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.ComponentModel.Container components = null;
public MyTest()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 80);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(136, 80);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = '*';
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 168);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// MyTest
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "MyTest";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyTest());
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
// OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=e:\\exam.mdb");
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\exam.mdb");
string str = "select * from admin WHERE userid='"+this.textBox1.Text.Trim()+"'";
OleDbCommand comm = new OleDbCommand(str, conn);

conn.Open();

OleDbDataReader dr = comm.ExecuteReader();

bool flag = false;
while(dr.Read())
{
if(dr["userid"].ToString()==this.textBox1.Text.ToString())
{
if (dr["password"].ToString() == this.textBox2.Text.ToString())
{
MessageBox.Show("你是合法用户");
}
else
{
MessageBox.Show("密码错误!");
this.textBox2.Focus();
this.textBox2.SelectAll();
}
flag = true;
}

}
if (flag==false)
MessageBox.Show("此用户不存在,是否注册!");

conn.Close();

}
catch(Exception ee)
{

MessageBox.Show(ee.Message);
}


}

}
}
amandag 2006-05-29
  • 打赏
  • 举报
回复
1.使用楼主所指用的路径,一般数据库文件放在和exe文件一个目录下,调试时对应的是debug目


2.你所使用的登录判断方式很容易被SQL 注入攻击

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧