zoukankan      html  css  js  c++  java
  • 数据库实现,以及工厂方法模式实现

    计算类的定义

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SQL
    {
        interface operater1
        {
    
            int calculate(int a, int b);
    
        }
    
        class Add : operater1
        {
            public int calculate(int a, int b)
            {
                return a + b;
    
            }
        }
    
        class Sub : operater1
        {
            public int calculate(int a, int b)
            {
                return a - b;
            }
        }
    
        class Mul : operater1
        {
            public int calculate(int a, int b)
            {
                return a * b;
            }
        }
    
        class Div : operater1
        {
    
            public int calculate(int a, int b)
            {
    
                if (b == 0)
                {
    
                    throw new Exception("除数不能为零!");
    
                }
    
                else
                {
    
                    return a / b;
    
                }
    
            }
        }
    }
    

     工厂方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SQL
    {
        interface Ifact//建一个工厂接口
        {
            operater1 CreatrOper();
        }
        class Addfactory : Ifact//加法工厂
        {
            public operater1 CreatrOper()
            {
                return new Add();
            }
        }
        class Subfactory : Ifact//减法工厂
        {
            public operater1 CreatrOper()
            {
                return new Sub();
            }
        }
        class Mulfactory : Ifact//乘法工厂
        {
            public operater1 CreatrOper()
            {
                return new Mul();
            }
     
        }
        class Divfactory : Ifact//除法工厂
        {
            public operater1 CreatrOper()
            {
                return new Div();
            }
        }
    }
    

    具体的调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SQL
    {
        class Factory1
        {
            private Ifact fact;
            private operater1 oper1;
            public Factory1(string operation)
            {
                switch (operation)
                {
                    case "+":
                        fact = new Addfactory();
                        oper1 = fact.CreatrOper();
                        break;
                    case "-":
                        fact = new Subfactory();
                        oper1 = fact.CreatrOper();
                        break;
                    case "*":
                        fact = new Mulfactory();
                        oper1 = fact.CreatrOper();
                        break;
                    case "/":
                        fact = new Divfactory();
                        oper1 = fact.CreatrOper();
                        break;
    
                }
                
            }
            public int claations(int a, int b)
            {
                return oper1.calculate(a, b);
            }
        }
    }

    数据库的操作的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using System.Windows.Forms;
    
    namespace SQL
    {
        class DB
        {
            public string strCon = @"Data Source=.;Initial Catalog=Tiku;Integrated Security=True";
            public SqlConnection sqlcon = new SqlConnection();
            public SqlDataAdapter sda = new SqlDataAdapter();
            public DataSet ds = new DataSet();
            public DataTable dt = new DataTable();
           public string number1;
           public string operation;
           public string number2;
           public int i;
            public void dbcon()//链接数据库
            {
                try
                {
                    sqlcon = new SqlConnection(strCon);
                }
                catch (Exception e)
                {
                    MessageBox.Show("数据库连接失败请重新链接:" + e.ToString());
                }
    
     
            }
            public void del(string sltstr)//删除所有题
            {
                dbcon();
                sqlcon.Open();
                SqlCommand sqlcom = new SqlCommand(sltstr, sqlcon);
                sqlcom.ExecuteNonQuery();
                MessageBox.Show("删除成功,请继续编题!");
                
            }
            public void dbinser(string inster)//插入数据存题
            {
                sqlcon = new SqlConnection(strCon);
                sqlcon.Open();
                SqlCommand sqlcom=new SqlCommand(inster ,sqlcon);
                try
                {
                    int a = sqlcom.ExecuteNonQuery();
                    if (a > 0)
                    {
                        MessageBox.Show("保存成功!");
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("保存失败!" + e.ToString());
                }
    
            }
            public void Reader(string sltstr)//读题的方法
            {
                sqlcon = new SqlConnection(strCon);
                sqlcon.Open();
                SqlCommand sqlcom = new SqlCommand(sltstr, sqlcon);
                sda = new SqlDataAdapter(sqlcom);
                sda.Fill(ds);
                dt=ds.Tables[0];
                if (i <dt.Rows.Count)
                {
                    number1 = dt.Rows[i][0].ToString().Trim();
                    operation = dt.Rows[i][1].ToString().Trim();
                    number2 = dt.Rows[i][2].ToString().Trim();
                    if (i == (dt.Rows.Count - 1))
                    {
                        MessageBox.Show("你的题做完了,去休息吧!一会我们继续!");
                    }
     
                }
                i++;
     
            }
    
        }
    }

    数据库的建立

    form的设计及代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SQL
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            DB db = new DB();
            
            private void button1_Click(object sender, EventArgs e)
            {
                string inter = "insert into Ti(number1,operation,number2) values('" + textBox1.Text + "','" + comboBox1.Text + "','" + textBox2.Text + "')";
                db.dbinser(inter);
                textBox1.Clear();
                textBox2.Clear();
                
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string sltstr = @"truncate table Ti";//清除表中数据
                db.del(sltstr);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                Form2 fa = new Form2();
                fa.ShowDialog();
            }
    
           
        }
    }


    form的代码及设计

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SQL
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            public static int right;
            public static int count;
            DB db = new DB();
            private void button1_Click(object sender, EventArgs e)
            {
                duti();
            }
    
            private void textBox3_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    int a = int.Parse(textBox1.Text);
                    int b = int.Parse(textBox2.Text);
                    Factory1 factory = new Factory1(label1.Text);
                    int answer = factory.claations(a, b);
                    if (textBox3.Text == answer.ToString())
                    {
                        MessageBox.Show("回答正确!");
                        right++;
                    }
                    else
                    {
                        MessageBox.Show("回答错误!");
                    }
                    count++;
                    textBox3.Clear();
                    duti();
                 
                }                     
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Form3 fds = new Form3();
                fds.ShowDialog();
            }
            private void duti()
            {
                string sltstr = @"select number1,operation,number2 from Ti";
                db.Reader(sltstr);
                textBox1.Text = db.number1;
                textBox2.Text = db.number2;
                label1.Text = db.operation;
            }
        }
    }

    form3的代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace SQL
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            private void Form3_Load(object sender, EventArgs e)
            {
                textBox1.Text = Form2.right.ToString();
                textBox2.Text = (Form2.count - Form2.right).ToString();
                textBox3.Text = ((Form2.right / (double)(Form2.count)) * 100).ToString() + "%";
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }

    测试

    总结:写这程序用了很长时间,建库,进行代码的编写,等等。但是在写工厂方法模式中有些好奇为什么感觉没有策咯模式那么好用!

  • 相关阅读:
    golang手动导入github net(https://github.com/golang/net)库到本地(Windows)使用
    lstm例子generate_movies函数分析
    python使用pylab一幅图片画多个图
    python数组array的transpose方法
    python Parallel delayed 示例
    json&pickle数据序列化模块
    html的标签分类————body内标签系列
    html的标签分类————可以上传的数据篇
    不可将布尔值直接与true或者1进行比较
    mock
  • 原文地址:https://www.cnblogs.com/lizanqirxx/p/5036752.html
Copyright © 2011-2022 走看看