首页 > 代码库 > DBHelper类
DBHelper类
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace Business.DAL { public static class DBHelper { //以下被注的是连接配置文件用,使用时可先编写配置文件,把Name设置为ConnectionString private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //private static string connectionString = "server=WYP;database=MXDB1;uid=sa;pwd=sa"; private static SqlConnection connection; public static SqlConnection Connection { get { if (connection == null) { connection = new SqlConnection(connectionString); connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } public static int ExecuteCommand(string sql, SqlParameter value) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.Add(value); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = (int)cmd.ExecuteScalar(); return result; } public static int ExecuteScalar(string sql, SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = (int)cmd.ExecuteScalar(); return result; } public static int ExecuteScalar(string sql, SqlParameter value) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.Add(value); int result = (int)cmd.ExecuteScalar(); return result; } public static SqlDataReader ExecuteReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static SqlDataReader ExecuteReader(string sql, SqlParameter value) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.Add(value); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds,"temp"); return ds.Tables[0]; } public static DataTable GetDataSet(string safeSql,SqlParameter[] values) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds,"temp"); return ds.Tables[0]; } public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; } public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; } } }
DBHelper类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。