如何在每次交易后更新库存表以自动完成等

本文关键字:交易 更新 | 更新日期: 2023-09-27 18:24:41

如何在每次事务(即保存数据等)后用数据库更新我的表单自动完成源。我无法自动完成我刚刚保存的数据,直到我关闭应用程序并重新打开它

我使用的是C#,winform,SQLite:

public DataForm() { 
   InitializeComponent(); 
   AutoCompleteTextPlot(); 
   AutoCompleteTextLocation(); 
   AutoCompleteTextSize(); 
   AutoCompleteTextComArea(); 
   AutoCompleteTextRemarks(); 
   AutoCompleteTextOwnerName(); 
   AutoCompleteTextCustomerName(); 
}

如何在每次交易后更新库存表以自动完成等

因此,您在提交表单后似乎不会重新加载数据源。这感觉有点费力,但您可以在成功提交后重新加载所有自动加载提示:

public DataForm()
{
    InitializeComponent();
    InitializeAutocomplete(() => InitializeAutocomplete(tbName, "OwnerName", "Owners"));
    InitializeAutocomplete(() => InitializeAutocomplete(tbRemarks, "RemarkText", "Remarks"));
}
List<Action> autoCompleteInitializers = new List<Action>();
private void ReinitializeAutocomplete()
{
    foreach(var initializer in autoCompleteInitializers)
    {
        initializer();
    }
}
private void InitializeAutocomplete(Action init)
{
    init();
    autoCompleteInitializers.Add(init);
}
private void InitializeAutocomplete(TextBox tb, string fieldName, string tableName)
{
    tb.AutoCompleteMode = AutoCompleteMode.Suggest;
    tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
    tb.AutoCompleteCustomSource = new AutoCompleteStringCollection();
    using (SQLiteConnection dbconn = new SQLiteConnection())
    {
        dbconn.Open();
        using (SQLiteCommand dbcmd = dbconn.CreateCommand())
        {
            dbcmd.CommandText = String.Format("select {0} from {1}", fieldName, tableName);
            dbcmd.CommandType = CommandType.Text;
            using (SQLiteDataReader reader = dbcmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    tb.AutoCompleteCustomSource.Add(reader[fieldName].ToString());
                }
            }
        }
    }
}
private void btnSubmit_Click(object sender, EventArgs e)
{
    if (bTransactionSuccess) ReinitializeAutocomplete();
}