C# 数据库返回在启动时起作用,但在更新时不起作用
所以我有一个有趣的问题。。。我有一个运行的应用程序,但需要“OrderID.text”中的值。然后在MySQL数据库中搜索该值,并返回我需要的信息。在这种情况下,请输入电话号码 当我在代码中包含值时。(例如,newCurrentID=8),它返回电话号码。但是,我希望用户能够输入自己的值,并且电话号码会根据用户的偏好进行更改 当我输入一个值时,消息框在我按enter键后工作,但电话的文本框保持空白。有什么想法吗 我只包括与此相关的代码C# 数据库返回在启动时起作用,但在更新时不起作用,c#,mysql,database,C#,Mysql,Database,所以我有一个有趣的问题。。。我有一个运行的应用程序,但需要“OrderID.text”中的值。然后在MySQL数据库中搜索该值,并返回我需要的信息。在这种情况下,请输入电话号码 当我在代码中包含值时。(例如,newCurrentID=8),它返回电话号码。但是,我希望用户能够输入自己的值,并且电话号码会根据用户的偏好进行更改 当我输入一个值时,消息框在我按enter键后工作,但电话的文本框保持空白。有什么想法吗 我只包括与此相关的代码 public partial class Form1: {
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;
}
}
没有迹象表明表单会知道数据库类中的电话号码已更改。考虑一个设计,您的数据库应用程序不知道表单。您可以将参数传递给函数(newCurrentID),还可以将值返回到表单(phonenumber),然后表单需要将其放入文本字段中 编辑:
void Purchase();
CandyBar Purchase(money);
你需要用更专业的术语来思考你的职能。目前,您有两个交易方和一笔交易。例如,一个商人和一个顾客,顾客想要买一块糖果
您当前的行为是:
void Purchase();
CandyBar Purchase(money);
在幕后,商人把糖果塞到顾客的喉咙里,另一只手试图用手指从顾客的钱包里掏钱。两人都认识对方,都记下了对方是谁。如果一个商人是那样的话,我也会记笔记
您需要实施的是专业交易:
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;
}
}
}
请注意,突然之间,需要公开的内容数量减少到只有一个干净的GetCustomerByd界面。您可以将
这个.phoneNumber\u TextChanged
方法包括在内吗?是的,对不起。不过目前它还是空白的。一个空白的方法。。。我认为你是对的。我试了点东西。你建议我做什么?我正在尝试一些东西,看看它们是否有效。添加了一些建议。WOOT最后谢谢你!你知道吗,当你在同一件事情上工作了5个小时,当它开始工作后,你会有这种感觉??是的,我现在有这种感觉:)