简体   繁体   English

使用组合框项目更新数据库中的字段

[英]update field in database with combobox item

i have data base and repository class plus interface like that\\now i wana use some combobox with static items liek 100 200 300.. and whene i choose one of those item and prees the button i created before database update charge field(the field name in database) my connection is ok i dont know how code the button我有数据库和存储库类加上这样的界面\\现在我想使用一些带有静态项目的组合框,例如 100 200 300 .. 当我选择这些项目之一并按下我在数据库更新费用字段(字段名称)之前创建的按钮时在数据库中)我的连接没问题我不知道按钮是如何编码的

class apartment : Interfaceapartment
{
    private string Connection = "Data Source=.;initial catalog=apartment;integrated security=true";

    public bool update(int personID, int charge)
    {
        SqlConnection Conecting = new SqlConnection(Connection);
        try
        {
            string query = "Update myapartment Set charge=@charge Where personID=@ID";
            SqlCommand command = new SqlCommand(query, Conecting);
            command.Parameters.AddWithValue("ID", personID);
            command.Parameters.AddWithValue("charge", charge);
            Conecting.Open();
            command.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
        finally
        {
            Conecting.Close();
        }
}

您只需要获取 ComboBox 的选定值并将其传递给您的 update() 函数。

ChangedValue= combobox.SelectedValue.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM