名字略高端,其实就是一个计算器,然后把得到的结果传送到了数据库里
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace MySQLCalculate { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCalculate_Click(object sender, EventArgs e) { int a, b, c; a = Convert.ToInt32(txtNum1.Text); b = Convert.ToInt32(txtNum2.Text); c = 0; string d = txtSign.Text; if (d == "+") { c = a + b; } if (d == "-") { c = a - b; } if (d == "*") { c = a * b; } if (d == "/") { c = a / b; } string strcn = "Server=127.0.0.1;User ID=root;Password=mysql;Database=test;CharSet=gbk;"; MySqlConnection cn = new MySqlConnection(strcn);//实例化链接 cn.Open();//开启连接 MySqlCommand cmd = cn.CreateCommand(); cmd.CommandText = "insert into Calculate(num1,sign,num2,outcome) values(‘" + a + "‘,‘" + d + "‘,‘" + b + "‘,‘" + c + "‘)"; cmd.CommandType = CommandType.Text; int i = cmd.ExecuteNonQuery(); cn.Close();//关闭连接 } } }
输入显示
数据库显示
原文:http://www.cnblogs.com/CXYHY/p/5240355.html