Esempio n. 1
0
        /// <summary>
        /// 传入SQL返回dataSet
        /// OleDb 一般用于 access 数据库
        /// </summary>
        /// <param name="sql">传入sql</param>
        /// <returns></returns>
        public DataSet OleDbDataSet(string sql)
        {
            if (MyOleDb.State == ConnectionState.Open)//ConnectionState 得到當前資料源聯接的狀態
            {
                MyOleDb.Close();
            }
            OleDbDataAdapter MyDa = new OleDbDataAdapter(sql, MyOleDb);//新建DataSet對象

            MyDa.SelectCommand.CommandType = CommandType.Text;
            DataSet MyDs = new DataSet();

            try
            {
                MyDa.Fill(MyDs);
            }
            catch (Exception ex)                           //資料填充時的異常處理
            {
                if (MyOleDb.State == ConnectionState.Open) //ConnectionState 得到當前資料源聯接的狀態
                {
                    MyOleDb.Close();
                }
                MyDs = null;
                Err(ex.Message);
            }

            if (MyOleDb.State == ConnectionState.Open)//ConnectionState 得到當前資料源聯接的狀態
            {
                MyOleDb.Close();
            }

            return(MyDs);
        }
Esempio n. 2
0
        /// <summary>
        /// 传入一个SQL代码 返回DataReader 要求手动關闭
        /// </summary>
        /// <param name="sql">SQL語句</param>
        /// <returns> 返回DataReader 資料集 </returns>
        public SqlDataReader SqlDataReader(string sql)
        {
            SqlCommand Cmd = new SqlCommand(sql, MySql);

            MySql.Open();                                      //打开数据源
            SqlDataReader ObjDataReader = Cmd.ExecuteReader(); //建立DataReader对象、

            try
            {
                return(ObjDataReader);
            }
            catch (SqlException ex)//異常處理
            {
                MyOleDb.Close();
                Err(ex.Message);
                Res.Response.End();
            }

            return(ObjDataReader);
        }