Determine the existence of database data and database data

Source: Internet
Author: User

Determine the existence of database data and database data

Purpose: To determine whether a category name already exists
1. Write code to SQLHelper In the DAL layer:

/// <Summary>
/// This method executes the passed SQL query statement
/// </Summary>
/// <Param name = "SQL"> SQL query statement </param>
/// <Returns> </returns>
Public DataTable ExecuteQuery (string SQL)
{
DataTable dt = new DataTable ();
String connStr = @ "server = SIKU; database = newssystem; uid = sa; pwd = 123456 ";
SqlConnection conn = new SqlConnection (connStr );
Conn. Open ();
SqlCommand cmd = new SqlCommand (SQL, conn );
SqlDataReader sdr = cmd. ExecuteReader ();
Dt. Load (sdr );
Sdr. Close ();
Conn. Close ();
Return dt;
}
2. Write code in CategoryDAO at the DAL layer:
/// <Summary>
/// Determine whether the category name already exists
/// </Summary>
/// <Param name = "caName"> category name </param>
/// <Returns> </returns>
Public bool IsExists (string caName)
{
Bool flag = false;
String SQL = "select * from category where [name] = '" + caName + "'";
DataTable dt = new DataTable ();
SQLHelper sqlhelper = new SQLHelper ();
Dt = sqlhelper. ExecuteQuery (SQL );
If (dt. Rows. Count> 0)
{
Flag = true;
}
Return flag;
}
3. In the Web layer Default. cs, enter:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Bind the GridView
GridView1.DataSource = new CategoryDAO (). SelectAll ();
GridView1.DataBind ();

}
// Button event
Protected void button#click (object sender, EventArgs e)
{
String caName = TextBox1.Text;
Bool B = new CategoryDAO (). IsExists (caName );
Response. Write (B );
}
}

Related Article

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.