这个是设计的登陆界面
以下是登陆界面的代码:
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.OleDb;
using WindowsFormsApplication3;
namespace WindowsFormsApplication3
{
public partial class DL : Form
{
public static string User="";
public static string Pswd="";
public DL()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
User = "";
Pswd = "";
textBox1.Text = User;
}
private void button1_Click(object sender, EventArgs e)
{
string User = textBox1.Text;
string Pswd = textBox2.Text;
string sql = "Select 用户名,密码 from 密码表 where 学号=" + User + "";
//conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, Db.conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr[1].ToString() == Pswd)
{
Main MyForm = new Main();
MyForm.Show();
}
else
{
MessageBox.Show("密码错误,请重新输入!");
}
}
else
{
MessageBox.Show("用户名不存在,请重新输入!");
}
Db.conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (User == string.Empty)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
}
}
然后是调用ACCESS数据库的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication3
{
class Db
{
public static OleDbConnection str = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DATA:\C#\学生成绩管理.mdb");
public static OleDbConnection conn = new OleDbConnection();
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DL());
}
}
}
然后进行运行调试:
结果输入用户名后 登陆按钮就变成灰色的了。。。
搞不懂是怎么回事 麻烦懂的同学伸出援手。。。感激不尽。。

以下是登陆界面的代码:
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.OleDb;
using WindowsFormsApplication3;
namespace WindowsFormsApplication3
{
public partial class DL : Form
{
public static string User="";
public static string Pswd="";
public DL()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
User = "";
Pswd = "";
textBox1.Text = User;
}
private void button1_Click(object sender, EventArgs e)
{
string User = textBox1.Text;
string Pswd = textBox2.Text;
string sql = "Select 用户名,密码 from 密码表 where 学号=" + User + "";
//conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, Db.conn);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr[1].ToString() == Pswd)
{
Main MyForm = new Main();
MyForm.Show();
}
else
{
MessageBox.Show("密码错误,请重新输入!");
}
}
else
{
MessageBox.Show("用户名不存在,请重新输入!");
}
Db.conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (User == string.Empty)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
}
}
然后是调用ACCESS数据库的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication3
{
class Db
{
public static OleDbConnection str = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DATA:\C#\学生成绩管理.mdb");
public static OleDbConnection conn = new OleDbConnection();
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DL());
}
}
}
然后进行运行调试:

结果输入用户名后 登陆按钮就变成灰色的了。。。

