This is the SqlHelper.cs class, and the class encapsulates the method
Using System; Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
<summary>
Summary description of SqlHelper
</summary>
public class SqlHelper
{
Public SqlHelper ()
{
//
TODO: Add constructor logic here
//
}
public static readonly string sqlstr=configurationmanager.connectionstrings["Sqlcnn"]. ConnectionString;
<summary>
Gets the result of the first row of the result set
</summary>
<param name= "SQLText" > Search statement to execute </param>
<param name= "PARAMSS" > Retrieve the argument list in the statement </param>
<returns></returns>
public static Object ExecuteScalar (String sqltext,params sqlparameter[] paramss)
{
Using (SqlConnection sqlcnn=new SqlConnection (Sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn. CreateCommand ())
{
Sqlcmm.commandtext = SQLText;
Fillparam (PARAMSS, SQLCMM);
Sqlcnn. Open ();
Return SQLCMM. ExecuteScalar ();
}
}
}//returns the first row of the lookup column
<summary>
Fill the Command object's argument list
</summary>
<param name= "PARAMSS" > Parameter list </param>
<param name= "SQLCMM" >command object </param>
private static void Fillparam (sqlparameter[] Paramss, SqlCommand SQLCMM)
{
foreach (SqlParameter param in Paramss)
{
SQLCMM. Parameters.Add (param);
}
}//Traversal parameters
<summary>
Perform the increase, deletion, and modification of the database and return the number of rows affected in the database
</summary>
<param name= "SQLText" > Insert, UPDATE, DELETE statement to execute </param>
<param name= "PARAMSS" > Parameter list in the statement to execute </param>
<returns></returns>
public static int ExecuteNonQuery (string sqltext,params sqlparameter[] paramss)
{
Using (SqlConnection sqlcnn=new SqlConnection (Sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn. CreateCommand ())
{
Sqlcmm.commandtext = SQLText;
Fillparam (PARAMSS,SQLCMM);
Sqlcnn. Open ();
Return SQLCMM. ExecuteNonQuery ();
}
}
}//returns SQLCMM. Excutequery ()
<summary>
Get retrieve result set, return DataTable
</summary>
<param name= "SQLText" > Search statement to execute </param>
<param name= "PARAMSS" > Retrieve the argument list in the statement </param>
<returns></returns>
public static DataTable exectetable (string sqltext, params sqlparameter[] paramss)
{
Using (SqlConnection sqlcnn=new SqlConnection (Sqlstr))
{
using (SqlCommand sqlcmm=sqlcnn. CreateCommand ())
{
Sqlcmm.commandtext = SQLText;
Fillparam (PARAMSS,SQLCMM);
Sqlcnn. Open ();
using (SqlDataReader reader=sqlcmm. ExecuteReader ())
{
DataTable dt = new DataTable ();
Dt. Load (reader);
return DT;
}
}
}
}//Back to DataTable
}
Here are the additions and deletions to check the operation:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.SqlClient;
Using System.Data;
public partial class Caozuo:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void btnAdd_Click (object sender, EventArgs e)
{
String sqlstr = "INSERT into Denglu (Sname,spassword) VALUES (@name, @password)";
int i = sqlhelper.executenonquery (sqlstr,new SqlParameter ("@name", This.txtName.Text), New SqlParameter ("@password", This.txtPassword.Text));
if (i > 0)
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' Add success. ') </script> ", false);
}
Else
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' Add failed. ') </script> ", false);
}
}
protected void Btndel_click (object sender, EventArgs e)
{
String sqlstr = "Delete from Denglu where sname= @name";
int i = sqlhelper.executenonquery (sqlstr,new SqlParameter ("@name", This.txtName.Text));
if (i > 0)
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' Delete succeeded. ') </script> ", false);
}
Else
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' Delete failed. ') </script> ", false);
}
}//Delete
protected void Btnedit_click (object sender, EventArgs e)
{
String sqlstr = "Update Denglu set spassword= @password where sname= @name";
int i = sqlhelper.executenonquery (sqlstr,new SqlParameter ("@name", This.txtName.Text), New SqlParameter ("@password", This.txtPassword.Text));
if (i > 0)
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' modified successfully. ') </script> ", false);
}
Else
{
Clientscript.registerclientscriptblock (GetType (), "hint", "<script>alert" (' Modify failed. ') </script> ", false);
}
}//changes
protected void Btnlook_click (object sender, EventArgs e)
{
String sqlstr = "Select Spassword from Denglu where sname= @name";
DataTable dt = sqlhelper.exectetable (sqlstr,new SqlParameter ("@name", This.txtName.Text.Trim ());
This.txtPassword.Text = dt. Rows[0][0]. ToString ();
}//Find
protected void Btnrefresh_click (object sender, EventArgs e)
{
String sqlstr = "SELECT * from Denglu";
DataTable dt = sqlhelper.exectetable (SQLSTR);
This. Gridview1.datasource = DT;
This. Gridview1.databind ();
}//Refresh
}