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 );
}
}