DataBase返回在启动时起作用,但在更新时不起作用

时间:2013-05-16 05:01:04

标签: c# mysql database

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

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

当我输入一个值时,按Enter键后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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

修改

您需要以更专业的方式考虑您的职能。目前您有两方和一个交易。例如,商家和顾客以及顾客想要购买糖果棒。

您当前的行为是:

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; 
    }
}

您的表单负责从您的用户实际获取该号码并将结果放回用户界面:

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界面。