表格登录信息

时间:2015-04-12 03:59:06

标签: c#

我正在尝试让表单使用DATABASE代码中的ID和密码来登录学生帐户登录信息

如何将studentiDPassword添加到表单并让它执行?

这是数据库:

enter image description here

学生班:

    class Student : Person
    {
        private int iD;
        private String password;
        private String eMail;
        private double gpa;
        private String message;


        public Student() : base()
        {
            this.iD = 0;
            this.password = "";
            this.eMail = "";
            this.gpa = 0;
        }
        public Student(int i, String pa, String eM, int gp) : base()
        {
            this.iD = i;
            this.password = pa;
            this.eMail = eM;
            this.gpa = gp;
            InsertDB();
        }
        public Student(int iD)
        {
            SelectDB(iD);
        }
        //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
        public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
        public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
        public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
        public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
        public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
        public System.Data.OleDb.OleDbConnection OleDbConnection;
        public string cmd;

        public void DBSetup()
        {
            // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
            // This DBSetup() method instantiates all the DB objects needed to access a DB, 
            // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
            // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
            // Command object contains a Connection object and an SQL string object.
            OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
            OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
            OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
            OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
            OleDbConnection = new System.Data.OleDb.OleDbConnection();


            OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
            OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
            OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
            OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;


            OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
            "istry Path=;Jet OLEDB:Database L" + 
            "ocking Mode=1;Data Source=c:\\RegistrationMDB.accdb;J" + 
            "et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
            "ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
            "hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
            "OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
            "r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

        }

        public void SelectDB(int id) 
        { 
            //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
            DBSetup();
            cmd = "Select * from Students where ID = " + iD;
            OleDbDataAdapter.SelectCommand.CommandText = cmd;
            OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  {
                    OleDbConnection.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                    dr.Read();
                    id=iD;
                    setPassword(dr.GetValue(1)+"");
                    setEMail(dr.GetValue(2)+"");

                    setGpa(Double.Parse(dr.GetValue(3)+""));
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void InsertDB() 
        {
            // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

            DBSetup();
            cmd = "INSERT into Students values(" + getID() + "," +
                             "'" + getPassword() + "'," +
                             "'" + getEMail() + "'," +
                            "'" + getGpa() +  ")";

            OleDbDataAdapter.InsertCommand.CommandText = cmd;
            OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Inserted");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                
        }
        public void updateDB() 
        {
            //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

            cmd = "Update Students set ID = '" + getID() + "'," + 
                        "Password = '" + getPassword() +    "', " +
                        "Email = '" + getEMail() + "', " +
                         "GPA = " + getGpa();

            OleDbDataAdapter.UpdateCommand.CommandText = cmd;
            OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Updated");
                else
                    Console.WriteLine("ERROR: Updating Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void deleteDB() 
        {
            //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

            cmd = "Delete from Students where ID = " + getID();
            OleDbDataAdapter.DeleteCommand.CommandText = cmd;
            OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Deleted");
                else
                    Console.WriteLine("ERROR: Deleting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }





        public void setID(int iD)
        {
            this.iD = iD;
        }

        public void setPassword(String password)
        {
            this.password = password;
        }


        public void setEMail(String eMail)
        {
            this.eMail = eMail;
        }
        public void setGpa(double gpa)
        {
            this.gpa = gpa;
        }

        public int getID()
        {
            return iD;
        }


        public String getPassword()
        {
            return password;
        }



        public String getEMail()
        {
            return eMail;
        }

        public double getGpa()
        {
            return gpa;
        }
        public String getMessage()
        {
            return this.message;
        }

        public void displays()
        {
            System.Console.WriteLine("ID =  "+ getID());
            System.Console.WriteLine("Password =   "+ getPassword());
            System.Console.WriteLine("Email =  " + getEMail());
            System.Console.WriteLine("GPA =  " + getGpa());            
        }
    }

表格:

namespace Students
{
    public partial class StudentLogin : Form
    {
        public StudentLogin()
        {
            InitializeComponent();
        }

        private void Logingo_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

    private void Logingo_Click(object sender, EventArgs e)
    {
        int studentiD = 1;//Your ID
        string Password = "1234";//Your Password
        Student st = new Student(studentiD);
        if (st.getID() == studentiD && st.getPassword() == Password)
        {
            MessageBox.Show("Login Successed.");
            st.displays();//display ID,Password,Email,GPA
        }
        else
        {
            MessageBox.Show("Login Failed.");
        }
    }

答案 1 :(得分:0)

在您的课程中添加一个布尔方法来检查学生的凭据

在您的班级Student.cs中:

public string LogNotification{get;set;}
public bool ConfirmLogin(string id, string pw)
        {
            using(SqlConnection con = new SqlConnection("Your connection string here"))
            {
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("SELECT ID, PASSWORD FROM Students WHERE ID = @ID OR PASSWORD = @PASSWORD",con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = id;
            cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = pw;


            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ID = dr["@ID"].ToString();
                Password = dr["@PASSWORD"].ToString();
            }

            if (ID == id && Password == pw)
            {
                return true;
            }
            else
            {
                LogNotification = "ID/Password is incorrect";
                return false;
             }
            }
        }

在您的表格中:

private void Logingo_Click(object sender, EventArgs e)
    {
         Student st = new Student();
         If(st.ConfirmLogin(txtID.text,txtPass.text)==false)
              MessageBox.Show(st.LogNotification);
         else
            //show next form or whatever action you prefer

    }