数据库实现,以及工厂方法模式实现
生活随笔
收集整理的這篇文章主要介紹了
数据库实现,以及工厂方法模式实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
計(jì)算類的定義
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("除數(shù)不能為零!");}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();}} }具體的調(diào)用
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);}} }數(shù)據(jù)庫的操作的類
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()//鏈接數(shù)據(jù)庫 {try{sqlcon = new SqlConnection(strCon);}catch (Exception e){MessageBox.Show("數(shù)據(jù)庫連接失敗請重新鏈接:" + e.ToString());}}public void del(string sltstr)//刪除所有題 {dbcon();sqlcon.Open();SqlCommand sqlcom = new SqlCommand(sltstr, sqlcon);sqlcom.ExecuteNonQuery();MessageBox.Show("刪除成功,請繼續(xù)編題!");}public void dbinser(string inster)//插入數(shù)據(jù)存題 {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("你的題做完了,去休息吧!一會我們繼續(xù)!");}}i++;}} }數(shù)據(jù)庫的建立
form的設(shè)計(jì)及代碼
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";//清除表中數(shù)據(jù) db.del(sltstr);}private void button3_Click(object sender, EventArgs e){Form2 fa = new Form2();fa.ShowDialog();}} }
form的代碼及設(shè)計(jì)
?
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();}} }測試
?
總結(jié):寫這程序用了很長時間,建庫,進(jìn)行代碼的編寫,等等。但是在寫工廠方法模式中有些好奇為什么感覺沒有策咯模式那么好用!
?
轉(zhuǎn)載于:https://www.cnblogs.com/lizanqirxx/p/5036752.html
總結(jié)
以上是生活随笔為你收集整理的数据库实现,以及工厂方法模式实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 7.0 上安装和配置 VN
- 下一篇: 带数据库的智能合约