using System;
using System。Collections。Generic;
using System。Text;
using System。Data;
using System。Data。SqlClient;
using System。
Configuration;
namespace ZeroSpaceDAL {
public static class DBHelper {
public static string ConnString {
get {
return ConfigurationManager。
ConnectionStrings["ZeroSpace"]。ConnectionString;
}
}
public static SqlConnection Connection {
get {
return new SqlConnection(ConnString);
}
}
public static int ExecuteCommand(string safeSql) {
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd。
Connection。Open();
try {
int result = cmd。ExecuteNonQuery();
return result;
}
finally {
cmd。
Connection。Close();
cmd。Connection。Dispose();
}
}
public static int ExecuteCommand(string sql, params SqlParameter[] values) {
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd。
Connection。Open();
try {
cmd。Parameters。AddRange(values);
return cmd。ExecuteNonQuery();
}
finally {
cmd。
Connection。Close();
cmd。Connection。Dispose();
}
}
public static int GetScalar(string safeSql) {
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd。
Connection。Open();
try {
int result = Convert。ToInt32(cmd。ExecuteScalar());
return result;
}
finally {
cmd。
Connection。Close();
cmd。Connection。Dispose();
}
}
public static int GetScalar(string sql, params SqlParameter[] values) {
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd。
Connection。Open();
try {
cmd。Parameters。AddRange(values);
int result = Convert。
ToInt32(cmd。ExecuteScalar());
return result;
}
finally {
cmd。
Connection。Close();
cmd。Connection。Dispose();
}
}
public static SqlDataReader GetReader(string safeSql) {
SqlCommand cmd = new SqlCommand(safeSql, Connection);
cmd。
Connection。Open();
SqlDataReader reader = cmd。ExecuteReader(CommandBehavior。CloseConnection);
return reader;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values) {
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd。
Parameters。AddRange(values);
cmd。Connection。Open();
SqlDataReader reader = cmd。ExecuteReader(CommandBehavior。
CloseConnection);
return reader;
}
public static DataTable GetDataSet(string safeSql) {
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
try {
SqlDataAdapter da = new SqlDataAdapter(cmd);
da。
Fill(ds);
return ds。Tables[0];
}
finally {
cmd。Connection。Dispose();
}
}
public static DataTable GetDataSet(string sql, params SqlParameter[] values) {
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
try {
cmd。
Parameters。AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da。Fill(ds);
return ds。
Tables[0];
}
finally {
cmd。Connection。Dispose();
}
}
}
}。
全部