//ExecuteNonQuery Build
public static int ExecuteNonQuery (string sql,params sqlparameter[] parameters)
{
String Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (STR))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd.commandtext = SQL;
foreach (SqlParameter par in parameters)
{
Cmd. Parameters.Add (PAR);
}
return CMD. ExecuteNonQuery ();
}
}
}
//Use of the ExecuteNonQuery method
private void Button1_Click (object sender, EventArgs e)//click event
{
Try
{
Sqlhelper.executenonquery ("INSERT into T_age (name,age) VALUES (@n,@a)", New SqlParameter ("@N", TxtLogin.Text.Trim ()), New SqlParameter ("@A", TxtAge.Text.Trim ()));
MessageBox.Show ("Insert succeeded! ");
}
catch (Exception ex)
{
MessageBox.Show (String. Format ("Insert failed! : {0} ", ex));
Return
}
}
//-----------------------------------------------------------------------------
//ExecuteScalar build
public static Object Execcutescalar (String sql, params sqlparameter[] parameters)
{
String Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (STR))
{
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = SQL;
foreach (SqlParameter par in parameters)
{
Cmd. Parameters.Add (PAR);
}
return CMD. ExecuteScalar ();
}
}
//Use of the ExecuteScalar method
private void Button2_Click (object sender, EventArgs e)//click event
{
int i = Convert.ToInt32 (Sqlhelper.execcutescalar ("SELECT count (*) T_phone"));
MessageBox.Show (Convert.ToString (i));
}
//-----------------------------------------------------------------------------
//ExecuteReader construction//cannot use using
public static SqlDataReader SqlDataReader (String sql, params sqlparameter[] parameters)
{
String Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
SqlConnection conn = new SqlConnection (STR);
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = SQL;
foreach (SqlParameter par in parameters)
{
Cmd. Parameters.Add (PAR);
}
SqlDataReader dr = cmd. ExecuteReader (commandbehavior.closeconnection);
Cmd. Parameters.clear ();
Return Dr;
}
//Use of the ExecuteReader method
private void Button4_Click (object sender, EventArgs e)//click event
{
SqlDataReader dr = Sqlhelper.sqldatareader_using ("SELECT * from T_age");
while (Dr. Read ())
{
MessageBox.Show (convert.tostring (dr["Name"])); Read the value of the Name field
}
}
//-----------------------------------------------------------------------------
//Construction of ExecuteDataset
public static DataTable ExecuteDataset (String sql, params sqlparameter[] sqlparmeter)
{
String Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (STR))
{
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = SQL;
foreach (SqlParameter par in sqlparmeter)//Traversal add Sqlparmeter parameter
{
Cmd. Parameters.Add (PAR);
}
DataSet DataSet = new DataSet ();
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
Adapter. Fill (DataSet);
return DataSet. Tables[0];
}
}
//Use of the ExecuteDataset method
private void Button5_click (object sender, EventArgs e)
{
DataTable dt = SqlHelper.ExecuteDataset ("SELECT * from T_age");
for (int i = 0; i < dt. Rows.Count; i++)
{
DataRow row = dt. Rows[i];
String name =convert.tostring (row["name"]);
MessageBox.Show (name);
}
}
//-----------------------------------------------------------------------------
Note: The SqlDataReader will not execute if used, because it cannot be read when the data is read by using
The following is the wrong code
public static SqlDataReader executereader_using (String sql, params sqlparameter[] parameters)
{
String Str = configurationmanager.connectionstrings["ConnStr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (STR))
{
Conn. Open ();
SqlCommand cmd = conn. CreateCommand ();
Cmd.commandtext = SQL;
foreach (SqlParameter par in parameters)
{
Cmd. Parameters.Add (PAR);
}
return CMD. ExecuteReader ();
}
}
//-----------------------------------------------------------------------------
Simple SqlHelper.