using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using System.Windows.Forms;
namespace DL580_9130
{

    class database
    {
        private static  OdbcConnection Connection = null;
        private static  OdbcCommand db = null;
        private static OdbcTransaction tan = null;
        public static bool Open()
        {
            try
            {
                Connection = new OdbcConnection();
                db = new OdbcCommand();
                Connection.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" + Application.StartupPath + "\\DL580.mdb";
                Connection.Open();
                db.Connection = Connection;
                return true;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "   ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        public static void   Close()
        {
            Connection.Close();
            Connection.Dispose();
        }
        public static OdbcDataReader Datareader(string sqlstr) 
        {
            try
            {
                OdbcDataReader rs = null;
                db.CommandText = sqlstr;
                rs = db.ExecuteReader();
                return rs;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

        }
        public static bool ExecuteNonQuery(string sqlstr)
        {
            try
            {
                db.CommandText = sqlstr;
                db.Transaction = tan;
                db.ExecuteNonQuery();
                return true;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        public static bool Transaction()
        {
            try
            {
                tan = Connection.BeginTransaction();           //起始事务
                return true;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            } 
        }
        public static bool Commit()
        {
            try
            {
                tan.Commit();        //提交事务
                return true;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        public static bool Rollback()       //回滚事务
        {
            try
            {
                tan.Rollback();
                return true;
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message + "  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
    }
}
