- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace Aptech.DAL
- {
- public static class DBHelper
- {
- private static SqlConnection connection;//字段
- public static SqlConnection Connection//属性
- {
- get
- {
- //string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString;
- string connectionString = "Data Source=.;Initial Catalog=stu;Integrated Security=True";
- connection = new SqlConnection(connectionString);
- if (connection == null)
- {
- 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;
- }
- }
- //执行SQL语句,返回所影响的行数
- public static int ExecuteCommand(string safeSql)
- {
- SqlCommand cmd = new SqlCommand(safeSql, Connection);
- int result = cmd.ExecuteNonQuery();
- return result;
- }
- //执行带参的SQL语句,返回所影响的行数
- public static int ExecuteCommand(string sql, params SqlParameter[] values)
- {
- SqlCommand cmd = new SqlCommand(sql, Connection);
- cmd.Parameters.AddRange(values);
- return cmd.ExecuteNonQuery();
- }
- //执行SQL ,返回第一行第一列的字符串形式
- public static string ReturnStringScalar(string safeSql)
- {
- SqlCommand cmd = new SqlCommand(safeSql, Connection);
- try
- {
- string result = cmd.ExecuteScalar().ToString();
- return result;
- }
- catch (Exception e)
- {
- return "0";
- }
- connection.Close();
- }
- //执行SQL ,返回第一行第一列的整数形式
- public static int GetScalar(string safeSql)
- {
- SqlCommand cmd = new SqlCommand(safeSql, Connection);
- try
- {
- int result = Convert.ToInt32(cmd.ExecuteScalar());
- return result;
- }
- catch (Exception e)
- {
- return 0;
- }
- connection.Close();
- }
- //执行带参的SQL,返回影响的第一行第一列
- public static int GetScalar(string sql, params SqlParameter[] values)
- {
- SqlCommand cmd = new SqlCommand(sql, Connection);
- cmd.Parameters.AddRange(values);
- int result = Convert.ToInt32(cmd.ExecuteScalar());
- return result;
- connection.Close();
- }
- //执行SQL,返回数据流
- public static SqlDataReader GetReader(string safeSql)
- {
- SqlCommand cmd = new SqlCommand(safeSql, Connection);
- SqlDataReader reader = cmd.ExecuteReader();
- return reader;
- reader.Close();
- reader.Dispose();
- }
- //执行带参SQL,返回数据流
- 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;
- reader.Close();
- reader.Dispose();
- }
- //执行SQL,返回数据集
- public static DataTable GetDataSet(string safeSql)
- {
- DataSet ds = new DataSet();
- SqlCommand cmd = new SqlCommand(safeSql, Connection);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(ds);
- connection.Close();
- connection.Dispose();
- return ds.Tables[0];
- }
- //执行带参SQL,返回数据集
- public static DataTable GetDataSet(string sql, params SqlParameter[] values)
- {
- DataSet ds = new DataSet();
- SqlCommand cmd = new SqlCommand(sql, Connection);
- cmd.Parameters.AddRange(values);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- da.Fill(ds);
- connection.Close();
- connection.Dispose();
- return ds.Tables[0];
- }
- }
- }