using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql ;
using System.Data.SqlClient ;
using System.Configuration;  
using System.Data ;
namespace mySqlServer					//定义名称空间mySqlServer
{
    public class sqlServerClass     			//定义操纵数据库类
    {
        private string connectionString;  		//定义数据库连接字符串成员
        public string ConnectionString  
        {  
            set { connectionString = value; }  
        }  
      public sqlServerClass(string connectionString) //构造函数,入口参数是数据库连接字符串
        {
           this.connectionString = connectionString;  
        }
        /// 执行一个查询，并返回结果集,入口参数是要执行的SQL语句，并返回查询结果集 
        public DataTable ExecuteDataTable(string sql)  
        {
            DataTable data = new DataTable()	//实例化DataTable，用于装载查询结果集   
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(sql, connection);
            //设置command的CommandType为指定的CommandType   
　　command.CommandType = CommandType.Text ;
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(data);				//填充DataTable   
            return data;  
        }  
       /// 记取数据表内容的方法，入口参数是要执行的查询SQL文本命令  
       public SqlDataReader ExecuteReader(string sql)  
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(sql, connection);
            connection.Open();
            //CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象   
            return command.ExecuteReader(CommandBehavior.CloseConnection);  
        }  
   	   // 返回查询所返回的结果集中第一行的第一列或空引用，忽略其他列或行   
       public Object ExecuteScalar(string sql)  
        {
            object result = null;
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(sql, connection);
            command.CommandType = CommandType.Text ;
            connection.Open();					//打开数据库连接   
            result = command.ExecuteScalar();
            return result;				//返回查询结果的第一行第一列，忽略其它行和列   
        }  
        /// 对数据库执行增删改操作   
       public int ExecuteNonQuery(string sql)  
        {
            int count = 0;
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand(sql, connection);
            command.CommandType = CommandType.Text;
            connection.Open();                  //打开数据库连接   
            count = command.ExecuteNonQuery();
            return count;               //返回执行增删改操作之后，数据库中受影响的行数    
         }  
        /// 返回当前连接的数据库中所有由用户创建的数据库   
       public DataTable GetTables()  
        {  
            DataTable data = null;  
            using (SqlConnection connection = new SqlConnection(connectionString))  
            {  
                connection.Open();//打开数据库连接   
                data = connection.GetSchema("Tables");  
            }  
            return data;  
        }  
    }  
}  