using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
GameStatistics namespace
{
/// <Summary>
/// database operations
/// </ Summary>
class SqlHelp
{
Private static Connection the SqlConnection;
#region 1. connection string
/// <Summary>
/// database connection
/ // </ Summary>
public static connection the SqlConnection
{
GET
{
// Get connection string
string connString = @ "Data Source = D1 \ SQL2008R2SP2; Initial Catalog = GameStatistics; User ID = sa; Password = 123456";
if (connection == null)
{
connection = new SqlConnection(connString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
#Endregion
#region 2. execute the query and returns the query results returned by the first column of the first row. Ignore other columns or rows
/// <the Summary>
/// execute the query and returns the query results returned by the first column of the first row. Ignore other columns or rows, returns the number of rows affected
/// </ the Summary>
/// <param name = "sqlString"> SQL query </ param>
/// <param name = "CommType"> specify how interprets the command string </ param>
/// <param name = "parameters"> parameter indicative of System.Data.SqlClient.SqlCommand </ param>
/// <Returns> number of rows affected </ Returns>
public static the ExecuteScalar int (String sqlString, the CommandType CommType, the params the SqlParameter [] Parameters)
{
Command new new = the SqlCommand the SqlCommand (sqlString, Connection);
command.Parameters.AddRange (Parameters);
return (int) command.ExecuteScalar ();
}
#endregion
#region 3. connection Transact-SQL statements executed and returns the affected rows number
/// <Summary>
/// performed on a connection Transact-SQL statements and returns the number of rows affected, returns the number of rows affected
/// </ Summary>
/// <param name = "commText"> the SQL query </ param>
/// <param name = "CommType"> specify how to interpret the command string </ param>
/// <param name = "the parameters"> parameter represents the System.Data.SqlClient.SqlCommand </ param>
/// <Returns> number of rows affected < /returns>
static int the ExecuteCommand public (String commText, the CommandType CommType, the params the SqlParameter [] Parameters)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
command.Parameters.AddRange (Parameters);
return command.ExecuteNonQuery ();
}
#endregion
#region 4. The query returns a DataTable
/// <Summary>
/// The query returns a DataTable
/// </ Summary>
/// <param name = "commText"> query </ param>
/// <param name = "commType"> command string type </ param>
/// <Returns> the Table < /returns>
the DataTable GetDataTable static public (String commText, the CommandType CommType)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
the SqlDataAdapter the SqlDataAdapter new new DA = (Command);
the DataSet the DataSet new new DS = ();
da.Fill (DS, "Table") ;
return ds.Tables [ "Table"];
}
#endregion
#region 5. The query returns a DataView according
/// <Summary>
/// The query returns a DataView
/// </ Summary>
/// < param name = "commText"> query </ param>
/// <param name = " commType "> command string type </ param>
/// <Returns> the DataView </ Returns>
public static GetDataView the DataView (String commText, the CommandType CommType)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
the SqlDataAdapter the SqlDataAdapter new new DA = (Command);
the DataSet the DataSet new new DS = () ;
da.Fill (DS, "Table");
return ds.Tables [ "Table"] the DefaultView;.
}
#endregion
#region 6. The query returns a datatable according
/// <Summary>
/// returns an query a DataTable
/// </ Summary>
/// <param name = " commText "> connection string </ param>
/// <param name="commType">命令字符串类型</param>
/// <param name="parameters">返回一组映射</param>
/// <returns>datatable</returns>
public static DataTable GetDataTable(string commText, CommandType commType, params SqlParameter[] parameters)
{
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(commText, Connection);
command.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(ds, "myTable");
return ds.Tables["myTable"]; #endregion
}
{
/// <Summary>
/// database operations
/// </ Summary>
class SqlHelp
{
Private static Connection the SqlConnection;
#region 1. connection string
/// <Summary>
/// database connection
/ // </ Summary>
public static connection the SqlConnection
{
GET
{
// Get connection string
string connString = @ "Data Source = D1 \ SQL2008R2SP2; Initial Catalog = GameStatistics; User ID = sa; Password = 123456";
if (connection == null)
{
connection = new SqlConnection(connString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
#Endregion
#region 2. execute the query and returns the query results returned by the first column of the first row. Ignore other columns or rows
/// <the Summary>
/// execute the query and returns the query results returned by the first column of the first row. Ignore other columns or rows, returns the number of rows affected
/// </ the Summary>
/// <param name = "sqlString"> SQL query </ param>
/// <param name = "CommType"> specify how interprets the command string </ param>
/// <param name = "parameters"> parameter indicative of System.Data.SqlClient.SqlCommand </ param>
/// <Returns> number of rows affected </ Returns>
public static the ExecuteScalar int (String sqlString, the CommandType CommType, the params the SqlParameter [] Parameters)
{
Command new new = the SqlCommand the SqlCommand (sqlString, Connection);
command.Parameters.AddRange (Parameters);
return (int) command.ExecuteScalar ();
}
#endregion
#region 3. connection Transact-SQL statements executed and returns the affected rows number
/// <Summary>
/// performed on a connection Transact-SQL statements and returns the number of rows affected, returns the number of rows affected
/// </ Summary>
/// <param name = "commText"> the SQL query </ param>
/// <param name = "CommType"> specify how to interpret the command string </ param>
/// <param name = "the parameters"> parameter represents the System.Data.SqlClient.SqlCommand </ param>
/// <Returns> number of rows affected < /returns>
static int the ExecuteCommand public (String commText, the CommandType CommType, the params the SqlParameter [] Parameters)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
command.Parameters.AddRange (Parameters);
return command.ExecuteNonQuery ();
}
#endregion
#region 4. The query returns a DataTable
/// <Summary>
/// The query returns a DataTable
/// </ Summary>
/// <param name = "commText"> query </ param>
/// <param name = "commType"> command string type </ param>
/// <Returns> the Table < /returns>
the DataTable GetDataTable static public (String commText, the CommandType CommType)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
the SqlDataAdapter the SqlDataAdapter new new DA = (Command);
the DataSet the DataSet new new DS = ();
da.Fill (DS, "Table") ;
return ds.Tables [ "Table"];
}
#endregion
#region 5. The query returns a DataView according
/// <Summary>
/// The query returns a DataView
/// </ Summary>
/// < param name = "commText"> query </ param>
/// <param name = " commType "> command string type </ param>
/// <Returns> the DataView </ Returns>
public static GetDataView the DataView (String commText, the CommandType CommType)
{
the SqlCommand the SqlCommand new new Command = (commText, Connection);
the SqlDataAdapter the SqlDataAdapter new new DA = (Command);
the DataSet the DataSet new new DS = () ;
da.Fill (DS, "Table");
return ds.Tables [ "Table"] the DefaultView;.
}
#endregion
#region 6. The query returns a datatable according
/// <Summary>
/// returns an query a DataTable
/// </ Summary>
/// <param name = " commText "> connection string </ param>
/// <param name="commType">命令字符串类型</param>
/// <param name="parameters">返回一组映射</param>
/// <returns>datatable</returns>
public static DataTable GetDataTable(string commText, CommandType commType, params SqlParameter[] parameters)
{
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(commText, Connection);
command.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(ds, "myTable");
return ds.Tables["myTable"]; #endregion
}
}
}
}
Reproduced in: https: //my.oschina.net/dongri/blog/610893