日期:2014-05-17  浏览次数:21007 次

如何做用户登录界面?
就是说做一个用户登录界面,用户需要先输入用户名和密码,正确的话才能进入到程序,否则提示错误。

------解决方案--------------------
这个问题有点严重。。。。 应该在学校都是从这个开始学起的。。。。
------解决方案--------------------
教mm学程序用的一个例子呵呵
C# code
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace stserver
{
  public partial class login : Form
  {
    public login()
    {
      InitializeComponent();
    }
    public bool islogin = false;

    private void btn_submit_Click(object sender, EventArgs e)
    {
      try
      {
        XmlDocument doc = new XmlDocument();
        doc.Load(Application.StartupPath + "\\config.xml");
        string connstr = doc.DocumentElement.SelectSingleNode("serverconnstr").InnerText;
        string selectstr = "select top 1 id from admin where uid='" + txb_uid.Text + "' and pwd='" + txb_pwd.Text + "'";
        SqlConnection conn = new SqlConnection(connstr);
        conn.Open();
        SqlCommand cmd = new SqlCommand(selectstr, conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
          islogin = true;
          dr.Close();
          conn.Close();
          conn.Dispose();
          MessageBox.Show("您好," + txb_pwd.Text + "!,欢迎使用\n\n祝您工作愉快!", "欢迎", MessageBoxButtons.OK, MessageBoxIcon.Information);
          this.Close();
        }
        else
        {
          dr.Close();
          conn.Close();
          conn.Dispose();
          MessageBox.Show("登录失败!\n请检查您的登录用户名和密码是否正确。\n\n如果您忘记了自己的密码请联系技术部");
          txb_uid.Text = "";
          txb_pwd.Text = "";
          txb_uid.Focus();
        }
        dr.Close();
        conn.Close();
        conn.Dispose();
      }
      catch
      {
        MessageBox.Show("不能连接到服务器,请联系技术部","服务器故障",MessageBoxButtons.OK,MessageBoxIcon.Error);
      }
    }

    private void btn_cancel_Click(object sender, EventArgs e)
    {
      Application.ExitThread();
    }
  }
}


------解决方案--------------------
上面是登陆窗体login.cs的代码

下面是program.cs的代码:
C# code
 
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace stserver
{