如何使用 mysql 数据库存储过程填充组合框

How to populate combobox with mysql database stored procedures

我有一个带有标签和文本框的 winform,用于将数据保存到 mysql 数据库,我添加了一个组合框来从我的存储过程中检索信息,该存储过程显示 2 列 ID 和名称。我的问题是如何使用存储过程的结果填充 combobox1。 下面是我的 C# 代码

DELIMITER $$

USE `sms_pigen`$$

DROP PROCEDURE IF EXISTS `sp_clearing_agent`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_clearing_agent`()
BEGIN
SELECT clearing_agent_id, clearing_agent_name
FROM tms_clearing_agents;
    END$$

DELIMITER ;

public partial class frmNewClient : Form
{
    MySqlConnection connection;
    MySqlCommand cmd;
    MySqlDataAdapter adp;
    DataSet cb = new DataSet();
    public frmNewClient()
    {
        InitializeComponent();
    }

    private void frmNewClient_Load(object sender, EventArgs e)
    {
    //    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

    //    connection = new MySqlConnection(connectionString);
    //    connection.Open();

    //    DataSet cb = new DataSet();

    }

    private void btnSubmitClients_Click_1(object sender, EventArgs e)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

        connection = new MySqlConnection(connectionString);
        connection.Open();

        try
        {
            string CmdText = "INSERT INTO t_pi_Clients(ClientCode,ClientName,PostalAdd,Telephone,Fax,EmailAdd1,EmailAdd2,EmailAdd3,Website,TotalDeposit,AccountBal,ChargeRate)VALUES(@ClientCode, @ClientName, @PostalAdd, @Telephone, @Fax, @EmailAdd1, @EmailAdd2, @EmailAdd3, @Website, @TotalDeposit, @AccountBal, @ChargeRate)";
            MySqlCommand cmd = new MySqlCommand(CmdText, connection);
            cmd.Parameters.AddWithValue("@ClientCode", txtboxClientCode.Text);
            cmd.Parameters.AddWithValue("@ClientName", txtboxClientName.Text);
            cmd.Parameters.AddWithValue("@PostalAdd", txtboxPostalAddress.Text);
            cmd.Parameters.AddWithValue("@Telephone", txtboxTelephone.Text);
            cmd.Parameters.AddWithValue("@Fax", txtboxFax.Text);
            cmd.Parameters.AddWithValue("@EmailAdd1", txtboxEmailAddress1.Text);
            cmd.Parameters.AddWithValue("@EmailAdd2", txtboxEmailAddress2.Text);
            cmd.Parameters.AddWithValue("@EmailAdd3", txtboxEmailAddress3.Text);
            cmd.Parameters.AddWithValue("@Website", txtboxWebsite.Text);
            cmd.Parameters.AddWithValue("@TotalDeposit", txtboxTotalDepo.Text);
            cmd.Parameters.AddWithValue("@AccountBal", txtboxAccountBal.Text);
            cmd.Parameters.AddWithValue("@ChargeRate", txtboxChargeRate.Text);

            int result = cmd.ExecuteNonQuery();
            MessageBox.Show("Entry Saved");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }

这里是从存储过程中检索数据并将数据绑定到组合框的代码

        DataTable dtTest = new DataTable();
        SqlConnection con=new SqlConnection(ConnectionString);
        con.Open();
        SqlCommand cmd=new SqlCommand();
        cmd.CommandText="sp_clearing_agent";
        cmd.CommandType=CommandType.StoredProcedure;

        SqlDataAdapter sdaTest = new SqlDataAdapter(cmd);
        DataSet dsTest=new DataSet();
        sdaTest.Fill(dsTest);
        //binding data to combobox
        cmbTest.DataSource = dsTest.Tables[0];
        comboBox1.DataSource.DisplayMember="clearing_agent_id"
        comboBox1.DataSource.ValueMember="clearing_agent_name"
        comboBox1.DataBind();
        con.Close();

在您的 Form_Load 命令中,您创建连接和命令以通过存储过程检索数据。必须将 CommandType 设置为 StoredProcedure,然后将此命令作为 SelectCommand 传递给 MySqlDataAdapter。使用 Fill 检索数据后,您将 DataTable 设置为组合框的数据源,并将 DisplayMember 和 ValueMember 设置为 table.

的相应字段
private void frmNewClient_Load(object sender, EventArgs e)
{
    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;
    using(MySqlConnection cnn = new MySqlConnection(connectionString))
    using(MySqlCommand cmd = cnn.CreateCommand())
    {
        cnn.Open();
        cmd.CommandText = "sp_clearing_agent";
        cmd.CommandType = CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        using(MySqlDataAdapter da = new MySqlDataAdapter(cmd))
        {
            da.Fill(dt);
            combobox.DisplayMember = "clearing_agent_name";
            combobox.ValueMember = "clearing_agent_id";
            combobox.DataSource = dt;

        }
    }
}

请注意,您不应保留连接和适配器等全局对象。需要时创建,使用后立即销毁