使用oracle数据库自动完成文本框(改进)

时间:2016-03-22 22:51:01

标签: c# oracle oracle11g textbox

  

已更新

   public class PopulateProduct
    {
        public string ProductDesc { get; set; }
        public decimal UnitPrice { get; set; }
    }

    Dictionary<string, PopulateProduct> dict = new Dictionary<string, PopulateProduct>();

    public void load()
    {
        string connstr = "user id=rawpic;password=admin";
        string cmdtxt = @"select PRODUCT_ID,DESCRIPTION,UNIT_PRICE 
                              from products";

        AutoCompleteStringCollection autocom = new AutoCompleteStringCollection();
        TB_PRODUCT_ID.AutoCompleteMode = AutoCompleteMode.Suggest;
        TB_PRODUCT_ID.AutoCompleteSource = AutoCompleteSource.CustomSource;
        TB_PRODUCT_ID.AutoCompleteCustomSource = autocom;

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
        {
            try
            {
                conn.Open();
                OracleDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    dict.Add((string)dr["PRODUCT_ID"],
                        new PopulateProduct()
                        {
                            ProductDesc = (string)dr["DESCRIPTION"],
                            UnitPrice = (decimal)dr["UNIT_PRICE"]
                        });
                    autocom.Add(dr["PRODUCT_ID"].ToString());
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }


    private void TB_PRODUCT_ID_TextChanged(object sender, EventArgs e)
    {
        if (dict.ContainsKey(TB_PRODUCT_ID.Text)) 
        {
            TB_PRODUCTS_DESC.Text = dict[TB_PRODUCT_ID.Text].ProductDesc;
            TB_UNIT_PRICE.Text = dict[TB_PRODUCT_ID.Text].UnitPrice.ToString();
        }
    }
  

------下面的旧剧本-------

我使用下面的代码自动完成textbox值数据 然后自动更改另外两个textboxs的值 产品描述和价格下面的代码工作正常,但 描述textbox值不正确它获取最后一个数据库记录值所以有关它的任何建议吗?

最后是否有更快的方法来完成自动完成?

注意没有必要更新说明和价格textbox立即同步产品ID可能会在用户选择产品ID后发生 喜欢选择如果可能和更快请告诉我如何

    private void TB_PRODUCT_ID_TextChanged(object sender, EventArgs e)
    {
        string connstr = "user id=rawpic;password=admin";
        string cmdtxt = @"select PRODUCT_ID,DESCRIPTION,UNIT_PRICE 
                              from products";

        AutoCompleteStringCollection autocom = new AutoCompleteStringCollection();
        TB_PRODUCT_ID.AutoCompleteMode = AutoCompleteMode.Suggest;
        TB_PRODUCT_ID.AutoCompleteSource = AutoCompleteSource.CustomSource;
        TB_PRODUCT_ID.AutoCompleteCustomSource = autocom;

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
        {
            try
            {
                conn.Open();
                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    autocom.Add(dr["PRODUCT_ID"].ToString());
                    TB_PRODUCTS_DESC.Text = dr["DESCRIPTION"].ToString();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

我已经厌倦了为select语句添加参数,如下所示,但不会有效

        private void TB_PRODUCT_ID_TextChanged(object sender, EventArgs e)
    {
        string connstr = "user id=rawpic;password=admin";
        string cmdtxt = @"select PRODUCT_ID,DESCRIPTION,UNIT_PRICE 
                              from products
                           where PRODUCT_ID=:PRODUCTID";

        AutoCompleteStringCollection autocom = new AutoCompleteStringCollection();
        TB_PRODUCT_ID.AutoCompleteMode = AutoCompleteMode.Suggest;
        TB_PRODUCT_ID.AutoCompleteSource = AutoCompleteSource.CustomSource;
        TB_PRODUCT_ID.AutoCompleteCustomSource = autocom;

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
        {
            try
            {
                conn.Open();
                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    autocom.Add(dr["PRODUCT_ID"].ToString());
                    cmd.Parameters.Add(new OracleParameter("PRODUCTID", TB_PRODUCT_ID.Text));
                    TB_PRODUCTS_DESC.Text = dr["DESCRIPTION"].ToString();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您只是从查询的PRODUCT_ID结果中填充列表。第一个示例代码块显示您正在更新AutoCompleteStringCollection,但您只是使用循环中包含的最后一项覆盖静态文本框的描述。

此外,每次文本更改时,您都会执行这些繁重的代码,这一定非常迟缓。

第二个例子不会起作用,因为你将一个项目放入参数集合中,但是当你在循环完成时处理的一个使用块中(你不会以这种方式使用参数)

一个建议是创建一个类型字典并在循环中填充它。当您的文本框更改时,您可以使用词典[PRODUCT_ID] .Description和.Price - 这将提高应用程序的运行速度,因为您不会每次都返回数据库。

public class MyObject {
  string Description {get;set;}
  decimal UnitPrice {get;set;}
}

public Dictionary<string, MyObject> _dict = new Dictionary<string, MyObject>();

public void Load() 
{
    string connstr = "user id=rawpic;password=admin";
    string cmdtxt = @"select PRODUCT_ID,DESCRIPTION,UNIT_PRICE from products";

    AutoCompleteStringCollection autocom = new AutoCompleteStringCollection();
    TB_PRODUCT_ID.AutoCompleteMode = AutoCompleteMode.Suggest;
    TB_PRODUCT_ID.AutoCompleteSource = AutoCompleteSource.CustomSource;
    TB_PRODUCT_ID.AutoCompleteCustomSource = autocom;       

    using (OracleConnection conn = new OracleConnection(connstr))
    using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
    {
        try
        {
            conn.Open();
            OracleDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                _dict.Add(dr["PRODUCT_ID"], new MyObject() { Description = dr["DESCRIPTION"], UnitPrice = dr["UNIT_PRICE"] };
                autocom.Add(dr["PRODUCT_ID"].ToString());
            }
        }
        catch(Exception ex)
        {
            // handle exception
        }
    }
}

private void TB_PRODUCT_ID_TextChanged(object sender, EventArgs e)
{

if (_dict.ContainsKey(TB_PRODUCT_ID.Text))  
    {
        TB_PRODUCTS_KEY.Text = _dict[TB_PRODUCT_ID.Text].Description;
        TB_UNIT_PRICE.Text = _dict[TB_PRODUCT_ID.Text].UnitPrice.ToString();
    }
}

就像我说的,实现这一目标的一种方法,但没有看到你的其他代码很难分辨它是否是最佳方式。