【发布时间】:2016-11-13 13:59:10
【问题描述】:
namespace Training
{
public partial class AddingNewData : Form
{
public AddingNewData()
{
InitializeComponent();
fillcombo1();
fillcombo2();
autopopulatedays();
}
string original_city, destination_city;
void fillcombo1()
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string Query = "SELECT * FROM itemdelivery.fee GROUP BY orig_city;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string storig = myReader.GetString("orig_city");
comboBox1.Items.Add(storig);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void fillcombo2()
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string Query = "SELECT * FROM itemdelivery.fee GROUP BY dest_city;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string stdest = myReader.GetString("dest_city");
comboBox2.Items.Add(stdest);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
original_city = comboBox1.Text;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
destination_city = comboBox2.Text;
}
private void txt_deliverytime_TextChanged(object sender, EventArgs e)
{
}
void autopopulatedays()
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string Query = "SELECT `del_time` FROM `itemdelivery.fee` WHERE `orig_city` = @oc AND `dest_city`= @dc";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
try
{
conDataBase.Open();
cmdDataBase.Parameters.AddWithValue("@oc", original_city);
cmdDataBase.Parameters.AddWithValue("@dc", destination_city);
object result = cmdDataBase.ExecuteScalar();
if (result != null)
txt_deliverytime.Text = result.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
所以,我有 2 个组合框,选择其中 2 个后,我将 original_city 和 destination_city 中的值作为 string,使用 comboBox1_SelectedIndexChanged 和 comboBox2_SelectedIndexChanged。
然后在方法autopopulatedays() 上,我尝试通过匹配数据库上的original_city 和destination_city 的值,使用单个整数值自动填充txt_deliverytime.Text
那个查询。
但是,为什么我失败了?我得到的唯一错误是"No database selected",这对我来说很奇怪,当我选择 2 个组合框选项时,
txt_deliverytime.Text 不会自动填充。
================================================ ==========================
// UPDATE VERSION
// Connected to database, and ignoring the code before(above),
// This code only meant to auto-populate txt_deliverytime.Text when
// combobox1.selectitem and combobox2.selectitem
// Why it's still wrong?.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
original_city = comboBox1.SelectedItem.ToString();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
destination_city = comboBox2.SelectedItem.ToString();
// if the combobox1 is not empty and combobox2 is also not empty, then run this code
if (comboBox1.SelectedItem.ToString() != null)
{
string constring = "datasource=localhost;port=3306;username=root;password=root";
string Query = "SELECT del_time FROM itemdelivery.fee WHERE orig_city='" + original_city + "' AND dest_city='" + destination_city + "';";
// if I run this query on MySQL, it will show only a column name del_time with only a single row,
// thus only show a value, I want to get that value to txt_deliverytime.Text
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
conDataBase.Open();
string getValue = cmdDataBase.ExecuteScalar().ToString();
if (getValue != null)
{
txt_deliverytime.Text = getValue.ToString();
// meant to change it here, but seems not successful
}
conDataBase.Close();
}
}
【问题讨论】:
-
您需要在连接字符串或查询中提供数据库名称
-
如果我删除 autopopulatedays 方法,我不会收到“未选择数据库”错误。所以@viveknuna,你能告诉我如何做你的建议吗? (又名代码)
-
我刚刚更新我的帖子,连接没有问题了,很好。请帮我更正更新版本的代码。
-
你调试过
comboBox2_SelectedIndexChanged(...)吗?original_city和destination_city的值是否正确? -
在你的连接字符串中仍然看不到
Database=sth
标签: c# mysql sql visual-studio