c#中的文本框值更改

时间:2015-04-09 22:39:38

标签: c#

如果文本框值已更改,我如何通过新文本框值而不是旧值从数据库中进行选择?

string constring = "datasource=127.0.0.1;username=root;password=admin";
string Query = "select * from mohamed.usercompany1 where office  =  '" + textBox1.Text + "'  ;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
DataSet DataSet1 = new DataSet();
MySqlDataReader myReader;
try
{
    conDataBase.Open();
    myReader = cmdDataBase.ExecuteReader();
    if (myReader.Read())
    {
        string scode = myReader.GetInt32("techname").ToString();
        textBox1.Text = (string)myReader["techname"];
        comboBox2.Items.Add(scode).ToString();
        listView1.Items.Add(scode).ToString();
        this.Refresh();
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

1 个答案:

答案 0 :(得分:1)

您可以为TextChanged事件添加处理程序。文本更改时会引发它。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    //textBox1.Text contains the new Text now
    //execute your SQL...
}

但你应该小心。可以在TextBox中输入SQL语句,这会损坏数据库。使用参数更安全:
Parameterized Query for MySQL with C#