Program.cs代码如下
using System;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace HFGZ
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form loginfm = new myLogin();
            if (loginfm.ShowDialog() == DialogResult.OK)
                Application.Run(new mainform());
            else
                Application.Exit();
        }
    }
}
 
myLogin.cs代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
 
namespace HFGZ
{
    public partial class myLogin : Form
    {
        private int LoginCount = 0;
        public myLogin()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //在项目中引用System.Configuration
            string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            //result = DialogResult.OK;
            using (SqlConnection connection = new SqlConnection(conString))
            {
                SqlCommand command = new SqlCommand("[Security].[myLogin]", connection);
                command.CommandType=CommandType.StoredProcedure;
                SqlParameter UserName = command.Parameters.Add("@UserName", SqlDbType.VarChar);
                SqlParameter Password = command.Parameters.Add("@Password", SqlDbType.VarChar);
                SqlParameter Validate = command.Parameters.Add("@validate", SqlDbType.Bit);
                UserName.Direction = ParameterDirection.Input;
                Password.Direction = ParameterDirection.Input;
                Validate.Direction = ParameterDirection.Output;
                UserName.Value = textBox1.Text;
                Password.Value = textBox2.Text;
                connection.Open();
                command.ExecuteNonQuery();
                if ((bool)Validate.Value)
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    LoginCount += 1;
                    MessageBox.Show("用户名或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox1.Focus();
                    if (LoginCount == 3)
                    {
                        MessageBox.Show("超过登录次数!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.DialogResult = DialogResult.Cancel;
                    }
                }
            }
        }
 
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                e.Cancel = true;
                MessageBox.Show("请输入用户名!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Select(0, textBox1.Text.Length);
                textBox1.Focus();
            }
        }
    }
}

解决方案 »

  1.   

    参看
    http://blog.csdn.net/knight94/archive/2006/04/06/652394.aspx
      

  2.   

    添加模块,在里面定义Public变量
      

  3.   

    参考窗体操作,
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx
    http://blog.csdn.net/zhzuo/archive/2006/05/05/708941.aspx
      

  4.   

    可以定义全局变量, 也可以重载mainform的构造函数mainform(string username)来传入用户名
    我建议定义一个全局的类来维护一些全局数据, 比如用户名, 用户的其他信息, 登陆时间等等
      

  5.   

    我的做法:在login
    private static string xxx;
    public static strig Getxxx();
    {
        return xxx;
    }
    在你需要的地方给xxx付值
    在mainForm
    在你需要调用的地方
    string xxx1 = login.Getxxx();
    高手看看这样好不
    反正我是这样使用的