现在的位置: 首页 > 综合 > 正文

C#数据库通用可移植类模块与应用实例

2012年01月05日 ⁄ 综合 ⁄ 共 30221字 ⁄ 字号 评论关闭

                            作者:段利庆         QQ:14035344

         此代码可以为你的数据库开发带来极大的便利性,如果需要全部的代码可以在评论处留下邮箱地址,我在稍晚时间发送到你的邮箱。

一.DBBase抽象类中封装了数据库内通用的操作函数。

1.ExecuteSQL 两个重载方法,一个实现了读取数据表的操作(SELECT),另一个实现了执行插入删除更新(INSERT,DELETE,UPDATE)。

2.删除记录采用虚方法,在基本中声明,这样可以从基类中直接实现删除方法,也可以在他的子类中实现。

3.其他一些实现高效处理查询和返回界面数据的方法。

二.数据记录的添加更新通过实现DBBase抽象类的两个接口AddNew,UpDate

UML视图

浏览视图地址:http://leek.woku.com/article/5053650.html

总结: 

一.在这个应用中合理的应用了程序开发模式,基类中提供了大多基本的通用函数。可以在派生类中使用并且通过接口分离了方法和方法的实现部分,实现了不同实例相同方法管理。

二.抽象类中删除功能的虚方法,合理的减少的代码量,可以在基类和派生类中均可以实现功能。

三.本例子应用了基类.派生类.接口.虚方法.等应用,对初学者是一个非常好的学习实例。

 

