我知道之前可能已经被问过并回答过了。但我无法在我的问题中使用它们,因为我是新手wpf并在c#中连接数据库。
这就是问题:我有一个mysql数据库,它的表有2列国家和地区。我从组合框中选择国家,这很好。我想根据国家选择地区。我使用2个组合框来选择国家和地区。
当我选择一个国家时,仍然无法加载区。我认为我的sql字符串和wpf事件处理可能会引发问题。
string new_student_sql = "SELECT Id,Name FROM world.city WHERE
CountryCode = '";
string connectionStr = new_student_sql + comboBoxCountry.Text +
"' AND District= '" + comboBoxDistrict.Text + "'";
MySqlCommand newStudent = new MySqlCommand(connectionStr, conn);
newStudent.CommandText = connectionStr;
conn.Open();
DataTable dt = new DataTable();
dt.Load(newStudent.ExecuteReader());
conn.Close();
dataGrid.DataContext = dt;
MessageBox.Show(connectionStr);
我将组合框作为国家/地区值获取为comboBoxCountry.Text。但是当我在comboBoxDistrict选择改变方法中给出该值时,我在上面列出了一个空的组合框。
我应该如何根据第一个选择来填充它们。任何建议和代码样本都会非常感激。 在此先感谢。!
答案 0 :(得分:0)
string connectionStr = new_student_sql + comboBoxCountry.Text +
"' AND District= '" + comboBoxDistrict.Text + "'";
使用varible,
String country= comboBoxCountry.SelectedValue.ToString();
使其更易于使用。 感谢任何试图解决这个问题并指导我使用MVVM模式的人,我肯定会给出一个机会。