65.9K
CodeProject 正在变化。 阅读更多。
Home

基于条形码检索/更新 SQL 记录

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (5投票s)

2014年6月27日

CPOL

4分钟阅读

viewsIcon

31219

downloadIcon

1045

使用条形码从数据库提取记录,并使用此精巧的可配置工具更新相同或其他记录。

引言

还记得我的文章 "如何在 SSRS 报告中嵌入条形码" 吗?是的,让我们继续这个想法。

这个工具让您能够利用打印的条形码在自动化流程中手动干预,如有需要。例如,根据条形码内容从特定数据库中提取特定记录,然后按需更新一些扫描的记录或其他数据。

是的,也许我的许多亲爱的读者会认为整篇文章毫无用处,因为它不符合他们一直在寻找的东西。非常确定!但肯定有些人会喜欢它,因为它已经准备好使用,只需正确配置,并假定有真实的用例场景,可以即时使用,而不是从头开始自己做。所以,如果它不是您想要的,请不要给这篇文章差评。对于能够使用它的人来说,它的价值是 5/5!;-)

背景

想象一下,您已将条形码打印在您发出的每一封信函/订单/发票上。现在,其中一些因收件人已搬家而退回给您,显示为无法送达。现在,您的员工必须费力地打开这些信封,输入系统,并将信函标记为退回且无法送达。或者在寄出邮资时,您可能已打印了所有标签,信件也已准备好寄出,但在交给邮递员时,您想给它们一个最终更新。

所以,这个工具将允许您立即做到这一点。只需通过信封窗口或您为其条形码的任何地方扫描您的物品。该工具将从您的数据库中检索相应的记录并相应地更新它们。

这假设了一个条形码格式(您可以更改这一点),其中包含一个字符串,指示要使用哪个已知数据库(可配置),然后查询该数据库以获取特定记录(ID 也来自条形码),并将记录显示在 Grid 中。

然后,勾选或取消勾选要更新的记录,然后单击提交。

因此,这个工具可以重复用于各种“犯罪”活动,只需使用条形码即可检索和更新某些数据库记录。您可以为各种目的设置多个配置,并为每个目的保留该应用程序的副本,或者更进一步,对其进行修改以便从配置中进行选择。

使用代码

此工具的 app.config 是使其工作的关键!

您可以添加一个或多个 SQL 连接字符串。配置您的窗体的标题,每条记录前面的复选框列的列名,来自您扫描的条形码的逗号分隔的键,这些键在记录 ID 之前,用于确定是查询源数据库还是源数据库 1 的 Database/CustomerData,获取请求记录的 SQL 命令,以及提交按钮发出后用于根据此记录更新此记录或其他记录的更新命令。在下面的示例中,格式为 Code39Extended 且内容为 *ORA1234* 的条形码将使应用程序使用连接字符串 DB1,然后选择 ID 为 @ItemID=1234 的记录并在 datagridview 上显示它。足够简单吧?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <clear/>
    <add name="DB" connectionString="YourConnectionString" providerName="System.Data.SqlClient" />
    <add name="DB1" connectionString="YourOptional2DatabaseConnectionString" providerName="System.Data.SqlClient" />

  </connectionStrings>
  <appSettings>
    <add key ="Title" value ="Barcode Handler"/>
    <add key ="CheckColumnName" value ="Invalidate Address"/> <!-- the first column is a checkbox so you can check or uncheck to have that one updated or not //-->
    <add key ="DB" value ="ORD,ORF"/> <!-- Key name matching the first connection STring//-->
    <add key ="DB1" value="ORA,ORB"/> <!-- Optional Key name matching the second connection STring//-->
    <add key ="RetrieveCommand_ID_Column_No" value ="1"/>
    <!-- @ItemID is the ID that is returned and used from the second portion of the
    Barcode Your Barcode should contain a value from DB or DB1 plus the item ID enclosed by *
    i.e. *ORA1234* and 1234 would be the ItemID or OrderId if you like
    DB and DB1 just signify to query DB x or DB y depending which customer it is, if you splitted the data accross multiple DBs
   
    RetrieveCommand finds a record and puts it in the datagrid
    UpdateCommand could be multiple Updates semi colon separated using @ItemID to select the records to be updated with whatever status or SQL logic you can configure here
   //-->
    <add key="RetrieveCommand"  value="SELECT top 1 'ORA' as DB,'35295' ItemID,'2014-01-01 00:34:23' Orderdate , 'Sent' as Status, 'Peter' as [First name] ,'Pan' as [Last name] ,'Treehouse 1' as [Address],'Neverland' as [City],'Xmas Presents' as [Description] FROM orders where @ItemID=orderID"/>
    <add key ="UpdateCommand" value="UPDATE ORDER_STATUS SET STATUS = 'Returned Mail (Undeliverable)' ,statusdate = getdate() ,updated_by = 'BarcodeHandler' , updated_date = getdate() WHERE OrderID = @ItemID ; UPDATE m SET incorrect_address_flag = 'Y' FROM customer m inner joine orders ......... orderid= @ItemID ; "/>
  </appSettings>