基类的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace CSarp_DB_Sample
{
    class DBbase :IDisposable
    {
        public interface ITableOperate
        {
            void AddNew();
            void Update(long ID);
        }
        //'*可以在基类和派生类中访问
        protected string sTable = "" ;

        protected string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Password= ;User ID=Admin;Data Source="
                                + System.Environment.CurrentDirectory + "//DataBase//LeeDB.mdb";

        static void Main()
        {
            Application.Run(new FrmTest());
        }

        #region ExecuteSQL 两个重载方法
        public DataTable ExecuteSQL(string sSQL,out long RcdCount)
        {
            // ==========================================================
            //     开发人员:段利庆
            //     编写时间:09-12-08
            //     过程名称:ExecuteSQL
            //     参数说明:sSQL     查询语句 不能包含 :INSERT,DELETE,UPDATE   必须包含:SELECT
            //              
            //     功能说明:执行数据库的查询 返回一个数据表
            //               查询错误 输入参数【RcdCount】 = "-1"
            // ==========================================================
            DataTable tmpDt = new DataTable();
            try
            {              
                System.Console.WriteLine(sConn);

                System.Data.OleDb.OleDbConnection oleconn = new System.Data.OleDb.OleDbConnection(sConn);
               
                System.Data.OleDb.OleDbCommand cmd;
                cmd = new System.Data.OleDb.OleDbCommand(sSQL, oleconn);

                cmd.CommandType = CommandType.Text;

                string[] sTokens;
                sTokens = sSQL.Split((Char)32 );

               
                if ("INSERT,DELETE,UPDATE".IndexOfAny(sTokens[0].ToUpper().ToCharArray())  == 0)    
                {
                    if (oleconn.State != ConnectionState.Open)
                    {
                        oleconn.Open(); //打开数据库连接                         
                    }

                    cmd.ExecuteNonQuery();  //执行SQL语句

                    if (oleconn.State != ConnectionState.Closed)
                    {               
                        oleconn.Close(); //关闭数据库连接
                    }
                    if (sTokens[0].ToUpper() == "INSERT")
                    {
                        System.Console.WriteLine("插入记录成功" + sSQL);
                    }
                    if (sTokens[0].ToUpper()== "DELETE")
                    {
                        System.Console.WriteLine("删除记录成功" + sSQL);
                    }
                    if (sTokens[0].ToUpper() == "UPDATE")
                    {
                        System.Console.WriteLine("更新记录成功" + sSQL);
                    }

                    RcdCount = -1;
                    return tmpDt;
                }
            else
            {
                System .Data .DataSet ObjectdsDataSet;
                ObjectdsDataSet =new System .Data .DataSet();
                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter();
                adapter.TableMappings.Add("Table", "TEMP");
                adapter.SelectCommand = cmd;
                if (oleconn.State != ConnectionState.Open)
                {
                    oleconn.Open() ; //打开数据库连接
                }
                cmd.ExecuteNonQuery();  //执行SQL语句
                if (oleconn.State != ConnectionState.Closed)
                {
                    oleconn.Close(); //关闭数据库连接
                }
                adapter.Fill(ObjectdsDataSet); //填充数据集

                RcdCount = ObjectdsDataSet.Tables[0].Rows.Count;
                return ObjectdsDataSet.Tables[0];
            }
            }
            catch (Exception  ee)
            {

                MessageBox.Show(ee.Message.ToString() + "/n" + sSQL);
                RcdCount = -1;
                return tmpDt;
            }

        }

        public void ExecuteSQL(string sSQL,bool bMsg)
        {

            // ==========================================================
            //     开发人员:段利庆
            //     编写时间:09-12-08
            //     过程名称:ExecuteSQL
            //     参数说明:sSQL     查询语句 必须包含 :INSERT,DELETE,UPDATE
            //               bMsg     查询操作完成后 是否显示消息框
            //     功能说明:执行数据库的 插入 删除 更新 操作
            //
            // ==========================================================           
            string sMessage = "";

            try
            {
                System.Console.WriteLine(sConn);

                System.Data.OleDb.OleDbConnection oleconn = new System.Data.OleDb.OleDbConnection(sConn);

                System.Data.OleDb.OleDbCommand cmd;
                cmd = new System.Data.OleDb.OleDbCommand(sSQL, oleconn);

                cmd.CommandType = CommandType.Text;

                string[] sTokens;
                sTokens = sSQL.Split((Char)32);

                if (" INSERT,DELETE,UPDATE".IndexOfAny(sTokens[0].ToUpper().ToCharArray()) != 0)
                {
                    if (oleconn.State != ConnectionState.Open)
                    {
                        oleconn.Open(); //打开数据库连接                         
                    }

                    cmd.ExecuteNonQuery();  //执行SQL语句

                    if (oleconn.State != ConnectionState.Closed)
                    {
                        oleconn.Close(); //关闭数据库连接
                    }
                    if (sTokens[0].ToUpper() == "INSERT")
                    {
                        sMessage = "插入记录成功";
                        System.Console.WriteLine(sMessage + sSQL);
                    }
                    if (sTokens[0].ToUpper() == "DELETE")
                    {
                        sMessage = "删除记录成功";
                        System.Console.WriteLine(sMessage + sSQL);
                    }
                    if (sTokens[0].ToUpper() == "UPDATE")
                    {
                        sMessage = "更新记录成功";
                        System.Console.WriteLine(sMessage + sSQL);
                    }

                    if (bMsg)
                    {
                        MessageBox.Show(sMessage, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                else
                {
                    MessageBox.Show("输入的数据库操作语句不包含:INSERT,DELETE,UPDATE" + "/n" + sSQL);
                }
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.Message.ToString() + "/n" + sSQL);
            }
        }

        #endregion

        //虚方法 在派生类中可以改变
        public virtual void Delete(long ID)
        {
            string strSQL = "";
            //永久删除
            //strSQL = "DELETE FROM " & sTable & " " +
            //          "WHERE 编号 = " & ID + " ";

            //保留记录原型,仅做删除标记操作
            strSQL = "UPDATE " + sTable + " " +
                        "SET 删除 = " + "1" + " " +
                      "WHERE 编号 = " + ID + " ";

            ExecuteSQL(strSQL, false);

        }

        public void  DeleteMsg()
        {
            string  strMessage;
            strMessage = "删除记录成功!";
            MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //采用接口【ITableOperate】实现此功能
        //public virtual void AddNew()
        //{

        //}

        //public virtual void UpDate(long ID)
        //{

        //}

        //定义这个虚方法 允许数据填入到ListView 或DataGrid
        public virtual void Full(ref ListView LV)
        {

        }

        //重载上面的方法 填充数据 到DataGridView
        public virtual void Full(ref DataGridView DaGv)
        {

        }

        public String QueryStr(String str)
       
        // ==========================================================
        //     开发人员:段利庆
        //     编写时间:09-12-07
        //     过程名称:QueryStr
        //     参数说明:str      待格式 的字符串
        //
        //     功能说明:返回 要过滤的查询字符串格式   例如: 'string'
        //
        // ==========================================================
        {
            return "'" + str + "'";
           
        }

        public string DateToSQLstr(DateTime dateVal, bool b)
        //'' ==========================================================
        //'     开发人员:段利庆
        //'     编写时间:05-6-26
        //'     函数名称:DateToSQLstr
        //'     参数说明:Date_Value  时间值
        //'              blnTime 开始时间(False) 00:00:00点  或(True)23:59:59
        //'     功能说明:将日期值转换为SQL查询可用的字符串

        //'' ==========================================================
        {
            if (b)
            {
                return "#" + dateVal.Month + "-" + dateVal.Day + "-" + dateVal.Year + " 00:00:00#";
            }
            else
            {
                return "#" + dateVal.Month + "-" + dateVal.Day + "-" + dateVal.Year + " 23:59:59#";
            }
        }

        public string DateToSQLstrServer(DateTime dateVal,bool b)
        //'' ==========================================================
        //'     开发人员:段利庆
        //'     编写时间:2009-12-11
        //'     函数名称:DateToSQLstr
        //'     参数说明:Date_Value  时间值
        //'              blnTime 开始时间(False) 00:00:00点  或(True)23:59:59
        //'     功能说明:将日期值转换为SQL查询可用的字符串
        //'               适用于SQL SERVER
        //'' ==========================================================
        {
            if (b)
            {
                return " '" + dateVal.Month + "-" + dateVal.Day + "-" + dateVal.Year + " 00:00:00' ";
            }
            else
            {
                return " '" + dateVal.Month + "-" + dateVal.Day + "-" + dateVal.Year + " 23:59:59' ";
            }
        }
        public string   DateToQuenyDateTime(DateTime  DateVal )
            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:09-07-31
            //'     函数名称:DateToQuenyDateTime
            //'     参数说明:DateVal  日期 时间值
            //'     功能说明:将日期 时间值转换为SQL查询可用的字符串
            //'               适用于ACCESS
            //'               转换后的结果如下:
            //'               #2009-7-31 14:51:12#

            //'' ==========================================================
        {
            return "#" + DateVal.Month.ToString() + "-" + DateVal.Day.ToString() + "-" + DateVal.Year.ToString() + " " +
                         DateVal.Hour.ToString() + ":" + DateVal.Minute.ToString() + ":" + DateVal.Second.ToString() + "#";

        }

        public long GetRecordCount(string strSQL)
        {
            DataTable tTable = new DataTable() ;
            long RcdCount;
            tTable = ExecuteSQL (strSQL,out RcdCount  );
            return tTable.Rows.Count;

        }

        public bool IsAlone(string sTalbe,
                            string IDFiled,
                            long IDVal,
                            string sFiled,
                            string sFiledVal)
        //'' ==========================================================
        //'     开发人员:段利庆
        //'     编写时间:2009-12-11
        //'     函数名称:sTalbe        表名称
        //'     参数说明:IDFiled       数据表索引ID字段的名称  例: 编号
        //'               IDVal         数据表索引ID字段的数值  例: 120
        //'               sFiled        要测试不重复的字段的名称
        //'               sFiledVal     要测试不重复字段的字符串值
        //'
        //'     功能说明:在更新操作前,检查除本条记录外,是否和其他的此字段值有重复
        //'               数据库设计时 采用字段属性为【索引无重复】 目的相同
        //'               无重复 为【True】
        //'' ==========================================================
        {

            string  strSQL;

            strSQL = "SELECT " + IDFiled + " , " +
                                 sFiled + " " +
                       "FROM " + sTalbe + " " +
                      "WHERE " + IDFiled + " <> " + IDVal + " AND " +
                                 sFiled + " = " + QueryStr(sFiledVal) + " ";

            if (this.GetRecordCount(strSQL) == 0)
            {
                return true;
            }

            else
            {
                return false;
            }

        }

            public void QueryToLV(String strSQL , ref ListView LV  , int ImageID  )

            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:2009-12-11
            //'     过程名称:QueryToLV
            //'     参数说明:strSQL     查询字符串
            //'               Lv      ListView
            //'               ImageID   绑定ImageList的图标 ID号
            //'     功能说明:将查询结果导入到<ListView>

            //'' ==========================================================
            {
                //Dim SQLString As  = strSQL
                long iCount = 0;
                DataTable UserTable = ExecuteSQL(strSQL, out iCount);

                if (iCount == -1)
                {
                    MessageBox.Show("查询错误");
                    return;
                }

                LV.Items.Clear();
                LV.Columns.Clear();

                LV.View = View.Details;
                LV.GridLines = true;
                LV.FullRowSelect = true;

                //'*绑定主窗体上的【ImgLst】
                //LV.SmallImageList = FrmMain.ImgLst16;

                for (int i = 0 ; i <= UserTable.Columns.Count - 1; i++ )
                {
                    LV.Columns.Add(UserTable.Columns[i].ColumnName);
                }

                int  RowCount ;
                string  RowName ;
                int  FidldsCount ;

                ListViewItem LItem ;
                for (RowCount = 0 ; RowCount<= UserTable.Rows.Count - 1;RowCount++)
                {
                    RowName = UserTable.Rows[RowCount].ItemArray[0].ToString();
                   // 'LV.Items.Add(RowName)
                    LItem = new ListViewItem(RowName, ImageID);
                    for (FidldsCount = 1 ; FidldsCount <= UserTable.Columns.Count - 1;FidldsCount++)
                    {

                        LItem.SubItems.Add(UserTable.Rows[RowCount].ItemArray[FidldsCount].ToString());
                    }

                    LV.Items.Add(LItem);
                }

            }
      
        public long GetRecordID(string  TableName ,
                                string FieldName ,
                                string FieldValue ,
                                string IDName )

            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:2009-12-11
            //'     函数名称:GetRecordID
            //'     参数说明:TableName   表名称
            //'               FieldName   字段名称
            //'               FieldValue  字段值
            //'               IDName      编号名称
            //'     功能说明:根据字段名称(数据库字段属性设为不重复)获得编号,应用于多表关系中

            //'' ==========================================================
        {

            //string  strMessage ;
            string  strSQL ;
            long    RcdCount ;

            long lngResult;

            strSQL = "SELECT " + IDName + ", " + FieldName + " " +
                       "FROM " + TableName + " " +
                      "WHERE " + FieldName + " = " + QueryStr(FieldValue);

            DataTable tTable = ExecuteSQL(strSQL, out RcdCount);

            if (RcdCount == -1)
            {
                lngResult = -1;
                System.Console.WriteLine("查询错误");
            }

            RcdCount = tTable.Rows.Count;

            if (RcdCount == 0 )
            {
                System .Console .WriteLine("记录表内 该名称的 字段 不存在");
                lngResult = -1;
                goto  PROC_EXIT;
            }

            if (RcdCount > 1 )
            {
                System .Console .WriteLine("记录表内 该名称的 字段 有重复");
                lngResult = -1;
                goto PROC_EXIT;
            }

            lngResult = (long)tTable.Rows[0][IDName];

            PROC_EXIT:

            return lngResult;

        }

        public string  GetRecordStr(string  TableName ,
                                 string FieldName ,
                                 long IDValue ,
                                 string IDName )

            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:2009-12-11
            //'     函数名称:GetRecordStr
            //'     参数说明:TableName   表名称
            //'               FieldName   字段名称
            //'               IDValue  字段值
            //'               IDName      编号名称
            //'     功能说明:根据字段[编号](数据库字段属性设为不重复)获得字段字符值,应用于多表关系中

            //'' ==========================================================

        {

            //string  strMessage ;

            string  strSQL;
            long RcdCount; // '记录总数

            string strResult;

            strSQL = "SELECT " + IDName + ", " + FieldName + " " +
                       "FROM " + TableName + " " +
                      "WHERE " + IDName + " = " + IDValue;

            DataTable tTable   = ExecuteSQL (strSQL,out RcdCount );

            if ( RcdCount == -1)
            {
                System .Console .WriteLine ("查询错误");
                strResult = "Error Message ~~!!";
            }

            RcdCount = tTable.Rows.Count;

            if (RcdCount == 0)
            {
                System .Console .WriteLine("记录表内 该名称的 字段 不存在");
                strResult = "Error Message ~~!!";
                goto PROC_EXIT;
            }

            if (RcdCount > 1)
            {
                System .Console .WriteLine("记录表内 该名称的 字段 有重复");
                strResult = "Error Message ~~!!";
                goto PROC_EXIT;
            }

            strResult = tTable.Rows[0][FieldName].ToString() ;

           PROC_EXIT:
            return strResult ;

        }

        public long  GetMaxIndexVal(string  sTable , string  sField )
            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:2009-12-11
            //'     函数名称:GetMaxIndexVal
            //'     参数说明:sTable   表名称
            //'               sField   字段名称  字段属性必须为数字

            //'     功能说明:获得表内最大的数字值, 防止添加有重复的字段值

            //'' ==========================================================
        {

            string strSQL ;
            long   RcdCount ; //  '记录总数

            strSQL = "SELECT Max(" + sField + ") AS MaxVal " +
                       "FROM " + sTable + " ";

            DataTable tTable  = ExecuteSQL  (strSQL,out RcdCount );
            if (RcdCount == -1)
            {
                System .Console .WriteLine ("查询错误");
                return -1;
            }

            return (long )tTable.Rows[0]["MaxVal"];
        }
   
        public void  LoadFieldToComb(string  TableName ,
                                     string  FieldName ,
                                     ref  ComboBox Comb )

                //'' ==========================================================
                //'     开发人员:段利庆
                //'     编写时间:2009-12-11
                //'     过程名称:LoadFieldToComb
                //'     参数说明:TableName  表名称
                //'                         FieldName   字段名称
                //'                         Comb  要载入的下拉列表框控件
                //'     功能说明:将数据库的一个字段内容载入下拉列表框

                //'' ==========================================================
        {

            //string strMessage ;
            string  strSQL  ;

            strSQL = "SELECT " + FieldName + " " +
                       "FROM " + TableName;

            long  RcdCount;   //  '记录总数

            DataTable tTable  = ExecuteSQL (strSQL,out RcdCount );
            if (RcdCount == -1)
            {
                System .Console .WriteLine ("查询错误");
            }

            RcdCount = tTable.Rows.Count;

            if (RcdCount == 0 )
            {
                System .Console .WriteLine("记录表内 该名称的 字段 不存在");
                return ;
            }

            Comb.Items.Clear();
            for (int  i = 0 ;i<= RcdCount - 1;i++)
            {
                Comb.Items.Add(tTable.Rows[i].ItemArray[0].ToString());
            }

        }
        public Object GetDBFileVal(string  TableName ,
                                   string  FieldName ,
                                     long  IDValue,
                                     string  IDName )

                //'' ==========================================================
                //'     开发人员:段利庆
                //'     编写时间:2009-12-11
                //'     函数名称:GetRecordStr
                //'     参数说明:TableName   表名称
                //'               FieldName   字段名称
                //'               IDValue  字段值
                //'               IDName      编号名称
                //'     功能说明:根据字段[编号](数据库字段属性设为不重复)获得字段值
                //'         其他:其中的返回值 采用【Object】目的是包含 数字,字符串,真假值     
                //'' ==========================================================
        {

            string  strMessage ;

            string  strSQL;
            long  RcdCount ;           //  '记录总数

            object objResult;

            strSQL = "SELECT " + IDName + ", " + FieldName + " " +
                       "FROM " + TableName + " " +
                      "WHERE " + IDName + " = " + IDValue;

            DataTable tTable = ExecuteSQL(strSQL ,out RcdCount );
           
            if (RcdCount == -1)
            {
                System .Console .WriteLine ("查询错误");
            }
            RcdCount = tTable.Rows.Count;

            if (RcdCount == 0 )
            {
                strMessage = "表名称:" + TableName + "/n" +
                             "编号:" + IDValue.ToString() + "/n" +
                             "记录不存在!";
         
                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                objResult =  -1;
                goto PROC_EXIT;
             }

            if (RcdCount > 1 )
            {
                strMessage = "表名称:" + TableName + "/n" +
                             "编号:" + IDValue.ToString() + "/n" +
                             "记录有重复!";

                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                objResult = -1;
                goto PROC_EXIT;
            }

            objResult = tTable.Rows[0][FieldName];

            PROC_EXIT:;

            return objResult ;
              
        }

        public void SetDBFileVal(string  TableName ,
                                 string  FieldName ,
                                 Object oFieldVal,
                                 long  IDValue,
                                 string  IDName )

            //'' ==========================================================
            //'     开发人员:段利庆
            //'     编写时间:2009-12-11
            //'     函数名称:SetRecordStr
            //'     参数说明:TableName   表名称
            //'               FieldName   字段名称
            //'               IDValue  字段值
            //'               IDName      编号名称
            //'     功能说明:根据字段[编号](数据库字段属性设为不重复)设置字段值 保存到数据库
            //'         其他:其中的返回值 采用【Object】目的是包含 数字,字符串,真假值     
            //'' ==========================================================
       {
            string  strMessage ;

            string  strSQL;
            long  RcdCount ;  //  '记录总数

            strSQL = "SELECT " + IDName + ", " + FieldName + " " +
                       "FROM " + TableName + " " +
                      "WHERE " + IDName + " = " + IDValue;

            DataTable tTable  = ExecuteSQL (strSQL ,out RcdCount );

            //'*更具不同的数据类型   确定SQL 可识别的 字段值   字符串 +'xxx'    日期  # XXX #
            switch  (tTable.Rows[0][FieldName].GetType().ToString())
            {
                case "System.String":
                    oFieldVal = QueryStr(oFieldVal.ToString());
                    break ;
                case "System.DateTime":
                    oFieldVal = DateToQuenyDateTime((DateTime )oFieldVal);
                    break ;
                default:
                    break ;

            }

            RcdCount = tTable.Rows.Count;

            if (RcdCount == 0)
            {
                strMessage = "表名称:" + TableName + "/n" +
                             "编号:" + IDValue.ToString() + "/n" +
                             "记录不存在!";

                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return ;
            }

            if (RcdCount > 1 )
            {
                strMessage = "表名称:" + TableName + "/n" +
                             "编号:" + IDValue.ToString() + "/n" +
                             "记录有重复!";

                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return ;
            }

            strSQL = "UPDATE " + TableName + " " +
                        "SET " + FieldName + " = " + oFieldVal.ToString() + " " +
                      "WHERE " + IDName + " = " + IDValue;

            ExecuteSQL (strSQL,out RcdCount );

            if (RcdCount == -1)
            {
                //'*判断是否被正确执行
                //'*此处没有意思

            }

         }

        #region IDisposable 成员

        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

    }
}

 

派生类代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace CSarp_DB_Sample
{
    class DBLogin:DBbase , DBbase .ITableOperate
    {

    public   long ID = 0 ;                    //编号
    public string  sCaption = "";             //名称      
    public string  sPassWord = "";            //口令
    public string  sLevel = "";               //级别    
    public string  sMemo  = "";               //备注
    public   bool  bDelTag =false ;           //删除标记

    public bool  bMenu_1 = false ;            //菜单一权限
    public bool  bMenu_2 = false ;            //菜单二权限
    public bool  bMenu_3 = false ;            //菜单三权限
    public bool  bMenu_4  = false ;           //菜单四权限
    public bool  bMenu_5 = false ;            //菜单五权限
    public bool  bMenu_6 = false;             //菜单六权限

        //构造函数
        public DBLogin()
        {
            this.sTable = "操作员";
        }

        //析构函数
         ~DBLogin()
        {
            System.Console.WriteLine("析构函数");
            //MessageBox.Show("析构函数测试");
        }
       
        //遮盖 父类的 同名方法
        //这里 仅作测试用
        //public override void  Delete(long ID)
        //{
        //    string  strSQL;

        //    //'strSQL = "DELETE FROM " & sTable & " " & _
        //    //'               "WHERE 编号 = " & ID

        //    strSQL = "UPDATE " + sTable + " " +
        //                "SET 删除 = " + "1" + " " +
        //              "WHERE 编号 = " + ID;

        //    ExecuteSQL (strSQL,false  );

        //}

        public override void Full(ref ListView LV)
        {
            string  strSQL;
            strSQL = "SELECT 编号 as _,名称,口令,级别,备注,删除, MENU_1,MENU_2,MENU_3 " +
                       "FROM " + this.sTable + " " +
                      "WHERE 删除 = 0 " +
                   "ORDER BY 编号 ";

            this.QueryToLV(strSQL, ref LV,1);

            //'*设置列宽度
            LV.Columns[0].Width = 18;
            LV.Columns[1].Width = 60;
            LV.Columns[2].Width = 80;

            LV.Columns[3].Width = 60;
            LV.Columns[4].Width = 60;
            LV.Columns[5].Width = 60;
        }

        //public void Update(long ID)
        //{
        //    //throw new Exception("The method or operation is not implemented.");
        //}

        public string AttachCode()
        //'随机生成五位附加验证码A~Z
        {
            string  TempCode  = "";
            int randomvalue = 0;

            System .Random Rand = new Random() ;

            for (int  i = 1; i <= 5 ; i++)
            {
                randomvalue = Rand .Next(65,90);       
                TempCode = TempCode + (char) randomvalue;
            }
            return TempCode;

        }

        public bool LoginConfirm()
        {
            bool bResult = false;

            string strMessage = "";
            string strSQL = "";
            strSQL = "SELECT 编号,名称,口令,级别,备注,删除, MENU_1,MENU_2,MENU_3 " +
                       "FROM 操作员 " +
                      "WHERE 名称 = " + this.QueryStr(this.sCaption) + " AND " +
                            "口令 = " + this.QueryStr(this.sPassWord) + " ";
           
            if (this.GetRecordCount(strSQL ) != 1)
            {
                strMessage = "输入用户名或密码有误,请重试";
                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                goto  PROC_EXIT;
            }

            long iCount = 0 ;
            DataTable tTable; //= new DataTable() ;
            tTable =  this.ExecuteSQL(strSQL, out iCount);

            if (iCount == -1)
            {
                goto PROC_EXIT;
            }

            this.sCaption = tTable.Rows[0]["名称"].ToString() ;
            this.sLevel = tTable.Rows[0]["级别"].ToString() ;
            this.bMenu_1 = System .Convert.ToBoolean ( tTable.Rows[0]["MENU_1"]);
            this.bMenu_2 = System.Convert.ToBoolean(tTable.Rows[0]["MENU_2"]);
            this.bMenu_3 = System.Convert.ToBoolean(tTable.Rows[0]["MENU_3"]);

            bResult = true;
           
            PROC_EXIT: ;

            return bResult;
           
        }

        #region ITableOperate 成员

        public void AddNew()
        {
            //throw new Exception("The method or operation is not implemented.");

            string strMessage;
            string strSQL = "";

            strSQL = "SELECT 编号,名称,口令,级别,备注,删除, MENU_1,MENU_2,MENU_3 " +
                     "FROM  " + this.sTable + " " +
                     "WHERE 名称 = " + this.QueryStr(this.sCaption );
           
            if (this .GetRecordCount (strSQL ) >=1 )
            {
                strMessage = "操作员名称已存在";
                MessageBox.Show(strMessage, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                goto PROC_EXIT;
            }

            strSQL = "INSERT INTO " + this.sTable + "(名称,口令,级别,备注,删除, MENU_1,MENU_2,MENU_3 ) " +
                                "VALUES( " + this.QueryStr(this.sCaption) + "," +
                                             this.QueryStr(this.sPassWord) + "," +
                                             this.QueryStr(this.sLevel) + "," +
                                             this.QueryStr(this.sMemo) + "," +
                                             "False" + "," +
                                             this.bMenu_1.ToString() + "," +
                                             this.bMenu_2.ToString() + "," +
                                             this.bMenu_3.ToString() + ")";
           
            this.ExecuteSQL(strSQL, true);
       
            PROC_EXIT: ;
        }

        //void DBbase.ITableOperate.Update(long ID)
        //{
        //    【第一种】
        //    这种定义 是实现接口默认的定义 方式
        //    这样定义的接口,只能通过接口的赋值后的方法实现
        //   例子:
        //        DBbase.ITableOperate ILogin;
        //        ILogin = objData ;
        //        ILogin.Update(ID);
        //   而不能作为该类对象的成员方法调用
        //      【错误】
        //        objData.Update (ID );
        //    【第二种】  
        //     下面采用public void Update(long ID) 定义实现的接口方法
        //    
        //   例子:
        //        DBbase.ITableOperate ILogin;
        //        ILogin = objData ;
        //        ILogin.Update(ID);
        // 
        //      上面的功能同时还可以作为 类的对象成员直接调用
        //      【正确】
        //        objData.Update (ID );  
        //   
        //}

        public void Update(long ID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string  strMessage;

            //'*防止产生同名称
            string  sFiled = "";
            sFiled = "名称";
            if ( !IsAlone(this.sTable, "编号", ID, sFiled,this.sCaption))
            {
                strMessage = sFiled + "已经存在,更新失败。" + "/n" +
                             "请更改后重试。";
                MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                goto  PROC_EXIT;
            }

            string  strSQL = "";
            strSQL = "UPDATE " + this.sTable + " " +
                        "SET 名称 = " + this.QueryStr(this.sCaption) + ", " +
                            "口令 = " + this.QueryStr(this.sPassWord) + ", " +
                            "级别 = " + this.QueryStr(this.sLevel) + ", " +
                            "备注 = " + this.QueryStr(this.sMemo.ToString()) + ", " +
                            "删除 = " + this.bDelTag.ToString() + ", " +
                            "MENU_1 = " + this.bMenu_1.ToString() + ", " +
                            "MENU_2 = " + this.bMenu_2.ToString() + ", " +
                            "MENU_3 = " + this.bMenu_3.ToString() + " " +
                     "WHERE 编号 = " + ID;

            this .ExecuteSQL (strSQL ,true );

            PROC_EXIT:;

        }

        #endregion

【上篇】
【下篇】

抱歉!评论已关闭.