0

所以我有一个有趣的问题......我有一个可以运行的应用程序,但需要“OrderID.text”中的一个值。然后在 MySQL 数据库中搜索该值,并返回我需要的信息。在这种情况下,电话号码。

当我在代码中包含该值时。(例如,newCurrentID = 8),它返回电话号码。但是,我希望用户能够输入自己的值,并且电话号码更改为用户的偏好。

当我输入一个值时,MessageBox 在我按下回车后工作,但电话的文本框保持空白。有任何想法吗????

我只包括与此相关的代码。

public partial class Form1:
{
    //the rest is just design code for the app.
    private void InitializeComponent()
    {
        this.orderID.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EnterKey);
        this.orderID.Text = "";
        this.phoneNumber.Text = DB_App.phone_number;
        this.phoneNumber.TextChanged += new System.EventHandler(this.phoneNumber_TextChanged);
    }
}

public partial class Form1 : Form
{
    private DBApplication DB_App;

    public Form1()
    {
        DB_App = new DBApplication(this);
        InitializeComponent();
    }

    // I assigned a value to this to make sure the MySQL connection worked.
    public int newCurrentID;

    private void EnterKey(object o, KeyPressEventArgs e)
    {
        if(e.KeyChar == (char)Keys.Enter)
        {
            if (!int.TryParse(orderID.Text, out newCurrentID))
                MessageBox.Show("not a number");
            else
            {
                int.TryParse(orderID.Text, out newCurrentID);
                //I used this to make sure the "newCurrentID" was being read.
                //MessageBox.Show(newCurrentID.ToString()); 
            }
            DB_App.OrderID();
            e.Handled = true;
        }
    }

    private void phoneNumber_TextChanged(object sender, EventArgs e)
    {

    }
}

public class DBApplication : DBInfo
{
    public DBApplication(Form1 form)
    {
        this.Form = form;
        OrderID();
        CloseConnection();
    }

    //my guess is that the problem is somewhere in here, but im not certain.
    public void OrderID()
    {
        customer_id = this.Form.newCurrentID;
        //add the DataBase query to the string.

        query = "SELECT * FROM wdb_customer WHERE customer_id= @customer_id";

        if (OpenConnection() == true)
        {
            //MySqlCommand myComm = connection.CreateCommand();
            MySqlCommand myComm = new MySqlCommand(query, connection);
            MySqlDataReader Reader;
            //myComm.CommandText = query;
            //myComm.Connection = connection;
            myComm.Parameters.AddWithValue("@customer_id", customer_id);
            Reader = myComm.ExecuteReader();

            while (Reader.Read())
            {
                lastname = Reader["customer_name"].ToString();
                phone_number = Reader["customer_telephone"].ToString();
            }
            Reader.Close();
        }

        CloseConnection();
        //return lastname;
    }
}
4

1 回答 1

1

没有迹象表明您的表单会知道数据库类中的电话号码已更改的事实。考虑一个设计,您的 DBApplication不知道表单。您可以将参数传递给函数 (newCurrentID),并且可以将值返回给表单(电话号码),然后表单需要将其放入其文本字段中。

编辑:

你需要用更专业的术语来考虑你的功能。目前,您有两方和一笔交易。例如,商家和客户以及客户想要购买糖果棒。

您当前的行为是:

void Purchase();

在幕后,商人把糖果塞进顾客的喉咙,另一只手试图从顾客钱包里掏钱。两人都认识对方,都记下了对方是谁。 如果商人是这样,我也会做笔记!

您需要实施的是专业交易:

CandyBar Purchase(money);

顾客交钱,商家给糖果。交易完成后,双方都可以随心所欲地处理各自的货物。双方在交易后立即忘记对方。

您的交易是,您的数据库在获得客户编号时交出数据:

public class CustomerData
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
}


public class DataStore
{
    // this is a clean description of your "deal", there's nothing behind the scenes.
    public CustomerData GetCustomerByID(int orderID)
    {
       CustomerData customer = null;

       // somehow get data from database as before using customerID
       customer = new CustomerData { Name = "John Smith", PhoneNumber = "555-123456789" };

       return customer; 
    }
}

您的表单负责实际从您的用户那里获取该数字并将结果放回 UI:

public class Form1
{
private DataStore data;

// call this any time the user changes something
    private void UpdateData()
    {
        int orderID;
        if (int.TryParse(orderID.Text, out orderID))
        {
            var customer = data.GetCustomerByID(orderID);
            this.phoneNumber.Text = customer.PhoneNumber;
            // this.name.Text = customer.Name;
        }
    }
}

请注意,突然之间,需要公开的内容数量减少到只有一个干净的 GetCustomerByID 接口。

于 2013-05-16T05:33:38.137 回答