-
-
- public class SqlHelper
- {
- private string connectionString;
-
-
-
-
- public string ConnectionString
- {
-
- set { connectionString = value; }
- }
-
-
-
-
-
- public SqlHelper(string connectionString)
- {
- this.connectionString = connectionString;
- }
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql,CommandType commandType)
- {
- return ExecuteDataTable(sql, commandType, null);
- }
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql)
- {
- return ExecuteDataTable(sql, CommandType.Text, null);
- }
-
-
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql, CommandType commandtype, SqlParameter[] parameters)
- {
- DataTable data = new DataTable();
- using (SqlConnection con = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand(sql, con))
- {
- cmd.CommandType = commandtype;
-
- if (parameters != null)
- {
- foreach (SqlParameter parameter in parameters)
- {
- cmd.Parameters.Add(parameter);
- }
- }
-
-
- SqlDataAdapter adapter = new SqlDataAdapter(cmd);
- adapter.Fill(data);
-
- }
- }
- return data;
- }
-
-
-
-
-
-
- public SqlDataReader ExecuteReader(string sql)
- {
- return ExecuteReader(sql, CommandType.Text, null);
- }
-
-
-
-
-
-
-
- public SqlDataReader ExecuteReader(string sql,CommandType commandType)
- {
- return ExecuteReader(sql, commandType, null);
- }
-
-
-
-
-
-
-
-
- public SqlDataReader ExecuteReader(string sql, CommandType commandType, SqlParameter[] parameters)
- {
- SqlConnection con = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand(sql, con);
-
- if (parameters != null)
- {
- foreach (SqlParameter parameter in parameters)
- {
- cmd.Parameters.Add(parameters);
- }
- }
- con.Open();
-
- return cmd.ExecuteReader(CommandBehavior.CloseConnection);
- }
-
-
-
-
-
-
- public Object ExecuteScalar(string sql)
- {
- return ExecuteScalar(sql, CommandType.Text, null);
- }
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql, CommandType commandType)
- {
- return ExecuteScalar(sql, commandType, null);
- }
-
-
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql,CommandType commandType, SqlParameter[] parameters)
- {
- Object result=null;
- SqlConnection con=new SqlConnection(connectionString);
- SqlCommand cmd=new SqlCommand(sql,con);
- cmd.CommandType= commandType;
- if(parameters!=null)
- {
- foreach (SqlParameter parapmeter in parameters)
- {
- cmd.Parameters.Add(parapmeter);
- }
- }
-
- con.Open();
- result=cmd.ExecuteScalar();
- con.Close();
- return result;
- }
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql)
- {
- return ExecuteNonQuery(sql, CommandType.Text, null);
- }
-
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, CommandType commandType)
- {
- return ExecuteNonQuery(sql, commandType, null);
- }
-
-
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, CommandType commandType, SqlParameter[] parameters)
- {
- int count = 0;
- SqlConnection con = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand(sql, con);
- cmd.CommandType = commandType;
- if (parameters != null)
- {
- foreach(SqlParameter parameter in parameters)
- {
- cmd.Parameters.Add(parameter);
- }
- }
-
- con.Open();
- count = cmd.ExecuteNonQuery();
- con.Close();
- return count;
- }
-
-
-
-
-
- public DataTable GetTables()
- {
- DataTable table = null;
- using (SqlConnection con = new SqlConnection(connectionString))
- {
- con.Open();
- table = con.GetSchema("Tables");
-
- }
- return table;
- }
- }
- 本文出自 “强子的专栏” 博客,请务必保留此出处http://yisuowushinian.blog.51cto.com/4241271/999324
【ASP.NET开发】ASP.NET对SQLServer的通用数据库访问类(转)