DataCon类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SMS.BaseClass
{
    class DataCon
    {
        //建立数据库连接
        public SqlConnection getcon()
        {
            //string M_str_sqlcon = "Data Source=(local);Database=db_SMS;User id =sa;PWD=";
            string M_str_sqlcon = "Data Source=LEOVANY-PC;Initial Catalog=db_SMS;Integrated Security=True";
            SqlConnection mycon = new SqlConnection(M_str_sqlcon);
            return mycon;
        }


        //执行命令对象
        public void getcom(string M_str_sqlstr)
        {
            SqlConnection sqlcon = this.getcon();         //实例化数据库连接对象
            sqlcon.Open();                           //打开数据库连接
            SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon);  //实例化sqlcommand 对象
            sqlcom.ExecuteNonQuery();                   //执行sql 语句
            sqlcom.Dispose();    //释放sqlcommand 对象资源
            sqlcon.Close();      //关闭数据库
            sqlcon.Dispose();    //释放数据库连接资源
        }

 

        //创建一个数据集对象
        public DataSet getds(string M_str_sqlstr, string M_str_table)
        {
            SqlConnection sqlcon = this.getcon();  //实例化数据库连接对象
            SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr,sqlcon);  //实例化SqlDataAdapter 对象
            DataSet myds = new DataSet();   //实例化数据集对象
            sqlda.Fill(myds,M_str_table);  //填充数据集
            return myds;                    //返回DataSet 数据集
        }

 

        //创建一个SqlDataReader对象
        public SqlDataReader getread(string M_str_sqlstr)
        {
            SqlConnection sqlcon = this.getcon();  //实例化数据库连接对象
            SqlCommand sqlcom = new SqlCommand(M_str_sqlstr,sqlcon); //实例化SqlCommand 对象
            sqlcon.Open();  //打开数据库连接
            SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);  //获取SqlDataReader 对象,并关闭SqlDataReader 对象和Connection对象
            return sqlread;        //返回数据读取对象
        }
    }
}

你可能感兴趣的:(VisioStudio)