Energy-Efficient Query Processing in Web Search Engines
Energy-Efficient Query Processing in Web Search Engines
CHAPTER 9
Admin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class AdminPage : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public AdminPage()
{
InitializeComponent();
}
#region //=========== Execute code to store values in database
=====================
private void bt_add_Click(object sender, EventArgs e)
{
//Check whether user has specified all the contents in textboxes.
if ((tb_descripiton.Text != "") && (tb_keywords.Text != "") && (tb_url.Text != ""))
{
label7.Visible = label8.Visible = label9.Visible = false;
if (conn.State == ConnectionState.Closed)
conn.Open();
//Check whether the url already exist in database or not
SqlCommand cmd = new SqlCommand("select * from sites where url='" + tb_url.Text
+ "'", conn);
SqlDataReader dr_submitdetails = cmd.ExecuteReader();
if (dr_submitdetails.HasRows)
{
MessageBox.Show("URL already exists");
dr_submitdetails.Close();
tb_descripiton.Text = tb_keywords.Text = tb_url.Text = "";
}
else
{
dr_submitdetails.Close();
SqlCommand cmd_insert;
if (rb_content.Checked)
//insert values into sites table url,desc,key,rank,date
cmd_insert = new SqlCommand("insert into
sites(url,description,keywords,type,ranking,datecreated )values('" + tb_url.Text + "','" +
tb_descripiton.Text + "','" + tb_keywords.Text + "','CB','0','" + DateTime.Now + "')", conn);
else
cmd_insert = new SqlCommand("insert into
sites(url,description,keywords,type,ranking,datecreated )values('" + tb_url.Text + "','" +
tb_descripiton.Text + "','" + tb_keywords.Text + "','LB','0','" + DateTime.Now + "')", conn);
cmd_insert.ExecuteNonQuery();
MessageBox.Show("Records Inserted Successfully", "Success");
}
}
else
label7.Visible = label8.Visible = label9.Visible = true;
}
#endregion
#region //=========== Navigate to view Database windows ====================
private void bt_view_Click(object sender, EventArgs e)
{
DatBase frm_DB = new DatBase();
frm_DB.ShowDialog();
}
#endregion
#region //============ When admin window is closed navigate to login window
==========
private void AdminPage_FormClosing(object sender, FormClosingEventArgs e)
{
LoginPage Login = new LoginPage();
Login.Show();
}
#endregion
private void btnAddUsr_Click(object sender, EventArgs e)
{
AddUser newuser = new AddUser();
newuser.ShowDialog();
}
}
}
New user
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class AddUser : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public AddUser()
{
InitializeComponent();
}
private void AddUser_Load(object sender, EventArgs e)
{
userid();
}
private void btnAddUser_Click(object sender, EventArgs e)
{
if ((txtUsrName.Text != "") && (txtpass.Text != "") && (txtConPass.Text != ""))
{
lblerr1.Visible = lblerr2.Visible = lblerr3.Visible = false;
if ((txtpass.Text) == (txtConPass.Text))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into logindetails values('" +
txtUid.Text + "','" + txtUsrName.Text + "','" + txtpass.Text + "','user')", conn);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
MessageBox.Show("New User Added Successfully","DataBase");
cmd.Dispose();
conn.Close();
userid();
txtUsrName.Text = "";
txtpass.Text = "";
txtConPass.Text = "";
}
else
{
MessageBox.Show("Confirmation of Password Mismatch","Alert!!");
}
}
else
{
lblerr1.Visible = lblerr2.Visible = lblerr3.Visible = true;
}
}
public void userid()
{
int uid;
conn.Open();
SqlCommand cmd = new SqlCommand("select max(uid) from logindetails", conn);
txtUid.Text = cmd.ExecuteScalar().ToString();
uid = Convert.ToInt32(txtUid.Text) + 1;
txtUid.Text = uid.ToString();
cmd.Dispose();
conn.Close();
}
}
}
Database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class DatBase : Form
{
#region //============= Globla declaration of connection string
=============================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public DatBase()
{
InitializeComponent();
}
#region //============== Load all data from database to display in grid
====================
private void DatBase_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("select id as LinkID,url as URL, description as
Description,keywords as Keywords, type as Type,ranking as Ranking,datecreated as
DateCreated from sites", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
DgView_DetaildDB.DataSource = ds.Tables[0].DefaultView;
if (DgView_DetaildDB.Rows.Count == 1)
MessageBox.Show("No Records Found", "Records not Found");
if (conn.State == ConnectionState.Open)
conn.Close();
}
#endregion
#region //============= Delete selected link from database
===========================
private void bt_delselectd_Click(object sender, EventArgs e)
{
if (DgView_DetaildDB.SelectedRows.Count > 0)
{
string sz_sno =
Convert.ToString(DgView_DetaildDB.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Delete from sites where id='" + sz_sno +
"'", conn);
cmd.ExecuteNonQuery();
foreach (DataGridViewRow dr in DgView_DetaildDB.SelectedRows)
{
DgView_DetaildDB.Rows.Remove(dr);
}
MessageBox.Show("Deleted Successfully");
if (conn.State == ConnectionState.Open)
conn.Close();
}
else
MessageBox.Show("Please select Valid Data");
}
else
MessageBox.Show("Please select any Data");
}
#endregion
}
}
Login
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class LoginPage : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public LoginPage()
{
InitializeComponent();
}
}
#region //======= End Application when form is closed
===========================
private void LoginPage_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
#endregion
}
}
User History
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class UserHistory : Form
{
#region //================= Global declaration of variable to store in query
==================
public static string sz_link;
public static string desc = "";
public string linkid = "";
#endregion
#region //================= Global declaration SQL connection
==================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public UserHistory()
{
InitializeComponent();
}
#region //=========== Load user browsed history in grid and diaplay =============
private void UserHistory_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("select linkid as LinkID,uid as UserID,url as
URL, description as Description,date as LastAccess from usrhistry where uid='" + LoginPage.id
+ "'", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No History Found", "Records not Found");
btnLink.Enabled = false;
}
if (conn.State == ConnectionState.Open)
conn.Close();
}
#endregion
#region //============ Clear all the history for the particular user ============
private void btnHistory_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount == 0)
{
MessageBox.Show("History Cleared", "No Records Found");
btnLink.Enabled = false;
}
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("delete from usrhistry where uid='" +
LoginPage.id + "'", conn);
cmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
btnLink.Enabled = false;
}
#endregion
#region //======== Goto Link to which user clicked ====================
private void btnLink_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
// Get link,desc and link id to store in user history
string sz_sno = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value);
desc = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value);
linkid = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
sz_link = sz_sno;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Select ranking from sites where id='" +
dataGridView1.SelectedRows[0].Cells[0].Value + "'", conn);
int n_rnk = Convert.ToInt32(cmd.ExecuteScalar());
n_rnk++;
SqlCommand cmd_update = new SqlCommand("Update sites set ranking = " +
n_rnk + " where id='" + dataGridView1.SelectedRows[0].Cells[0].Value + "'", conn);
cmd_update.ExecuteNonQuery();
#region //====== store history of the user ========
//Call user function to update user record
user();
#endregion
if (conn.State == ConnectionState.Open)
conn.Close();
Engine_Data frm_engin = new Engine_Data();
frm_engin.ShowDialog();
}
else
MessageBox.Show("Please select Valid URL");
}
else
MessageBox.Show("Please select any URL");
#endregion
#region //========== Save history of the user ===============
public void user()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
con.Open();
string query = "insert into usrhistry values ('" + linkid + "','" + LoginPage.id + "','" +
sz_link + "','" + desc + "','" + DateTime.Now + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
#endregion
}
}
Userpage
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class UserPage : Form
{
#region //========== Global declaration for sql connection =====================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
#region //=========== Global declaration of variables for user history =======
public static string sz_link;
public static string desc = "";
public static string key = "";
public static string linkid = "";
#endregion
public UserPage()
{
InitializeComponent();
}
#region //========= Search the keywork in database ======================
private void bt_search_Click(object sender, EventArgs e)
{
if (tb_search.Text != "")
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sz_like = "%" + tb_search.Text + "%";
SqlCommand cmd; //= new SqlCommand("select id as ID,url as URL, description as
Description from searches where description like '"+sz_like+"' order by ranking desc", conn);
if (rb_content.Checked)
cmd = new SqlCommand("select id as ID,url as URL, description as Description
from sites where keywords like '" + sz_like + "' and type='CB' order by ranking desc", conn);
else
cmd = new SqlCommand("select id as ID,url as URL, description as Description from sites
where keywords like '" + sz_like + "' and type='LB' order by ranking desc", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
DgView_Links.DataSource = ds.Tables[0].DefaultView;
//DataGrid.Columns(0).HeaderText = MyTextBox.Text
if (DgView_Links.Rows.Count == 1)
MessageBox.Show("No Records Found", "Records not Found");
if (conn.State == ConnectionState.Open)
conn.Close();
}
else
{
MessageBox.Show("Please Enter Search Keyword", "Search Warning");
}
}
#endregion
#region //========== Goto the link to which user selected ==============
private void bt_link_Click(object sender, EventArgs e)
{
if (DgView_Links.SelectedRows.Count > 0)
{
//get link,desc and link id to save user history
string sz_sno = Convert.ToString(DgView_Links.SelectedRows[0].Cells[1].Value);
desc = Convert.ToString(DgView_Links.SelectedRows[0].Cells[2].Value);
linkid = Convert.ToString(DgView_Links.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
sz_link = sz_sno;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Select ranking from sites where id='" +
DgView_Links.SelectedRows[0].Cells[0].Value + "'", conn);
int n_rnk = Convert.ToInt32(cmd.ExecuteScalar());
n_rnk++;
SqlCommand cmd_update = new SqlCommand("Update sites set ranking = " +
n_rnk + " where id='" + DgView_Links.SelectedRows[0].Cells[0].Value + "'", conn);
cmd_update.ExecuteNonQuery();
}
}
9.2 SCREEN SHOT