</configuration>

看看我是如何将 RetrieveCommand 和 UpdateCommand 硬编码为不可用内容的。好吧,您将无法使用我使用的内容,因为它取决于您的特定数据库架构,但重点是您可以在此处配置您的 SQL 命令,这些命令可以检索单个记录并更新单个记录,使用 @ItemID 参数,该参数可以引用您的 orderID 或 fulfillmentID 或您使用的任何其他内容。其他字段只是为了显示一些数据,以便用户能够识别和验证检索到的记录是否与刚刚扫描的项匹配。

RetrieveCommand_ID_Column_No 是 0 基索引,应指向您的 select(检索命令)中包含与条形码后半部分匹配的实际 ID 的列号。

关注点

我让工具动态地从 select 语句中检索 gridview 列名,所以也不用担心配置它。

private void SetColumns()

当然,您也可以只使用一个数据库运行此程序,没问题。只需删除第二个连接字符串。

请确保在连接字符串中保留 </clear> 以避免尝试打开未标记的“LocalSqlServer”连接。

ScannerTextBox 类使条形码扫描器易于使用,可以自动扫描并发出记录检索,而无需单击任何额外的按钮。我遵循了很久以前在某个论坛上的一个想法(不幸的是我不记得是谁了 - 如果是您,请告诉我,我会在这里公平地提及您。;-)),使用一个计时器来测量输入和来自扫描器的后续“\r”。在我看来,这是几年前从某个在线拍卖网站上购买的 10 美元的最便宜的 USB 扫描器。它的表现就像一把魔法键盘。

  public class ScannerTextBox : TextBox
    {
        public event ChangedEventHandler BarCodeEntered;

        // Invoke the Changed event; called whenever list changes
        protected virtual void OnChanged(EventArgs e)
        {
        }

        public bool BarcodeOnly { get; set; }

        Timer timer;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (BarcodeOnly == true)
            {
                Text = "";
            }
            timer.Enabled = false;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (BarcodeOnly == true)
            {
                if (timer == null || !timer.Enabled )
                    Text = "";

                if (timer == null)
                {
                    timer = new Timer();
                    timer.Interval = 200;
                    timer.Tick += new EventHandler(timer_Tick);
                    timer.Enabled = false;
                }
                timer.Enabled = true;
            }

            if (e.KeyChar == '\r')
            {
                if (BarcodeOnly == true && timer != null)
                {
                    timer.Enabled = false;
                    if (BarCodeEntered != null)
                    {
                        if (this.Text.Length > 3 && !String.IsNullOrEmpty(this.Text))
                        {
                            BarCodeEnteredEventArgs e1 = new BarCodeEnteredEventArgs(this.Text);
                            BarCodeEntered(this, e1);
                        }
                    }
                }
            }
        }
    }

历史

暂无历史记录。

© . All rights reserved.