WPF+数据库+三层

1.计算类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Zwt
{
class Class1
{
}
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 == )
{
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);
} }
}

2,实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Zwt
{
class TIModel
{
string number1;
string number2;
string operation;
public string Number1
{
get
{
return number1;
}
set
{
number1 = value;
}
}
public string Number2
{
get
{
return number2;
}
set
{
number2 = value;
}
}
public string Operation
{
get
{
return operation;
}
set
{
operation = value;
}
} }
}

3.DBhelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace Zwt
{
class DBhelper
{
string constr = "Data Source=.;Initial Catalog=TIKU;Integrated Security=True";
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
public void dbcon()
{
try
{
conn = new SqlConnection(constr);
}
finally
{ } }
public void OPen()
{
conn.Open();
}
public void Close()
{
conn.Close();
}
public int execSql(string safeSql, params SqlParameter[] values)//增删
{
int result;
conn = new SqlConnection(constr);
cmd = new SqlCommand(safeSql, conn);
OPen();
if (values != null)
{
cmd.Parameters.AddRange(values);
}
try
{
result = cmd.ExecuteNonQuery();
}
finally
{
Close(); }
return result; }
public DataSet execDataset(string safeSql, params SqlParameter[] values)//读
{
conn = new SqlConnection(constr);
cmd = new SqlCommand(safeSql, conn);
if (values != null)
{
cmd.Parameters.AddRange(values);
}
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
} }
}

4,数据访问层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Zwt
{
class TIDAL
{
public int InsterTI(TIModel Ti)
{
string sql = "insert into TI(number1,operation,number2) values (@number1,@operation,@number2)";
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@number1",Ti.Number1),
new SqlParameter("@operation",Ti.Operation),
new SqlParameter("@number2",Ti.Number2),
};
DBhelper helper = new DBhelper();
return helper.execSql(sql, para);
}
public DataSet Read()
{
string sql = "select number1,operation,number2 from TI";
DBhelper helper = new DBhelper();
return helper.execDataset(sql); }
public int DeleteTI()
{
string sql = "delete from TI ";
SqlParameter[] para = new SqlParameter[] { };
DBhelper helper = new DBhelper();
return helper.execSql(sql, para); }
}
}

5,业务逻辑层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace Zwt
{
class TIBLL
{
TIDAL TI1 = new TIDAL();
public int InsertTi(TIModel Ti1)
{
return TI1.InsterTI(Ti1);
}
public int Delect()
{
return TI1.DeleteTI();
}
public DataSet Read()
{
return TI1.Read();
}
}
}

UI层

Mainwidowd的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace Zwt
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
TIBLL tib = new TIBLL(); private void button1_Click(object sender, RoutedEventArgs e)
{
TIModel Tim = new TIModel();
Tim.Number1 =textBox1.Text;
Tim.Number2 = textBox2.Text;
Tim.Operation = comboBox1.Text;
int a = tib.InsertTi(Tim);
if (a > )
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!");
}
textBox1.Clear();
textBox2.Clear();
} private void button2_Click(object sender, RoutedEventArgs e)
{
int b = tib.Delect();
if (b > )
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
} private void button3_Click(object sender, RoutedEventArgs e)
{
Window1 win = new Window1();
win.ShowDialog();
} private void Window_Loaded(object sender, RoutedEventArgs e)
{ }
}
}

window1的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data; namespace Zwt
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
TIBLL TIll = new TIBLL();
int i = ;
private void Window_Loaded(object sender, RoutedEventArgs e)
{ DataSet ds= TIll.Read();
DataTable dt = ds.Tables[];
textBox1.Text = dt.Rows[][].ToString().Trim();
textBox2.Text = dt.Rows[][].ToString().Trim();
label1.Content = dt.Rows[][].ToString().Trim(); } private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
double a=Convert.ToDouble(textBox1.Text);
double b=Convert.ToDouble(textBox2.Text);
Factionsss fas = new Factionsss(label1.Content.ToString());
double aswer = fas.cal(a, b);
if (aswer.ToString() == textBox3.Text)
{
MessageBox.Show("回答正确!");
}
else
{
MessageBox.Show("回答错误!");
}
textBox3.Clear();
read(); }
private void read()
{
DataSet ds = TIll.Read();
DataTable dt = ds.Tables[];
textBox1.Text = dt.Rows[i][].ToString().Trim();
textBox2.Text = dt.Rows[i][].ToString().Trim();
label1.Content = dt.Rows[i][].ToString().Trim(); }
}
}
上一篇:Kali 2017 使用SSH进行远程登录 设置 ssh 开机自启动


下一篇:linux下安装oracle及weblogic