如何从数据库中检索数据并将其显示

如何从数据库中检索数据并将其显示

本文介绍了如何从数据库中检索数据并将其显示在标签上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用幸运抽奖系统的注册页面。因此,注册过程是用户需要使用USB条形码扫描器扫描他们的条形码。我坚持使用用户扫描后无法检查数据库上的员工ID或输入文本框的那部分。我还希望在扫描条形码后显示员工姓名。任何人都可以给我一些想法/解决方案。



我尝试过:



以下是我的代码:



I'm working on the Registration page for Lucky Draw system.So the process for registration is user need to scan their barcode using usb barcode scanner. I stuck on that part which i cannot check the employee id on database after user scan or type in the textbox. I also want to display the employee name after scan the barcode.Can anyone give me some ideas/solution.

What I have tried:

Below are my code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Attendance : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string str;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = @"Data Source= (LocalDB)\MSSQLLocalDB; AttachDbFilename =
          C:\Users\Nor  Asyraf  Mohd No\source\repos\LuckyDraw\LuckyDraw\App_Data\ticket.mdf;
            Integrated Security = True";
        txtText.Focus();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        idcheck();
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = "UPDATE EMPLOYEES SET Attendance = 'Present' WHERE EMP_ID = @id";
                command.Parameters.AddWithValue("@id", txtText.Text);
                connection.Open();

                command.ExecuteNonQuery();

                connection.Close();
            }
        }
    }

public void idcheck()
        {
        string query = "select EMP_ID from EMPLOYEES where EMP_ID='" + txtText.Text + "'";
        SqlDataAdapter ada = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        ada.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            lblError.Text = dt.Rows[0]["EMP_NAME"].ToString();
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}

推荐答案




这篇关于如何从数据库中检索数据并将其显示在标签上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:40