using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public sealed class SQLHelper
{
public readonly static string strCon = "server=.;uid=sa;pwd=123456;database=Library";
public int ExecuteNonQuery(string sql)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
Conn.Open();
SqlCommand sqlcom = new SqlCommand(sql, Conn);
int n= sqlcom.ExecuteNonQuery();
return n;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
/// <summary>
/// 登陆存储过程
/// </summary>
/// <param name="sql">SQL语句或者存储过程名</param>
/// <param name="paras">参数数组</param>
/// <param name="CmdType">命令类型</param>
/// <returns>
/// 1、输出参数值
/// 2、返回值
/// </returns>
public int[] User_login(string sql, SqlParameter[] paras, CommandType CmdType)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
Conn.Open();
SqlCommand Comm = new SqlCommand();
Comm.Connection = Conn;
Comm.CommandText = sql;
Comm.CommandType = CmdType;
//循环遍历添加参数
foreach (SqlParameter sp in paras)
{
Comm.Parameters.Add(sp);
}
//执行
Comm.ExecuteNonQuery();
int[] arr = new int[2];
//获取输出参数和返回值
foreach (SqlParameter sp in paras)
{
if (sp.Direction == ParameterDirection.Output)
{
if (sp.Value != DBNull.Value)
{
arr[0] =Convert.ToInt32(sp.Value);
}
}
else if (sp.Direction == ParameterDirection.ReturnValue)
{
arr[1] = Convert.ToInt32(sp.Value);
}
}
//返回数组
return arr;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
/// <summary>
/// 返回一个Table数据集
/// </summary>
/// <param name="sql">sql查询语句</param>
/// <returns>返回Table数据集</returns>
public DataTable GetTable(string sql)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
public sealed class SQLHelper
{
public readonly static string strCon = "server=.;uid=sa;pwd=123456;database=Library";
public int ExecuteNonQuery(string sql)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
Conn.Open();
SqlCommand sqlcom = new SqlCommand(sql, Conn);
int n= sqlcom.ExecuteNonQuery();
return n;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
/// <summary>
/// 登陆存储过程
/// </summary>
/// <param name="sql">SQL语句或者存储过程名</param>
/// <param name="paras">参数数组</param>
/// <param name="CmdType">命令类型</param>
/// <returns>
/// 1、输出参数值
/// 2、返回值
/// </returns>
public int[] User_login(string sql, SqlParameter[] paras, CommandType CmdType)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
Conn.Open();
SqlCommand Comm = new SqlCommand();
Comm.Connection = Conn;
Comm.CommandText = sql;
Comm.CommandType = CmdType;
//循环遍历添加参数
foreach (SqlParameter sp in paras)
{
Comm.Parameters.Add(sp);
}
//执行
Comm.ExecuteNonQuery();
int[] arr = new int[2];
//获取输出参数和返回值
foreach (SqlParameter sp in paras)
{
if (sp.Direction == ParameterDirection.Output)
{
if (sp.Value != DBNull.Value)
{
arr[0] =Convert.ToInt32(sp.Value);
}
}
else if (sp.Direction == ParameterDirection.ReturnValue)
{
arr[1] = Convert.ToInt32(sp.Value);
}
}
//返回数组
return arr;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
/// <summary>
/// 返回一个Table数据集
/// </summary>
/// <param name="sql">sql查询语句</param>
/// <returns>返回Table数据集</returns>
public DataTable GetTable(string sql)
{
using (SqlConnection Conn = new SqlConnection(strCon))
{
try
{
SqlDataAdapter sda = new SqlDataAdapter(sql, Conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Dispose();
}
}
}
}
}