1,首先定义一个数据库连接与操作的抽象类,这个里面写一些你需要用到的一些抽象方法。
然后就是对应你的数据库写与之对应的类并继承你说定义的抽象类,并重写抽象类里面的方法!
对应代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace DBASES { public abstract class DBastr { abstract public int Insert(string number1,string operation, string number2); abstract public int Delect(); abstract public DataTable Gettable(); abstract public void Open(); abstract public void Close(); } public class TIASS : DBastr { public static string constr = "Data Source=.;Initial Catalog=TIASS;Integrated Security=True"; SqlConnection conn = new SqlConnection(constr); public override void Open() { conn = new SqlConnection(constr); conn.Open(); } public override void Close() { conn.Close(); } public override int Insert(string number1, string operation, string number2) { string cmdstr = "insert into TI(number1,operation,number2) values (‘"+number1+"‘,‘"+operation+"‘,‘"+number2+"‘)"; Open(); SqlCommand cmd = new SqlCommand(cmdstr, conn); int a = cmd.ExecuteNonQuery(); Close(); return a; } public override int Delect() { string cmdstr = "delete from TI"; Open(); SqlCommand cmd = new SqlCommand(cmdstr, conn); int a = cmd.ExecuteNonQuery(); Close(); return a; } public override DataTable Gettable() { string cmdstr = "select number1,operation, number2 from TI"; SqlDataAdapter dat = new SqlDataAdapter(cmdstr,conn); DataTable dt = new DataTable(); dat.Fill(dt); return dt; } } public class TIBD : DBastr { public static string constr = "Data Source=.;Initial Catalog=TIBD;Integrated Security=True"; SqlConnection conn = new SqlConnection(constr); public override void Open() { conn.Open(); } public override void Close() { conn.Close(); } public override int Insert(string number1, string operation, string number2) { string cmdstr = "insert into TA(number1,operation,number2) values (‘" + number1 + "‘,‘" + operation + "‘,‘" + number2 + "‘)"; Open(); SqlCommand cmd = new SqlCommand(cmdstr, conn); int a = cmd.ExecuteNonQuery(); Close(); return a; } public override int Delect() { string cmdstr = "delete from TA"; Open(); SqlCommand cmd = new SqlCommand(cmdstr, conn); int a = cmd.ExecuteNonQuery(); Close(); return a; } public override DataTable Gettable() { string cmdstr = "select number1,operation, number2 from TA"; SqlDataAdapter dat = new SqlDataAdapter(cmdstr, conn); DataTable dt = new DataTable(); dat.Fill(dt); return dt; } } public class Faction //定义工厂类 { public DBastr dbastr; public DBastr GetDB(string DBtyle) { switch (DBtyle) { case "TIASS": dbastr = new TIASS(); break; case "TIDB": dbastr = new TIBD(); break; } return dbastr; } } }
然后定义计算所用的类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DBASES { interface Iation//定义计算接口 { double Calation(double a, double b); } class Add : Iation//加法 { public double Calation(double a, double b) { return a + b; } } class Sub : Iation//减法 { public double Calation(double a, double b) { return a - b; } } class Mul : Iation//乘法 { public double Calation(double a, double b) { return a * b; } } class Div : Iation//除法 { public double Calation(double a, double b) { if (b == 0) { throw new Exception("除数不能为零!"); } else { return a / b; } } } class Factionsss { private Iation clation; public Factionsss(string operation) { switch (operation) { case "+": clation = new Add(); break; case "-": clation = new Sub(); break; case "*": clation = new Mul(); break; case "/": clation = new Div(); break; } } public double cal(double a, double b) { return clation.Calation(a, b); } } }
from1代码
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 DBASES { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Faction faction = new Faction(); private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked == true) { label2.Text = "你选择了TIASS数据库!"; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked == true) { label2.Text = "你选择了TIDB数据库!"; } } private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked == true) { int a = faction.GetDB("TIASS").Insert(textBox1.Text, comboBox1.Text, textBox2.Text); if (a > 0) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } textBox1.Clear(); textBox2.Clear(); } else if (radioButton2.Checked == false) { MessageBox.Show("请选择你所要链接的数据库!"); } else { int b = faction.GetDB("TIDB").Insert(textBox1.Text, comboBox1.Text, textBox2.Text); if (b > 0) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("保存失败!"); } textBox1.Clear(); textBox2.Clear(); } } private void button2_Click(object sender, EventArgs e) { if (radioButton1.Checked == true) { int c = faction.GetDB("TIASS").Delect(); if (c > 0) { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } else if (radioButton2.Checked == true) { int d = faction.GetDB("TIDB").Delect(); if (d > 0) { MessageBox.Show("删除成功!"); } else { MessageBox.Show("删除失败!"); } } else { MessageBox.Show("请选择你所要删除的数据!"); } } private void button3_Click(object sender, EventArgs e) { Form2 fra = new Form2(); fra.ShowDialog(); } private void Form1_Load(object sender, EventArgs e) { radioButton1.Checked = true; } } }
from1设计
from2代码:
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 DBASES { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private int i = 0; Faction fation = new Faction(); private void Form2_Load(object sender, EventArgs e) { radioButton1.Checked = true; i = 0; Read("TIASS"); } public void Read(string DB) { DataTable dt = fation.GetDB(DB).Gettable(); if (i <= dt.Rows.Count - 1) { textBox1.Text = dt.Rows[i][0].ToString().Trim(); label1.Text = dt.Rows[i][1].ToString().Trim(); textBox2.Text = dt.Rows[i][2].ToString().Trim(); i++; } else { MessageBox.Show("本数据库题你做完了!"); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { i = 0; Read("TIDB"); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { double a = Convert.ToDouble(textBox1.Text); double b = Convert.ToDouble(textBox2.Text); string oper = label1.Text; Factionsss fas = new Factionsss(oper); double answer = fas.cal(a, b); if (textBox3.Text == answer.ToString()) { MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); } textBox3.Clear(); if (radioButton1.Checked == true) { Read("TIASS"); } else { Read("TIDB"); } } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { i = 0; Read("TIASS"); } } }
from2设计
测试
TIASS数据库存题
TIDB数据库存题
TIASS数据库做题
TIDB数据库做题
多数据库的存题读题感觉很灵活,但是只能是表结构相同的情况下才能成功?
本来是想写一个可以两个数,三个数,四个数都可以选择的数据库存题读题。但是发现用这个方法无法实现!
时间: 2024-10-05 05:02:09