- DataAccess.cs
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- using System.Text;
- /// <summary>
- /// DataAccess 的摘要说明
- /// </summary>
- public class DataAccess
- {
- private SqlConnection conn = null;
- private SqlCommand comm = null;
- private SqlDataAdapter da = null;
- public DataAccess()
- {
- conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
- }
- // 执行非查询语句,并返回受影响的记录行数
- public int ExecuteCommand(string sql)
- {
- int result = 0;
- comm = new SqlCommand(sql, conn);
- try
- {
- conn.Open();
- result = comm.ExecuteNonQuery();
- }
- catch
- {
- return 0;
- }
- finally
- {
- this.conn.Close();
- }
- return result;
- }
- // 执行非查询语句,并返回受影响的记录行数
- public int ExecuteCommand(string sql, CommandType cmdtype, SqlParameter[] parameters)
- {
- int result = 0;
- comm = new SqlCommand();
- try
- {
- conn.Open();
- comm.Connection = conn;
- comm.CommandText = sql;
- comm.CommandType = cmdtype;
- if (parameters != null && parameters.Length > 0)
- {
- foreach (SqlParameter param in parameters)
- {
- comm.Parameters.Add(param);
- }
- }
- result = comm.ExecuteNonQuery();
- }
- catch
- {
- return 0;
- }
- finally
- {
- this.conn.Close();
- }
- return result;
- }
- public int ExecuteCommand(string sql, CommandType cmdtype, SqlParameter[] parameters, ref String[] output)
- {
- int _result = 0;
- comm = new SqlCommand();
- try
- {
- conn.Open();
- comm.Connection = conn;
- comm.CommandText = sql;
- comm.CommandType = cmdtype;
- if (parameters != null && parameters.Length > 0)
- {
- foreach (SqlParameter param in parameters)
- {
- comm.Parameters.Add(param);
- }
- }
- _result = comm.ExecuteNonQuery();
- for (int i = 0; i < output.Length; i++)
- {
- output[i] = parameters[0].Value.ToString().Trim();
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- finally
- {
- conn.Close();
- }
- return _result;
- }
- //生成参数
- public SqlParameter CreateParameter(string field, DbType dbtype, string value)
- {
- SqlParameter p = new SqlParameter();
- p.ParameterName = field;
- p.DbType = dbtype;
- p.Value = value;
- return p;
- }
- public SqlParameter CreateParameter(string field, DbType dbtype, string value, ParameterDirection direction)
- {
- SqlParameter p = new SqlParameter();
- p.ParameterName = field;
- p.DbType = dbtype;
- p.Value = value;
- p.Direction = direction;
- return p;
- }
- // 执行查询,并以DataReader返回结果集
- public SqlDataReader ExecuteReader(string sql)
- {
- SqlDataReader result = null;
- comm = new SqlCommand(sql, conn);
- try
- {
- conn.Open();
- result = comm.ExecuteReader();
- }
- catch
- {
- return null;
- }
- return result;
- }
- // 执行查询,并以DataSet返回结果集
- public DataSet GetDataSet(string sql)
- {
- DataSet ds = new DataSet();
- try
- {
- conn.Open();
- da = new SqlDataAdapter(sql, conn);
- da.Fill(ds);
- }
- catch
- {
- return null;
- }
- finally
- {
- this.conn.Close();
- }
- return ds;
- }
- // 执行查询,并以DataView返回结果集
- public DataView GetDataView(string sql)
- {
- DataView dv = this.GetDataSet(sql).Tables[0].DefaultView;
- return dv;
- }
- // 执行查询,并以DataTable返回结果集
- public DataTable GetDataTable(string sql)
- {
- DataTable dt = GetDataSet(sql).Tables[0];
- return dt;
- }
- //返回首行首列的值
- public object ExecuteScalar(string sql)
- {
- object result = null;
- SqlCommand command = new SqlCommand(sql, conn);
- try
- {
- conn.Open();
- result = command.ExecuteScalar();
- }
- catch
- {
- return null;
- }
- finally
- {
- this.conn.Close();
- }
- return result;
- }
- }
常用的工具类