老師在講C#實戰項目時所使用的SqlHelper,使用的資料庫時SQL Server ...
老師在講C#實戰項目時所使用的SqlHelper,使用的資料庫時SQL Server
註意,連接資料庫的連接字元串需要在配置文件中設置好
<appSettings>
<add key = "connString" value="data source = .; database = CourseSelectionInfo; integrated security = true"/>
</appSettings>
public class SqLHelper
{
#region 連接字元串connString
private static string connString;
static DBHelper()
{
connString = ConfigurationManager.AppSettings["connString"];
}
#endregion
#region 增刪改int ExcuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
/// <summary>
/// 增刪改
/// </summary>
/// <param name="cmdText">sql</param>
/// <param name="cmdType"></param>
/// <param name="parameters">參數</param>
/// <returns>修改了幾行</returns>
public static int ExcuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(cmdText, connection))
{
cmd.CommandType = cmdType;
connection.Open();
if (parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
return cmd.ExecuteNonQuery();
}
}
}
#endregion
#region 獲取一個數據 T ExcuteScalar<T>(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
/// <summary>
/// 獲取一個數據<T>
/// </summary>
/// <typeparam name="T">數據類型</typeparam>
/// <param name="cmdText">sql</param>
/// <param name="cmdType"></param>
/// <param name="parameters">參數</param>
/// <returns>任意類型的數據</returns>
public static T ExcuteScalar<T>(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(cmdText, connection))
{
cmd.CommandType = cmdType;
connection.Open();
if (parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
return (T)cmd.ExecuteScalar();
}
}
}
#endregion
#region 讀取數據表SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
/// <summary>
/// 讀取數據表
/// </summary>
/// <param name="cmdText">sql</param>
/// <param name="cmdType"></param>
/// <param name="parameters">參數</param>
/// <returns>SqlDataReader</returns>
public static SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connString);
using (SqlCommand cmd = new SqlCommand(cmdText, connection))
{
cmd.CommandType = cmdType;
connection.Open();
if (parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
#endregion
#region 臨時資料庫DataSet GetDataSet(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
/// <summary>
/// 獲取臨時資料庫
/// </summary>
/// <param name="cmdText"></param>
/// <param name="cmdType"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static DataSet GetDataSet(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand(cmdText, connection))
{
cmd.CommandType = cmdType;
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
if (parameters.Length > 0)
{
adapter.SelectCommand.Parameters.AddRange(parameters);
}
adapter.Fill(ds);
}
}
}
return ds;
}
#endregion
#region 臨時數據表DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
/// <summary>
/// 獲取臨時數據表
/// </summary>
/// <param name="cmdText">sql</param>
/// <param name="cmdType"></param>
/// <param name="parameters">參數</param>
/// <returns></returns>
public static DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] parameters)
{
return GetDataSet(cmdText, cmdType, parameters).Tables[0];
}
#endregion
}