将文本框值与数据库字段进行比较 [英] Comparing textbox value with database field

查看:394
本文介绍了将文本框值与数据库字段进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



如何将用户输入的文本框值与数据库中的字段进行比较,以检查该值是否存在?我希望用户输入一个条形码,然后系统检查该bacode是否存在 - 用户可以根据结果进行任务处理。



谢谢。

解决方案

首先,您必须建立与数据库的连接。根据数据库的类型,您可以运行类似于/ b

  SELECT  *的SQL查询 FROM  tblBarcodes  WHERE 条形码= '  1234567890' 



我们需要有关用于协助此类连接的数据库的更多信息。也是网络上的数据库,或者它是否在本地PC上运行。


这是算法:



1。读取文本框值

2.将值发送到数据库,使用存储过程执行以下查询以检查值是否存在:



 声明  @ itexists   as   int ; 

如果 存在选择 * 来自 MyTable 其中条形码= @条形码)
set @ itexists = 1;
else
set @ itexists = 0;

print @ itexists ;





3.使用ExecuteScalar获取结果并根据此值执行其余操作





在你的情况下。只需修改您的数据库代码如下:



  public   bool  CompareBarcode( string 条形码)
{
使用(SqlConnection conn = new SqlConnection(ConnectionSTring))
{
SqlCommand cmd = new SqlCommand( uspCheckBarcode,conn);
cmd .CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ barcode,条形码);
return Convert.ToBoolean(cmd.ExecuteScalar());

}


我正在使用SQL Server数据库..我创建了一个存储过程(SELECT Barcode FROM Equipment条形码= @barcode)

-当我执行该查询并输入已注册的条形码时,它会返回它,然后当我输入未注册的条形码时它不会返回任何内容。



>>>数据访问层

 public DataTable CompareBarcode(字符串条形码)
{
using(SqlConnection) conn = new SqlConnection(ConnectionSTring))
{

DataTable dtBarcode = new DataTable();
SqlCommand cmd = new SqlCommand(uspCheckBarcode'+ barcode +',conn);
dbAdapter = new SqlDataAdapter(cmd);
dbAdapter.Fill(dtBarcode);
返回dtBarcode;

}



>>>演示层

  private   void  txtInputBarcode_TextChanged( object  sender ,EventArgs e)
{
// 检查用户输入的条形码是否已注册,通过检查来自数据库

BusinessObject bo = new BusinessObject();
bo.CompareBarcode(txtInputBarcode.Text);

// lblBarcodeResult.Text =条形码注册;



// lblBarcodeResult.Text =条形码未注册;

}



我需要一个if语句来检查是否有条形码,并返回一条lblresult消息


Hi All

How can I go about comparing a textbox value inputted by a user, with a field in a database to check whether that value exists? I want the user to enter a barcode , and the system to check whether that bacode exists or not-and the user may procceed with a task,depending on the result.

Thanks.

解决方案

Firstly you will have to make a connection to the database. Depending on the type of database it is you can then run a SQL query someting similar to

SELECT * FROM tblBarcodes WHERE Barcode = '1234567890'


We will need more information on the database being used to assist with such connection. Also is the database on a network or is it running on the local PC.


Here is the algorithm:

1. Read the textbox value
2. Send the value to database, using stored procedure execute the below query to check whether the values exist or not:

declare @itexists as int;

if exists(select * from MyTable where Barcode=@barcode)
    set @itexists=1;
else
    set @itexists=0;

print @itexists;



3. Get the result using ExecuteScalar and perform rest of the operations based on this value


In your case. just modify your database code as below:

public bool CompareBarcode(string barcode)
       {
           using (SqlConnection conn = new SqlConnection(ConnectionSTring))
           {
               SqlCommand cmd = new SqlCommand("uspCheckBarcode", conn);
               cmd .CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@barcode", barcode);
               return Convert.ToBoolean(cmd.ExecuteScalar());

           }


I am using an SQL Server database.. I created a stored procedure ( SELECT Barcode FROM Equipment WHERE barcode = @barcode)
-when I execute that query and type a registered barcode,it returns it and then when i type an unregistered barcode it doesnt return anything.

>>>Data Access layer

public DataTable CompareBarcode(string barcode)
       {
           using (SqlConnection conn = new SqlConnection(ConnectionSTring))
           {

               DataTable dtBarcode = new DataTable();
               SqlCommand cmd = new SqlCommand("uspCheckBarcode'" + barcode + "'", conn);
               dbAdapter = new SqlDataAdapter(cmd);
               dbAdapter.Fill(dtBarcode);
               return dtBarcode;

           }


>>>Presentation layer

private void txtInputBarcode_TextChanged(object sender, EventArgs e)
       {
           //checks whether barcode entered by user is registered, by checking from the database

               BusinessObject bo = new BusinessObject();
               bo.CompareBarcode(txtInputBarcode.Text);

               //lblBarcodeResult.Text = "Barcode registered";



               //lblBarcodeResult.Text = "Barcode is not registered";

       }


I need an "if" statement that's going to check whether barcode,and return one of the lblresult messages


这篇关于将文本框值与数据库字段进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