即使使用get / set方法,也不会使用对象传递Int值

时间:2017-02-19 19:08:38

标签: c#

如果我的标题令人困惑,我很抱歉,但我不知道如何以不同的方式表达我的问题。我有两个类,两个窗口形式;其中一个是登录界面,另一个是用户在登录有效时被带到的商店界面。现在,我试图将用户名和密码传递到商店界面类,这样我就可以减去"购买"从数据库中的正确行,但由于某种原因,这是行不通的。我已经通过直接通过代码传递有效的用户名和密码字符串来测试我的方法,如果我这样做,该方法工作正常。调试模式也表明用户名和密码的值不会通过get方法传递给shop接口类。希望你能提前帮助,并且对于相当大的文本块抱歉:)

以下是两个有问题的类,以及包含我用于数据库的方法的类:

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;

namespace ShopshopFinalfinal
{
    public partial class LoginInterface : Form
    {
        DatabaseConnection dbConnection = new DatabaseConnection();
        private ShopInterface shop1Interface;
        private RegisterInterface reg1Interface;
        private string _username;
        private string _password;

        public void SetUsername(string username)
        {
            _username = username;
        }

        public void SetPassword(string password)
        {
            _password = password;
        }

        public string GetUsername()
        {
            return _username;
        }

        public string GetPassword()
        {
            return _password;
        }


        public LoginInterface()
        {
            InitializeComponent();
            txt_password.PasswordChar = '*';
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if (dbConnection.CheckUsername(txt_username.Text) == 1 && dbConnection.CheckPassword(txt_password.Text) == 1)
            {
                SetUsername(txt_username.Text);
                SetPassword(txt_password.Text);
                //txt_username.Clear();
                //txt_password.Clear();
                shop1Interface = new ShopInterface();
                shop1Interface.Show();

            }
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
           reg1Interface = new RegisterInterface();
            reg1Interface.Show();
        }
    }
}


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;

namespace ShopshopFinalfinal
{
    public partial class ShopInterface : Form
    {

        private DatabaseConnection dbConnection = new DatabaseConnection();
        private LoginInterface login = new LoginInterface();



        public ShopInterface()
        {
            InitializeComponent();
        }

        private void btn_buyapple_Click(object sender, EventArgs e)
        {


            //string username = login.GetUsername();
            //string password = login.GetPassword();

            dbConnection.Transaction(login.GetUsername(), login.GetPassword(), 10);


        }
    }
}

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopshopFinalfinal
{
    class DatabaseConnection
    {
        private SqlConnection conn;
        private SqlDataReader rdr;
        private SqlCommand cmd;

        public DatabaseConnection()
        {
            conn = new SqlConnection(Properties.Settings.Default.connectionstring);
        }

        public int CheckUsername(string username)
        {
            int result = 0;

            //Opret den ønskede SQL kommmando - her tjekker den om username fra textboxen er lig en i User table.
            cmd = new SqlCommand("select * from dbo.Users where Username ='" + username + "'", conn);

            //Åbn forbindelsen til databasen.
            conn.Open();

            //Udfører det ønskede SQL statement på databasen.
            rdr = cmd.ExecuteReader();

            //Tjek om den har læst og om det er rigtigt.
            if (rdr.Read())
            {
                result = 1;
            }
            else
            {
                result = 0;
            }

            //Luk forbindelsen til databasen.
            conn.Close();

            return result;
        }

        public int CheckPassword(string password)
        {
            int result = 0;

            //Opret SQL-kommando - tjek om password fra textbox er lig med et i databasen.
            cmd = new SqlCommand("select * from dbo.Users where Password ='" + password + "'", conn);

            //Åben forbindelsen.
            conn.Open();

            //Udfør det ønskede SQL statement på databasen.
            rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                result = 1;
            }
            else
            {
                result = 0;
            }

            conn.Close();

            return result;

        }

        public void RegisterUser(string username, string password)
        {
            conn.Open();

            cmd =
                new SqlCommand("insert into dbo.Users (Username, Password, IsAdmin, Balance) values ('" +
                               username +
                               "', '" + password + "', '0', '0')", conn); 
            rdr = cmd.ExecuteReader();
            rdr.Close();
            conn.Close();
        }

        public void Transaction(string username, string password, int price)
        {


            conn.Open();

            cmd = new SqlCommand("update dbo.Users set Balance = Balance - " + price + " where Username = '"+username+"' and Password = '"+password+"'", conn);

            rdr = cmd.ExecuteReader();

            rdr.Close();

            conn.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题在于,ShopInterface创建了LoginInterface的全新实例,其所有属性都处于默认状态。这与实际具有值的实例不同,因此它们为空的原因。

我认为,而不是试图找到一种方法让ShopInterfaceLoginInterface获取值(这意味着从ShopInterfaceLoginInterface的强烈依赖),更好的方法是提供一种方法来传递它们:

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;

namespace ShopshopFinalfinal
{
    public partial class ShopInterface : Form
    {

        private DatabaseConnection dbConnection = new DatabaseConnection();
        private string Username { get; set; }
        private string Password { get; set; }

        public ShopInterface(string username, string password)
        {
            InitializeComponent();
            Username = username;
            Password = password;
        }

        private void btn_buyapple_Click(object sender, EventArgs e)
        {

            dbConnection.Transaction(username, password, 10);


        }
    }
}

然后你可以LoginInterface执行此操作:

shop1Interface = new ShopInterface(txt_username.Text, txt_password.Text);
shop1Interface.Show();

答案 1 :(得分:1)

您几乎就在那里,但您没有将数据传递给其他表单。在ShopInterface

中进行更改
private LoginInterface login = new LoginInterface();

到此:

public LoginInterface Login {get; set;}

然后在LoginInterface之前执行此操作,然后再显示shop1Interface

private void btn_login_Click(object sender, EventArgs e)
{
    if (dbConnection.CheckUsername(txt_username.Text) == 1 && dbConnection.CheckPassword(txt_password.Text) == 1)
    {
        SetUsername(txt_username.Text);
        SetPassword(txt_password.Text);
        //txt_username.Clear();
        //txt_password.Clear();
        shop1Interface = new ShopInterface();

        // This is the line you need
        shop1Interface.Login = this;

        shop1Interface.Show();

    }
}

现在,因为您已将LoginInterface的引用传递给shop1Interface,现在可以像这样访问它:

private void btn_buyapple_Click(object sender, EventArgs e)
{
    // Now it will work
    string username = this.Login.GetUsername();
    string password = this.Login.GetPassword();

    dbConnection.Transaction(login.GetUsername(), login.GetPassword(), 10);
}