How to tell if a value exists in a database in C #

Source: Internet
Author: User

Select a column, such as a user number column

User number to insert
String ll_userid= "xxxxxxxx";

Query whether this number exists
SqlCommand mycmd = new SqlCommand ("Select User number from table where user number = '" +ll_userid+ "'", mycon);

SqlDataReader MYSDR = myCMD. ExecuteReader ();

if (MYSDR. HasRows)
{
There are already records using this number
}
Else
{
This number is not being used
}

MYSDR. Close ();
Mycon. Close ();

----------If the user number column is an integer type, you can also write this

int Ll_userid;

Remove the maximum number value from the current record
SqlCommand mycmd = new SqlCommand ("Select max (user number) from table", mycon);

SqlDataReader MYSDR = myCMD. ExecuteReader ();

It also detects if a record is intended to determine if the table is empty
if (MYSDR. HasRows)
{
Table is not empty, based on the maximum number of queries obtained +1, and then insert a new record
}
Else
{
Table is empty, a default minimum number record is inserted
}

MYSDR. Close ();
Mycon. Close ();

How to tell if a value exists in a database in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.