using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
//创建数据库连接字符串 SqlConnection connection = new SqlConnection(connString); //通过connection对象创建数据库连接
con.ConnectionString = "server=DESKTOP-3U564EO;database=library;uid=xxpxxplove;pwd=feinibuaiv5";
string sql = string.Format("select count(*) from {2} where num='{0}'and password='{1}'", textBox1.Text,textBox2.Text , comboBox1.Text); //创建sql语句
con.Open(); //打开数据库连接
//创建command对象
SqlCommand command = new SqlCommand(sql,con);
int num = (int)command.ExecuteScalar(); //执行sql查询语句,ExecuteScalar()返回查询结果集中的第一行的第一列
if (num > 0)
{
MessageBox.Show("欢迎进入成绩管理系统!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information); //弹出登录成功消息框
this.Hide(); //当前窗口(登录窗口)不显示
if (comboBox1.Text=="Administrator")
{
Form AMain = new Form();
AMain.ShowDialog();
this.Close();
this.Dispose();
}
else
{
Form RMain = new Form();
RMain.ShowDialog();
this.Close();
this.Dispose();
}
}
else
{
MessageBox.Show("您输入的用户名或密码错误!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
con.Close();//关闭数据库连接
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
你把你的程序运行的现象截几张图出来看看,把你的问题现象描述详细些。
Form RMain = new Form(); Form AMain = new Form(); 仔细看你new的是form()不是 new RMain() 也不是 new AMain()
只是new了一个空白form而不你已经定义好的form...
你new 了一个空白form 打开的当然也是一个空白的form,,,
楼上已经回答了。你是想登录成功后加载你的图书馆里主页面?你定义的主页面Form名称是AMain,而你实例化的对象名称是Form,相当于你new了一个空白的Form,然后打开了它。
if (comboBox1.Text=="Administrator")
{
Form AMain = new Form();
AMain.ShowDialog();
this.Close();
this.Dispose();
}
else
{
Form RMain = new Form();
RMain.ShowDialog();
this.Close();
this.Dispose();
}
改为:
if (comboBox1.Text=="Administrator")
{
** AMain frm= new AMain();
frm.ShowDialog();**
this.Close();
this.Dispose();
}
else
{
**RMain frm = new RMain();
frm.ShowDialog();**
this.Close();
this.Dispose();
}