【问题标题】:c# Auto populate result of a textboxt.Text from database, base on 2 different combo boxc# 基于 2 个不同的组合框,从数据库中自动填充 textbox.Text 的结果
【发布时间】: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_citydestination_city 中的值作为 string,使用 comboBox1_SelectedIndexChangedcomboBox2_SelectedIndexChanged

然后在方法autopopulatedays() 上,我尝试通过匹配数据库上的original_citydestination_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_citydestination_city 的值是否正确?
  • 在你的连接字符串中仍然看不到Database=sth

标签: c# mysql sql visual-studio


【解决方案1】:

首先使用有效的ConnectionString

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;

密码=我的密码;

那我想这不是你的实际代码,因为缺少大括号

public partial class AddingNewData : Form
{
    public AddingNewData()
    {
        InitializeComponent();
        fillcombo1();
        fillcombo2();
        autopopulatedays();
}

最后,当组合框 1 或组合框 2 选择的项目发生变化时,您应该 `autopopulatedays',而不仅仅是在填充组合时。

【讨论】:

  • 我刚刚更新我的帖子,连接没有问题了,很好。请帮我更正更新版本的代码。
【解决方案2】:

我把上面的代码解成了下面这样的代码=

namespace Training
{
    public partial class AddingNewData : Form
    {
        public AddingNewData()
        {
            InitializeComponent();
            fillcombo1();
            fillcombo2();
    }

    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()
{
    // EMPTY
}

// just some improvement on query
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    original_city = comboBox1.SelectedItem.ToString();

    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT DISTINCT dest_city FROM itemdelivery.fee WHERE orig_city = '" + original_city + "' GROUP BY destination_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);
    }
}

// this is where I solved the problem
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    destination_city = comboBox2.SelectedItem.ToString();

    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 + "';";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);

        try
        {
            conDataBase.Open();

            var result = cmdDataBase.ExecuteScalar();
                if (result != null)
                {
                    txt_deliverytime.Text = result.ToString();
                }

            conDataBase.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

private void txt_deliverytime_TextChanged(object sender, EventArgs e)
{
}

}

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    相关资源
    最近更新 更多