using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace DotaChallenge.code
{
public class SqlHandler
{
static public string connectionString = @"Data Source=CAIKAIWEI/SQLEXPRESS;database = lab;User ID = sa;Password = 123123";
#region MakeParam
static public SqlParameter MakeParam(string paramName, object value)
{
SqlParameter sqlParam;
sqlParam = new SqlParameter(paramName, value);
return sqlParam;
}
/// <summary>
/// Command预处理
/// </summary>
/// <param name="conn">MySqlConnection对象</param>
/// <param name="trans">MySqlTransaction对象,可为null</param>
/// <param name="cmd">MySqlCommand对象</param>
/// <param name="cmdType">CommandType,存储过程或命令行</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdParms">MySqlCommand参数数组,可为null</param>
private static void PrepareCommand(SqlConnection conn, SqlTransaction trans, SqlCommand cmd, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
#region ExecuteNonQuery
/// <summary>
/// 执行命令
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令类型(存储过程或SQL语句)</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdParms">MySqlCommand参数数组</param>
/// <returns>返回受引响的记录行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(conn, null, cmd, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
#endregion
#region ExecuteScalar
/// <summary>
/// 执行命令,返回第一行第一列的值
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令类型(存储过程或SQL语句)</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdParms">MySqlCommand参数数组</param>
/// <returns>返回Object对象</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(connection, null, cmd, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
#endregion
#region ExecuteReader
/// <summary>
/// 执行命令或存储过程,返回MySqlDataReader对象
/// 注意MySqlDataReader对象使用完后必须Close以释放MySqlConnection资源
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令类型(存储过程或SQL语句)</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdParms">MySqlCommand参数数组</param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(conn, null, cmd, cmdType, cmdText, cmdParms);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch
{
conn.Close();
throw;
}
}
#endregion
#region ExecuteDataSet
/// <summary>
/// 执行命令或存储过程,返回DataSet对象
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令类型(存储过程或SQL语句)</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdParms">MySqlCommand参数数组(可为null值)</param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(conn, null, cmd, cmdType, cmdText, cmdParms);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"ds");
conn.Close();
cmd.Parameters.Clear();
return ds;
}
}
#endregion
}
}