C# ADO.NET 批量启用,停用账户
数据库表:
UserInfos:
数据:
底层Model:
SqlHelper.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WINERP.Common
{
public class SqlHelper
{
//数据库连接
//private string connStr = "server=192.168.0.204;uid=sa;pwd=123456;database=StudentDB";
public static readonly string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public static object ExecuteScalar(string sql, params SqlParameter[] paras)
{
object o = null;
using (SqlConnection conn = new SqlConnection(connString))
{
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.AddRange(paras);
//打开连接,最晚打开,最早关闭
conn.Open();
//执行sql语句
o = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
return o;
}
/// <summary>
/// 执行查询,返回SqlDataReader
/// </summary>
/// <param name="sql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] paras)
{
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.AddRange(paras);
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return sdr;
}
catch (SqlException ex)
{
conn.Close();
throw new Exception("执行查询出现异常", ex);
}
}
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static DataTable GetDataTable(string sql, params SqlParameter[] paras)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connString)</