---
title: "How to read and compare data from an SQL Server Express database"  
description: "How to read and compare data from an SQL Server Express database"  
author: "Anonymous User"  
published: 2014-01-28  
updated: 2014-01-28  
canonical: https://www.mindstick.com/forum/1894/how-to-read-and-compare-data-from-an-sql-server-express-database  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to read and compare data from an SQL Server Express database

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to create a [select statement](https://www.mindstick.com/forum/160076/explain-the-sql-select-statement-and-how-it-s-used-to-search-for-data-in-a-database) in C# to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) the value inserted into a textbox (userName) is in an existing [SQL database](https://www.mindstick.com/articles/337535/connecting-to-sql-databases-ado-dot-net-essentials). I have a database called [Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) containing a table called EVUSERS and it has a column called UName.

In my code I have a method which takes the value from a textbox called UserBox. I would like to know if there is a [temporary table](https://www.mindstick.com/articles/12546/temporary-table-in-sql) where the select is stored which I can compare the [textbox value](https://www.mindstick.com/forum/33830/how-to-calculate-data-grid-value-and-textbox-value-on-change-in-vb-dot-net-winform) to.

## Here is the code:

```
private void CheckLoginExist(){            String userName = UserBox.Text;            string connectionString = @"Data Source=.\SQLEXPRESS;Database=Employee;Integrated Security=true";            using (SqlConnection connection = new SqlConnection(connectionString))            {                using (SqlCommand command = connection.CreateCommand())                {                    command.CommandText = "SELECT UName FROM EVUSERS WHERE UName = @UName";                    command.Parameters.AddWithValue("@UName", userName);                    connection.Open();                    command.ExecuteNonQuery();                    connection.Close();                }            }}
```

## Replies

### Reply by Pravesh Singh

Hi John,\

You need ExecuteScalar, not ExecuteNonQuery

connection.Open();

var name = command.ExecuteScaclar().ToString();

connection.Close();

if (name != null) {

MessageBox.Show("This name already exists");

return;

}


---

Original Source: https://www.mindstick.com/forum/1894/how-to-read-and-compare-data-from-an-sql-server-express-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
