Using System; Using System.Data; Using System.Data.SqlClient; Using System.Configuration; <summary> Sqldbhelper: Common class for manipulating SQL Server databases Author: Li Baohen Date: 2012-03-15 version:1.0 </summary> public class Sqldbhelper { #region Field <summary> Private field </summary> private string connectionString; #endregion #region Properties <summary> Public Property Database Connection string </summary> public string connectiongstring { set {connectionString = value;} } #endregion #region Constructors <summary> Constructors </summary> Public Sqldbhelper () { Modify ConnectionString to a database connection string in a project Connectionstring= ""; } <summary> Constructors </summary> <param name= "strConnectionString" > Database connection string </param> Public Sqldbhelper (String strconnectionstring) { connectionString = strConnectionString; } #endregion #region Determine if the database is connected <summary>
Determine if the database is connected
</summary>
<returns> whether to connect </returns>
public bool IsConnected ()
{
SqlConnection connection = new SqlConnection (connectionString);
Try
{
if (connection. State!= ConnectionState.Open)
{
Connection. Open ();
}
return true;
}
Catch
{
return false;
}
} #endregion #region executes a query and returns the result set ///<summary> ///executes a query and returns the result set ///</ Summary> ///<param name= "SQL" > SQL text command to query </param> /// <returns> Query result set </RETURNS> public DataTable executedatatable (String sql) { return executedatatable (SQL, CommandType.Text, null); } <summary> Executes a query and returns the result set </summary> <param name= "SQL" > SQL text command to query </param> <param name= "CommandType" > query statement type, stored procedure or SQL text command </param> <returns> Query Result set </returns> Public DataTable executedatatable (String sql, CommandType commandtype) { return executedatatable (SQL, CommandType, NULL); } <summary>
Executes a query and returns the result set
</summary>
<param name= "SQL" > SQL text command to query </param>
<param name= "CommandType" > query statement type, stored procedure or SQL text command </param>
<param name= "Parameters" >t-sql statements or parameter groups of stored procedures </param>
<returns> Query Result set </returns>
Public DataTable executedatatable (String sql, CommandType commandtype, sqlparameter[] parameters)
{
Instantiate a DataTable for loading query result sets
DataTable data = new DataTable ();
using (SqlConnection connection = new SqlConnection (connectionString))
{
using (SqlCommand command = new SqlCommand (SQL, connection))
{
Specify CommandType
Command.commandtype = CommandType; if (Parameters!= null)
{
foreach (SqlParameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Instantiation of SqlDataAdapter
SqlDataAdapter adapter = new SqlDataAdapter (command);
Populating a DataTable
Adapter. Fill (data);
}
}
return data; } #endregion #region Returns a DataReader object instance ///<summary> ///Returns a DataReader object instance /// </summary> ///<param name= "SQL" > SQL text command to query </param> ///<returns>datareader Object instance </returns> public SqlDataReader ExecuteReader ( String sql) { return ExecuteReader SQL, CommandType.Text, NULL); } ///<summary> ///Returns a DataReader object instance /// </summary> ///<param name= "SQL" > SQL text command to query </param> ///<param name= "CommandType" > type of query statement to execute, stored procedure, or SQL text command </param> ///< Returns>datareader object Instance </returns> public SqlDataReader ExecuteReader (String sql, CommandType commandtype) { return ExecuteReader (SQL, CommandType, NULL); } ///<summary> ///Returns a DataReader object instance /// </summary> ///<param name= "SQL" > SQL text command to query </param> ///<param name= "CommandType" > type of query statement to execute, stored procedure, or SQL text command </param> ///< param name= "parameters" >t-sql statement or stored procedure parameter array </param> ///<returns> DataReader object Instance </returns> public SqlDataReader ExecuteReader (String sql, CommandType CommandType, sqlparameter[] parameters) { SqlConnection connection = new SqlConnection (connectionString); SqlCommand command = new SqlCommand (sql, connection); if (Parameters!= null) { foreach (SqlParameter parameter in parameters) { Command. Parameters.Add (parameter); } } Connection. Open (); Parameter commandbehavior.closeconnection that closes the connection object while closing the reader object return command. ExecuteReader (commandbehavior.closeconnection); } #endregion #region executes the query results, returning the first column of the first row ///<summary> ///Executes the query results, returning the first column of the first row ///</ Summary> ///<param name= "SQL" > SQL text command to query </param> /// <returns> returns the first column of the first row </returns> public Object ExecuteScalar (String sql) { return executescalar (SQL, CommandType.Text, null); } <summary> Executes the query result, returning the first column of the first row </summary> <param name= "SQL" > SQL text command to query </param> <param name= "CommandType" > type of query statement to execute, stored procedure or SQL text command </param> <returns> returns the first column of the first row </returns> public Object ExecuteScalar (String sql, CommandType commandtype) { return executescalar (SQL, CommandType, NULL); } <summary>
Executes the query result, returning the first column of the first row
</summary>
<param name= "SQL" > SQL text command to query </param>
<param name= "CommandType" > type of query statement to execute, stored procedure or SQL text command </param>
<param name= "parameters" >t-sql statement or stored procedure parameter array </param>
<returns> returns the first column of the first row </returns>
public Object ExecuteScalar (String sql, CommandType commandtype, sqlparameter[] parameters)
{
object result = NULL;
using (SqlConnection connection = new SqlConnection (connectionString))
{
using (SqlCommand command = new SqlCommand (SQL, connection))
{
Command.commandtype = CommandType; if (Parameters!= NULL) { foreach (SqlParameter parameter in parameters) { command. Parameters.Add (parameter); } } Connection. Open (); result = command. ExecuteScalar (); } } return result; } #endregion #region to the database for the additional deletion operation ///<summary> ///The database for an incremental deletion operation ///</ Summary> ///<param name= "SQL" > SQL text command to execute </param> /// <returns> return the affected function </returns> public int ExecuteNonQuery (String sql) { return ExecuteNonQuery (SQL, CommandType.Text, null); } <summary> To add a pruning operation to the database </summary> <param name= "SQL" > SQL text command to execute </param> <param name= "CommandType" > type of query statement to execute, stored procedure or SQL text command </param> <returns> returns the affected function </returns> public int ExecuteNonQuery (String sql, CommandType commandtype) { return ExecuteNonQuery (SQL, CommandType, NULL); } <summary>
To add a pruning operation to the database
</summary>
<param name= "SQL" > SQL text command to execute </param>
<param name= "CommandType" > type of query statement to execute, stored procedure or SQL text command </param>
<param name= "parameters" >t-sql statement or stored procedure parameter array </param>
<returns> returns the affected function </returns>
public int ExecuteNonQuery (String sql, CommandType commandtype, sqlparameter[] parameters)
{
int count = 0;
using (SqlConnection connection = new SqlConnection (connectionString))
{
using (SqlCommand command = new SqlCommand (SQL, connection))
{
Command.commandtype = CommandType; if (Parameters!= null)
{
foreach (SqlParameter parameter in parameters)
{
Command. Parameters.Add (parameter);
}
}
Connection. Open ();
Count = command. ExecuteNonQuery (); } } return count; } #endregion } |