//以下为Mymeans文件 
   class Mymeans
    {
        public static SqlConnection My_con;
        public static string Login_ID = "";
        public static string Login_Name = "";
        public static string M_str_sqlcon = "Data Source=5DF762D73A1A427;Database=PMS;User id=sa;Pwd=sa";
        public static int Login_n = 0;
        public static string AllSql ="Select * From 操作用户";
                public static SqlConnection getcon()
        {
            My_con = new SqlConnection(M_str_sqlcon);
            My_con.Open();
            return My_con;
        }        public void con_close()
        {
            if (My_con.State == ConnectionState.Open)
            {
                My_con.Close();
                My_con.Dispose();
            }
        }        public SqlDataReader getcom(string SQLstr)
        {
            getcon();
            SqlCommand My_com = My_con.CreateCommand();
            My_com.CommandText = SQLstr;
            SqlDataReader My_read = My_com.ExecuteReader();
            return My_read;
        }        public void getsqlcom(string SQLstr)
        {
            getcon();
            SqlCommand SQLcom = new SqlCommand(SQLstr,My_con);
            SQLcom.ExecuteNonQuery();
            SQLcom.Dispose();
            con_close();
        }        public DataSet getDataSet(string SQLstr, string tableName)
        {
            getcon();
            SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
            DataSet My_DataSet = new DataSet();
            SQLda.Fill(My_DataSet, tableName);
            con_close();
            return My_DataSet;
        }
    }
//以下为F_Login文件
  public partial class F_Login : Form
    {
        public F_Login()
        {
            InitializeComponent();
        }        PMS.Mymeans MyClass = new Mymeans();
        //private int MyTryCount = 0;        private void butLogin_Click(object sender, EventArgs e)
        {
            if (textName.Text != "" & textPass.Text != "")
            {
                SqlDataReader temDR = MyClass.getcom("Select * From 操作用户 Where 用户名称='"+textName.Text.Trim()+ "'and 用户密码='"+textPass.Text.Trim()+"'");
                bool ifcom = temDR.Read();
                if (ifcom)
                {
                    PMS.Mymeans.Login_Name = textName.Text.Trim();
                    PMS.Mymeans.Login_ID = temDR.GetString(0);
                    PMS.Mymeans.My_con.Close();
                    PMS.Mymeans.My_con.Dispose();
                    this.Close();
                }
                else
                {
                //    SqlCommand SQLcom1 = MyClass.getsqlcom();
                //    int MyCount = (int)SQLcom1.ExecuteScalar();
                //    if (MyCount == 1)
                //    {
                //        this.Close();
                //    }
                //    else
                //    {
                //        this.MyTryCount += 1;
                //        if (this.MyTryCount != 3)
                //        {
                //            int MyDoCount = 3 - this.MyTryCount;
                //            MessageBox.Show("用户密码或用户名称有错,还有" + MyDoCount + "次机会!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //            this.textName.Text = "";
                //            this.textPass.Text = "";
                //        }
                //        else
                //        {
                //            butClose_Click(null, null);
                //        }
                //    }

                    MessageBox.Show("用户名或密码输入错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textName.Text = "";
                    textPass.Text = "";
                }
                MyClass.con_close();
            }
            else
                MessageBox.Show("请将登录信息添写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);        }        private void textName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                textPass.Focus();
        }        private void textPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                butLogin.Focus();
        }        private void butClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
这段代码主要目的是想,当用户登录时,如果输入错误超过三次,程序自动关闭.
在Mymeans中,写了SQL的链接方面的内容
在F_Login主要是实现用户登录我想在F_Login中加入"//"注释内的内容(红色部分),但在运行的时候,就会报错,说是CONNECTION属性错误.
不知道怎么改才能运行成功

解决方案 »

  1.   

    做一个计数器啊
    输入错误 一次加1,
    大于三次就退出 int MyCount = (int)SQLcom1.ExecuteScalar(); 
    ExecuteScalar只是返回首行首列,你的表结构不知道什么样,也不知道你这个地方要干嘛
      

  2.   

    其实是这样的,由于本人想设计出一个文件是存放SQL的链接信息,然后再调用的时候不用再重新去写,同时亦方便可以修改一处就达到全部修改的效果.
    通常我见人家设计的,都是将SQL与用户登录页面放在一起的,但我想将这两部分的代码分开.但系当我将那个大于三次就退出的部分放入在用户登录页面的时候,就无办法运行,
    当我打入第一次错误时,需要将这个错误的记录返回给数据库,所以需要调用到数据,
    这是我个人的理解,不知道有没有错.如果那段代码有错,那要怎样改才能最终实现这个效果呢?
      

  3.   

    在你代码 MessageBox.Show("用户名或密码输入错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    下面加个int变量用来计数,当等于三时就让程序退出,Application.Exit();应该可以,你试试
      

  4.   

    老大  你的getsqlcom()方法是void的  根本没有返回值  怎么会不报错   在说getsqlcom已经执行了SQL语句了   PS:数据库连接字符串放在配置文件中  你这样的话都写死了   万一数据库换了服务器你不得改程序啊
      

  5.   

    写了个 超级简单的,能限制输入只能有3次,当然你可以改成你想要的效果:1、窗体里 添加一个button1(登录按钮),textBox1(输入密码),label1;
    2、所有详细代码:(注意i是全局变量)
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace ewgerdf
    {
        public partial class Form1 : Form
        {        public Form1()
            {
                InitializeComponent();
              
                
            }        private void Form2_Load(object sender, EventArgs e)
            {
               
            }
            int i = 3;
            private void button1_Click(object sender, EventArgs e)
            {
                string infor = textBox1.Text.Trim();            if (textBox1.Text.Trim() != "123"&&i!=1&&infor!="")
                {
                    --i;
                    label1.Text = "输入错误,你还有" + i + "次机会";
                    textBox1.SelectAll();
                    textBox1.Focus();
                    
                    textBox1_TextChanged(null, null); 
                }
                else if (textBox1.Text.Trim() == "123")
                {                MessageBox.Show("输入成功");
                }
                else if (i == 1&& infor != "")
                {
                  
                    label1.Text = "输入错误,你还有" + "0次机会";
                    textBox1.Enabled = false; }
                else
                { 
                    MessageBox.Show("输入用户名/密码!");
                textBox1.Focus();
            }
             
              
                
            }        private void textBox1_TextChanged(object sender, EventArgs e)
            {
           
            }
        }
    }
      

  6.   

    就使用个变量、 保存客户去触发事件后. 如果错误就+1. 当大于三时. 就close(). 
      

  7.   


    static int i=0;SqlDataReader temDR = MyClass.getcom("Select * From 操作用户 Where 用户名称='"+textName.Text.Trim()+ "'and 用户密码='"+textPass.Text.Trim()+"'"); 
                    bool ifcom = temDR.Read(); 
                    if (ifcom) 
                    { 
                        PMS.Mymeans.Login_Name = textName.Text.Trim(); 
                        PMS.Mymeans.Login_ID = temDR.GetString(0); 
                        PMS.Mymeans.My_con.Close(); 
                        PMS.Mymeans.My_con.Dispose(); 
                        this.Close(); 
                        
                    } else{messbox.show("shibai");i++}if(i==3)
    {
       application.exit();
    }