首页 > 代码库 > SqlHelper
SqlHelper
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;using System.Data;using System.Data.SqlClient;namespace LZJ_Common{ public class SqlHelper { private static readonly string ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; /// <summary> /// 增删改方法 /// </summary> /// <param name="sqlstr"></param> /// <param name="psm"></param> public int ExeCuteNonQuery(string sqlstr, params SqlParameter[] psm) { using (SqlConnection mycon = new SqlConnection(ConStr)) { using (SqlCommand mycom = new SqlCommand(sqlstr, mycon)) { if (psm != null) { mycom.Parameters.AddRange(psm); } mycon.Open(); return mycom.ExecuteNonQuery(); } } } public object ExeCuteSclar(string sqlstr, params SqlParameter[] psm) { using (SqlConnection mycon = new SqlConnection(ConStr)) { using (SqlCommand mycom = new SqlCommand(sqlstr, mycon)) { if (psm != null) { mycom.Parameters.AddRange(psm); } mycon.Open(); return mycom.ExecuteScalar(); } } } public SqlDataReader ExeCuteReader(string sqlstr, params SqlParameter[] psm) { SqlConnection mycon = new SqlConnection(ConStr); using (SqlCommand mycom = new SqlCommand(sqlstr, mycon)) { if (psm != null) { mycom.Parameters.AddRange(psm); } try { mycon.Open(); return mycom.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { mycon.Dispose(); throw ex; } } } public DataTable ExeCuteDataTable(string sqlstr, params SqlParameter[] psm) { DataTable dt = new DataTable(); using (SqlDataAdapter myda = new SqlDataAdapter(sqlstr, ConStr)) { if (psm != null) { myda.SelectCommand.Parameters.AddRange(psm); } myda.Fill(dt); return dt; } } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。