Simple SqlHelper.

Source: Internet
Author: User
Tags connectionstrings

//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.

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.