using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace 通过MD5实现登陆
{
public static class SqlHelper
{
private static readonly string conStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
public static int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = type;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string sql, CommandType type, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = type;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
public static SqlDataReader ExecuteReader(string sql, CommandType type, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(conStr);
try
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.CommandType = type;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
}
catch
{
if (con != null)
{
con.Close();
con.Dispose();
}
throw;
}
}
public static DataTable ExecuteDataTable(string sql,CommandType type, params SqlParameter[] pms)
{
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql, conStr);
sqlAdapter.SelectCommand.CommandType = type;
if (pms != null)
{
sqlAdapter.SelectCommand.Parameters.AddRange(pms);
}
DataTable dt = new DataTable();
sqlAdapter.Fill(dt);
return dt;
}
}
}
自己使用的sqlhelper
最新推荐文章于 2022-05-07 10:28:08 发布