ADO.NET常用的方法

 public class CommonClass
    {
        public CommonClass()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string g_connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings        ["Connection_conn"].ToString();
        private int g_index;
        private SqlConnection g_connecting;

        //关闭连结
        public void Connectionclose()
        {
            if (g_connecting.State == ConnectionState.Open)
            {
                g_connecting.Close();
                g_connecting.Dispose();


            }
        }
        //执行SQL语句
        public string ExecuteSQL(string Actionquery)
        {

            SqlConnection g_connectiong = new SqlConnection(g_connectionstring);
            SqlCommand command = new SqlCommand(Actionquery, g_connecting);
            g_connecting.Open();
            try
            {
                return command.ExecuteNonQuery().ToString();


            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (g_connecting.State == ConnectionState.Open)
                {
                    g_connecting.Close();
                }
            }
        }
        //判断是否读取
       
        public SqlDataReader GetDataRead(string sqlstring)
        {

            g_connecting = new SqlConnection(g_connectionstring);
            SqlCommand command = new SqlCommand(sqlstring, g_connecting);
            g_connecting.Open();
            SqlDataReader DataRead;
            try
            {
                DataRead = command.ExecuteReader(CommandBehavior.CloseConnection);
                return DataRead;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        //返回DataView
        public DataView GetDataView(string sqlstring)
        {
            DataView dataview;
            SqlDataAdapter dataadapter = new SqlDataAdapter();
            DataSet dataset = new DataSet();
            string tablename;
            g_connecting = new SqlConnection(g_connectionstring);
            g_connecting.Open();
            dataadapter = new SqlDataAdapter(sqlstring, g_connecting);
            g_index += 1;
            tablename = "Table" + g_index;
            try
            {
                dataadapter.Fill(dataset, tablename);
                dataview = new DataView(dataset.Tables[tablename]);
                return dataview;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (g_connecting.State == ConnectionState.Open)
                {
                    g_connecting.Close();
                }
            }

        }

        //返回dataset

        public DataSet GetDataSet(string sqlstring)
        {
            SqlDataAdapter dataadapter = new SqlDataAdapter(sqlstring, g_connecting);
            DataSet dataset = new DataSet();
            try
            {
                dataadapter.Fill(dataset);
                return dataset;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        //执行任意sql语句
        public DataTable ExecQuerySql(string sqlstring)
        {
            try
            {
                g_connecting = new SqlConnection(g_connectionstring);
                SqlCommand command = new SqlCommand(sqlstring, g_connecting);
                command.CommandTimeout = 100000000;
                SqlDataAdapter dataadapter = new SqlDataAdapter(command);
                DataTable datatable = new DataTable();
                dataadapter.Fill(datatable);
                return datatable;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }
        //邦定Gridview
        public void BindAnyGridView(string sqlstring, GridView gridview)
        {
            try
            {
                DataTable datatable = this.ExecQuerySql(sqlstring);
                gridview.DataSource = datatable;
                gridview.DataBind();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }

        }
        //邦定DropDownList
        public void BindAnyDropDownList(string sqlstring, DropDownList dropdownlist, string text, string value)
        {
            try
            {
                DataSet dataset = this.ExecQuerySqlDs(sqlstring);
                dropdownlist.DataTextField = text;
                dropdownlist.DataValueField = value;
                dropdownlist.DataSource = dataset.Tables[0];
                dropdownlist.DataBind();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        public DataSet ExecQuerySqlDs(string sqlstring)
        {
            try
            {

                g_connecting = new SqlConnection(g_connectionstring);
                SqlCommand command = new SqlCommand(sqlstring, g_connecting);
                command.CommandTimeout = 100000000;
                SqlDataAdapter dataadapter = new SqlDataAdapter(command);
                DataSet dataset = new DataSet();
                dataadapter.Fill(dataset);
                return dataset;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);

            }

        }


    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值