本文主要是介绍c#实现读取条形码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
条形码扫描器销售商品





当利用扫描器扫描条形码时,条形码数据会显示在当前获得焦点的窗口控件中。例如,如果当前编辑框获得焦点,那么条形码数据会显示在TextBox文本框中。然后会向TextBox文本框发送回车键按下时的消息。
在程序中只要触发TextBox文本框的KeyDown事件,判断当前按键是否是回车键,如果是,读取TextBox文本框中的条形码数据,并从数据表中根据条形码查询商品信息,将其显示在DataGridView列表中。


(1)新建一个项目,命名为Ex13_18,默认窗体为Form1。
(2)在Form1窗体中,主要添加TextBox控件,用于接收条形码;添加一个DataGridView控件,用于显示扫描器扫描条形码的商品销售信息。
(3)主要程序代码。
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 13)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "price.mdb" + ";Persist Security Info=False");
OleDbDataAdapter dap = new OleDbDataAdapter("select * from MerchandiseInfo where barcode='" + textBox1.Text + "'", con);
DataSet ds = new DataSet();
dap.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("该商品不存在!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
for (int i = 0; i < dataGridView1.RowCount; i++)
{
if (Convert.ToString(dataGridView1[0, i].Value) == "")
{
dataGridView1[0, i].Value = ds.Tables[0].Rows[0][0].ToString();
dataGridView1[1, i].Value = ds.Tables[0].Rows[0][1].ToString();
dataGridView1[2, i].Value = ds.Tables[0].Rows[0][2].ToString();
dataGridView1[3, i].Value = ds.Tables[0].Rows[0][3].ToString();
return;
}
}
}
}
这篇关于c#实现读取条形码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!