C#连接MySQL数据库 制作股票交易模拟程序
运行效果MySQL数据库的使用官网下载安装即可,安装的时候选择这两个安装完打开是这样使用方式这样可以查看表中列的属性可以在表中直接修改数据,点击apply,自动生成sql语句。一些sql笔记cd C:\Program Files\MySQL\MySQL Server 8.0\binshow global variables like "%datadir%";my...
·
运行效果
MySQL数据库的使用
官网下载安装即可,安装的时候选择这两个
安装完打开是这样
使用方式
这样可以查看表中列的属性
可以在表中直接修改数据,点击apply,自动生成sql语句。
一些sql笔记
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
show global variables like "%datadir%";
mysql –uroot –p111111
#插入数据
INSERT INTO `customer`(`CNAME`, `CAGE`, `CSEX`, `CID`, `CPHONE`, `CPASSWORD`, `CEMAIL`)
VALUES
("ceshi1", '3', '女', '100001', '100002', '100002', '100000');#可以单引号,也可以双引号
#插入数据
INSERT INTO `itravelin`.`customer` (`CNAME`, `CAGE`, `CSEX`, `CID`, `CPHONE`, `CPASSWORD`, `CEMAIL`)
VALUES
('ceshi1', '3', '女', '100000', '100000', '100000', '100000');
#查找
SELECT CNAME FROM itravelin.customer;#指定要查找的内容
SELECT * FROM itravelin.customer;#星号显示所有列
SELECT * FROM itravelin.customer where CSEX="女";
#添加列:在一个已经建好的表中添加一列,这一列在表的最后一列位置
alter table `itravelin`.`customer` add column BANANCE varchar(20) not null;
alter table `itravelin`.`customer` add column STOCK0 varchar(20) not null;
alter table `itravelin`.`customer` add column STOCK1 varchar(20) not null;
alter table `itravelin`.`customer` add column STOCK2 varchar(20) not null;
alter table `itravelin`.`customer` add column STOCK3 varchar(20) not null;
alter table `itravelin`.`customer` add column STOCK4 varchar(20) not null;
#添加列:在一个已经建好的表中添加一列,这一列在表的指定列之后
alter table `itravelin`.`customer` add column BANANCE_BF varchar(20) not null after `CEMAIL`;
#修改列名
alter table `itravelin`.`customer` change BANANCE BALANCE varchar(20);
alter table `itravelin`.`customer` change BALANCE_BF TOTALVALUE varchar(20);
CHAR_LENGTH
DELETE FROM `itravelin`.`customer` WHERE (length(`CPHONE`)<11);
select * from `itravelin`.`customer` where length(`CPHONE`)<11;
#按照条件,修改表中数据
UPDATE `itravelin`.`customer` SET `BALANCE` = '1000000' WHERE (`BALANCE` != "1000000");
UPDATE `itravelin`.`customer` SET `STOCK0` = '0' WHERE (`STOCK0` != '0' );
UPDATE `itravelin`.`customer` SET `STOCK1` = '0' WHERE (`STOCK1` != '0' );
UPDATE `itravelin`.`customer` SET `STOCK2` = '0' WHERE (`STOCK2` != '0' );
UPDATE `itravelin`.`customer` SET `STOCK3` = '0' WHERE (`STOCK3` != '0' );
UPDATE `itravelin`.`customer` SET `STOCK4` = '0' WHERE (`STOCK4` != '0' );
#创建一个表
CREATE TABLE IF NOT EXISTS STOCK(CNAME varchar(20) not null,PRICE varchar(20) not null,PRIMARY key(PRICE))engine = InnoDB default CHARSET=utf8;
#修改数据
UPDATE `itravelin`.`stock` SET `PRICE` = '16' WHERE (`PRICE` = '14');
#移动列
alter table customer modify STOCK4 varchar(20) after STOCK3;
另外,关于为什么需要添加环境变量:
1、计算机在执行命令的时候是在环境变量找对应的命令的位置的。如果不正确设置环境变量就不能正确使用相应的命令
2、比如说你要执行 java 命令,你不设置环境变量path包括你的jdk安装路径,那系统去哪找你的java.exe文件。
如果执行某个命令,系统无法在当前文件夹里找到对应的.exe,那么系统就会去path包含的路径找挨个找看是否能知道对应的.exe,一旦找到第一个对应的.exe就运行命令,其他的路径下就不找了。如果找不到你就会看到“系统找不到某某命令”的提示。
其他的环境变量也一样的用途,只不过是用来存储一些信息用的,这些信息可以被系统使用,也可以被你的应用程序使用
在C#中要添加如下引用:
主要窗体及代码
login
using MySql.Data.MySqlClient;
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;
namespace traveling
{
public partial class login : Form
{
public static string PHONENUM;
//update info
public void updateData(string phonenum)
{
int totalValue; //your stock worth + money left
int balance; //money left
double profitRate;
int[] holdBounds = new int[5];
textBox1.Text = phonenum;
try
{
//create database connection
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open database
con.Open();
//sql
string sql = "select TOTALVALUE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
MySqlCommand cmd = new MySqlCommand(sql, con);
totalValue = Convert.ToInt32(cmd.ExecuteScalar());
textBox2.Text = Convert.ToString(totalValue);
sql = "select BALANCE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
balance = Convert.ToInt32(cmd.ExecuteScalar());
textBox3.Text = Convert.ToString(balance);
profitRate = ((double)totalValue - 1000000) / 1000000;
textBox4.Text = Convert.ToString(profitRate);
for (int i = 0; i < 5; i++)
{
sql = "select STOCK" + i + " from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
holdBounds[i] = Convert.ToInt32(cmd.ExecuteScalar());
}
textBox5.Text = Convert.ToString(holdBounds[0]);
textBox6.Text = Convert.ToString(holdBounds[1]);
textBox7.Text = Convert.ToString(holdBounds[2]);
textBox8.Text = Convert.ToString(holdBounds[3]);
textBox9.Text = Convert.ToString(holdBounds[4]);
Application.DoEvents();
}
catch (Exception ex)
{
MessageBox.Show(ex + "发生异常");
throw;
}
}
public login()
{
InitializeComponent();
}
//log in
private void btnlogin_Click(object sender, EventArgs e)
{
//get input
string userName = this.usernametxt.Text;
string userPassword = this.passwordtxt.Text;
//input legal
if (FunctionDefine.Isempty(userName))
{
MessageBox.Show("用户名不能为空!");
return;
}
if (FunctionDefine.Isempty(userPassword))
{
MessageBox.Show("密码不能为空!");
}
//traveller login
if (this.radiocustomer.Checked)
{
Customer customer = new Customer(userPassword);
customer.Setcphone(userName);
customer.Setcpassword(userPassword);
try
{
//connect
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open
con.Open();
//sql
string sql = "select count(*) from itravelin.customer where CPHONE = '" + userName + "' and cpassword = '" + userPassword + "'";
MySqlCommand com = new MySqlCommand(sql, con);
//check psw
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
MessageBox.Show("登录成功!");
//update info
label15.ForeColor = Color.LimeGreen;
label15.Text = "状态:已登录";
updateData(userName);
Application.DoEvents();
//new winform
//Main_customer main_customer = new Main_customer();
//this.DialogResult = DialogResult.OK;
//main_customer.ShowDialog();
//this.Dispose();
//this.Close();
//Close();
}
//phonenum or password wrong
else
{
MessageBox.Show("用户名或密码错误!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
}
}
//admin login
else if (this.radioadmin.Checked)
{
Administrator administrator = new Administrator(userPassword);
administrator.Setusername(userName);
administrator.Setpassword(userPassword);
try
{
//connect
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open
con.Open();
//sql
string sql = "select count(*) from itravelin.manager where username = '" + userName + "' and password = '" + userPassword + "'";
MySqlCommand com = new MySqlCommand(sql, con);
//check psw
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
MessageBox.Show("登录成功!");
//new winform
Main_administrator main_administrator = new Main_administrator();
//this.DialogResult = DialogResult.OK;
main_administrator.ShowDialog();
//this.Dispose();
//this.Close();
}
//phonenum or password wrong
else
{
MessageBox.Show("用户名或密码错误!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + "异常");
}
}
else MessageBox.Show("请选择您的登录方式!");
}
private void btnregister_Click(object sender, EventArgs e)
{
register selectregister = new register();
selectregister.Show();
}
//startSimulation
private void button1_Click(object sender, EventArgs e)
{
if (label15.Text == "状态:未登录")
{
MessageBox.Show("请先登录!");
return;
}
startSimulation mysimulation = new startSimulation();
PHONENUM = textBox1.Text;
mysimulation.ShowDialog();
button3_Click(null, null);
}
//save and close
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
//button_update data
private void button3_Click(object sender, EventArgs e)
{
string phonenum = textBox1.Text;
int totalValue; //your stock worth + money left
int balance; //money left
double profitRate;
int[] holdBounds = new int[5];
try
{
//create database connection
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open database
con.Open();
//sql
string sql = "select TOTALVALUE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
MySqlCommand cmd = new MySqlCommand(sql, con);
totalValue = Convert.ToInt32(cmd.ExecuteScalar());
textBox2.Text = Convert.ToString(totalValue);
sql = "select BALANCE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
balance = Convert.ToInt32(cmd.ExecuteScalar());
textBox3.Text = Convert.ToString(balance);
profitRate = ((double)totalValue - 1000000) / 1000000;
textBox4.Text = Convert.ToString(profitRate);
for (int i = 0; i < 5; i++)
{
sql = "select STOCK" + i + " from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
holdBounds[i] = Convert.ToInt32(cmd.ExecuteScalar());
}
textBox5.Text = Convert.ToString(holdBounds[0]);
textBox6.Text = Convert.ToString(holdBounds[1]);
textBox7.Text = Convert.ToString(holdBounds[2]);
textBox8.Text = Convert.ToString(holdBounds[3]);
textBox9.Text = Convert.ToString(holdBounds[4]);
Application.DoEvents();
}
catch (Exception ex)
{
MessageBox.Show(ex + "发生异常");
throw;
}
}
}
}
startSimulation
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;
using System.Threading;
namespace traveling
{
public partial class startSimulation : Form
{
//update info
public void updateData()
{
string phonenum = login.PHONENUM;
int totalValue; //your stock worth + money left
int balance; //money left
int[] holdBounds = new int[5];
try
{
//create database connection
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open database
con.Open();
//sql
string sql = "select TOTALVALUE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
MySqlCommand cmd = new MySqlCommand(sql, con);
totalValue = Convert.ToInt32(cmd.ExecuteScalar());
textBox29.Text = Convert.ToString(totalValue);
sql = "select BALANCE from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
balance = Convert.ToInt32(cmd.ExecuteScalar());
textBox30.Text = Convert.ToString(balance);
for (int i = 0; i < 5; i++)
{
sql = "select STOCK" + i + " from itravelin.customer where CPHONE=\"" + phonenum + "\";";
cmd = new MySqlCommand(sql, con);
holdBounds[i] = Convert.ToInt32(cmd.ExecuteScalar());
}
tbstock0.Text = Convert.ToString(holdBounds[0]);
tbstock1.Text = Convert.ToString(holdBounds[1]);
tbstock2.Text = Convert.ToString(holdBounds[2]);
tbstock3.Text = Convert.ToString(holdBounds[3]);
tbstock4.Text = Convert.ToString(holdBounds[4]);
Application.DoEvents();
}
catch (Exception ex)
{
MessageBox.Show(ex + "发生异常");
throw;
}
}
public startSimulation()
{
InitializeComponent();
}
//start simulation
private void button5_Click(object sender, EventArgs e)
{
//initialize price from database read
int[] priceStock = new int[5];
for (int i = 0; i < 5; i++)
{
try
{
//create database connection
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open database
con.Open();
//sql
string sql = "select PRICE from itravelin.stock where CNAME=\"stock" + i + "\";";
MySqlCommand cmd = new MySqlCommand(sql, con);
//get result
//MySqlDataReader reader = cmd.ExecuteReader();
//reader.Close();//close then open
priceStock[i] = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception ex)
{
MessageBox.Show(ex + "发生异常");
throw;
}
}
//show price in textbox
textBox1.Text = priceStock[0].ToString();
textBox2.Text = priceStock[1].ToString();
textBox3.Text = priceStock[2].ToString();
textBox4.Text = priceStock[3].ToString();
textBox5.Text = priceStock[4].ToString();
//1
textBox18.Text = textBox1.Text;
textBox23.Text = textBox1.Text;
//2
textBox17.Text = textBox2.Text;
textBox22.Text = textBox2.Text;
//3
textBox16.Text = textBox3.Text;
textBox21.Text = textBox3.Text;
//4
textBox15.Text = textBox4.Text;
textBox20.Text = textBox4.Text;
//5
textBox14.Text = textBox5.Text;
textBox19.Text = textBox5.Text;
Application.DoEvents();
//timer
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Start();
timer.Elapsed += new System.Timers.ElapsedEventHandler(mytimer);
updateData();
}
private void mytimer(object sender, EventArgs e)
{
//get price from textbox
int[] priceStock = new int[5];
priceStock[0] = int.Parse(textBox1.Text);
priceStock[1] = int.Parse(textBox2.Text);
priceStock[2] = int.Parse(textBox3.Text);
priceStock[3] = int.Parse(textBox4.Text);
priceStock[4] = int.Parse(textBox5.Text);
//random
Random rd = new Random();
//wait 1 sec
Application.DoEvents();
for (int i = 0; i < 5; i++)
{
if (priceStock[i] > 200) priceStock[i] += rd.Next(-10, 4);//avoid price too high
else priceStock[i] += rd.Next(-3, 4);
if (priceStock[i] < 1) priceStock[i] = 1;
}
this.Invoke(new Action(() =>
{
textBox1.Text = priceStock[0].ToString();
textBox2.Text = priceStock[1].ToString();
textBox3.Text = priceStock[2].ToString();
textBox4.Text = priceStock[3].ToString();
textBox5.Text = priceStock[4].ToString();
textBox29.Text = Convert.ToString(int.Parse(textBox30.Text)+priceStock[0] * int.Parse(tbstock0.Text) + priceStock[1] * int.Parse(tbstock1.Text) + priceStock[2] * int.Parse(tbstock2.Text) + priceStock[3] * int.Parse(tbstock3.Text) + priceStock[4] * int.Parse(tbstock4.Text));
//find max
//1
if (int.Parse(textBox1.Text) > int.Parse(textBox18.Text)) textBox18.Text = textBox1.Text;
if (int.Parse(textBox1.Text) < int.Parse(textBox23.Text)) textBox23.Text = textBox1.Text;
//2
if (int.Parse(textBox2.Text) > int.Parse(textBox17.Text)) textBox17.Text = textBox2.Text;
if (int.Parse(textBox2.Text) < int.Parse(textBox22.Text)) textBox22.Text = textBox2.Text;
//3
if (int.Parse(textBox3.Text) > int.Parse(textBox16.Text)) textBox16.Text = textBox3.Text;
if (int.Parse(textBox3.Text) < int.Parse(textBox21.Text)) textBox21.Text = textBox3.Text;
//4
if (int.Parse(textBox4.Text) > int.Parse(textBox15.Text)) textBox15.Text = textBox4.Text;
if (int.Parse(textBox4.Text) < int.Parse(textBox20.Text)) textBox20.Text = textBox4.Text;
//5
if (int.Parse(textBox5.Text) > int.Parse(textBox14.Text)) textBox14.Text = textBox5.Text;
if (int.Parse(textBox5.Text) < int.Parse(textBox19.Text)) textBox19.Text = textBox5.Text;
Application.DoEvents();
//check trade
if (WANTBUY == true)
{
//if can buy
if (priceStock[STOCKBUYNUM] <= BUYPRICE)
{
//update data
switch (STOCKBUYNUM)
{
case 0: tbstock0.Text = Convert.ToString(int.Parse(tbstock0.Text) + BUYNUM); logger.Text += "交易成功,股票序号0,成交价格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text += "\r\n"; break;
case 1: tbstock1.Text = Convert.ToString(int.Parse(tbstock1.Text) + BUYNUM); logger.Text += "交易成功,股票序号1,成交价格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text += "\r\n"; break;
case 2: tbstock2.Text = Convert.ToString(int.Parse(tbstock2.Text) + BUYNUM); logger.Text += "交易成功,股票序号2,成交价格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text += "\r\n"; break;
case 3: tbstock3.Text = Convert.ToString(int.Parse(tbstock3.Text) + BUYNUM); logger.Text += "交易成功,股票序号3,成交价格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text += "\r\n"; break;
case 4: tbstock4.Text = Convert.ToString(int.Parse(tbstock4.Text) + BUYNUM); logger.Text += "交易成功,股票序号4,成交价格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text += "\r\n"; break;
}
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) - BUYNUM * BUYPRICE);
Application.DoEvents();
WANTBUY = false;
}
}
else if (WANTSELL == true)
{
//if can sell
if (priceStock[STOCKSELLNUM] >= SELLPRICE)
{
//update data
switch (STOCKSELLNUM)
{
case 0:
if (int.Parse(tbstock0.Text) - SELLNUM >= 0)
{
tbstock0.Text = Convert.ToString(int.Parse(tbstock0.Text) - SELLNUM);
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) + SELLNUM * SELLPRICE);
logger.Text += "交易成功,股票序号0,成交价格:";
logger.Text += Convert.ToString(SELLPRICE);
logger.Text += "\r\n";
WANTSELL = false;
}
else
{
logger.Text += "你的持仓不足,无法卖出!";
//debug
logger.Text += tbstock0.Text;
logger.Text += "-";
logger.Text += SELLNUM;
logger.Text += "\r\n";
}
break;
case 1:
if (int.Parse(tbstock1.Text) - SELLNUM >= 0)
{
tbstock1.Text = Convert.ToString(int.Parse(tbstock1.Text) - SELLNUM);
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) + SELLNUM * SELLPRICE);
logger.Text += "交易成功,股票序号1,成交价格:";
logger.Text += Convert.ToString(SELLPRICE);
logger.Text += "\r\n";
WANTSELL = false;
}
else
{
logger.Text += "你的持仓不足,无法卖出!";
//debug
logger.Text += tbstock1.Text;
logger.Text += "-";
logger.Text += SELLNUM;
logger.Text += "\r\n";
WANTSELL = false;
}
break;
case 2:
if (int.Parse(tbstock2.Text) - SELLNUM >= 0)
{
tbstock2.Text = Convert.ToString(int.Parse(tbstock2.Text) - SELLNUM);
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) + SELLNUM * SELLPRICE);
logger.Text += "交易成功,股票序号2,成交价格:";
logger.Text += Convert.ToString(SELLPRICE);
logger.Text += "\r\n";
WANTSELL = false;
}
else
{
logger.Text += "你的持仓不足,无法卖出!";
//debug
logger.Text += tbstock2.Text;
logger.Text += "-";
logger.Text += SELLNUM;
logger.Text += "\r\n";
WANTSELL = false;
}
break;
case 3:
if (int.Parse(tbstock3.Text) - SELLNUM >= 0)
{
tbstock3.Text = Convert.ToString(int.Parse(tbstock3.Text) - SELLNUM);
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) + SELLNUM * SELLPRICE);
logger.Text += "交易成功,股票序号3,成交价格:";
logger.Text += Convert.ToString(SELLPRICE);
logger.Text += "\r\n";
WANTSELL = false;
}
else
{
logger.Text += "你的持仓不足,无法卖出!";
//debug
logger.Text += tbstock3.Text;
logger.Text += "-";
logger.Text += SELLNUM;
logger.Text += "\r\n";
WANTSELL = false;
}
break;
case 4:
if (int.Parse(tbstock4.Text) - SELLNUM >= 0)
{
tbstock4.Text = Convert.ToString(int.Parse(tbstock4.Text) - SELLNUM);
textBox30.Text = Convert.ToString(int.Parse(textBox30.Text) + SELLNUM * SELLPRICE);
logger.Text += "交易成功,股票序号4,成交价格:";
logger.Text += Convert.ToString(SELLPRICE);
logger.Text += "\r\n";
WANTSELL = false;
}
else
{
logger.Text += "你的持仓不足,无法卖出!";
//debug
logger.Text += tbstock4.Text;
logger.Text += "-";
logger.Text += SELLNUM;
logger.Text += "\r\n";
WANTSELL = false;
}
break;
}
Application.DoEvents();
}
}
}));
}
//buy_choose
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "股票A": textBox6.Text = "股票A"; break;
case "股票B": textBox6.Text = "股票B"; break;
case "股票C": textBox6.Text = "股票C"; break;
case "股票D": textBox6.Text = "股票D"; break;
case "股票E": textBox6.Text = "股票E"; break;
}
Application.DoEvents();
}
//sell_choose
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox2.Text)
{
case "股票A": textBox13.Text = "股票A"; break;
case "股票B": textBox13.Text = "股票B"; break;
case "股票C": textBox13.Text = "股票C"; break;
case "股票D": textBox13.Text = "股票D"; break;
case "股票E": textBox13.Text = "股票E"; break;
}
//calcute how many stock you can sell
switch (comboBox2.Text)
{
case "股票A": textBox12.Text = tbstock0.Text; break;
case "股票B": textBox12.Text = tbstock1.Text; break;
case "股票C": textBox12.Text = tbstock2.Text; break;
case "股票D": textBox12.Text = tbstock3.Text; break;
case "股票E": textBox12.Text = tbstock4.Text; break;
}
Application.DoEvents();
}
public static bool WANTBUY = false;
public static bool WANTSELL = false;
public static int STOCKBUYNUM; //the num of stock
public static int BUYPRICE; //your price
public static int BUYNUM; //how much you want to buy
public static int STOCKSELLNUM;
public static int SELLPRICE;
public static int SELLNUM;
//buy_click
private void button1_Click(object sender, EventArgs e)
{
WANTBUY = true;
BUYNUM = int.Parse(textBox8.Text);
BUYPRICE = int.Parse(textBox9.Text);
switch (comboBox1.Text)
{
case "股票A": STOCKBUYNUM = 0; break;
case "股票B": STOCKBUYNUM = 1; break;
case "股票C": STOCKBUYNUM = 2; break;
case "股票D": STOCKBUYNUM = 3; break;
case "股票E": STOCKBUYNUM = 4; break;
}
MessageBox.Show("成功提交买入委托!");
}
//sell_click
private void button2_Click(object sender, EventArgs e)
{
WANTSELL = true;
SELLNUM = int.Parse(textBox11.Text);
SELLPRICE = int.Parse(textBox10.Text);
switch (comboBox2.Text)
{
case "股票A": STOCKSELLNUM = 0; break;
case "股票B": STOCKSELLNUM = 1; break;
case "股票C": STOCKSELLNUM = 2; break;
case "股票D": STOCKSELLNUM = 3; break;
case "股票E": STOCKSELLNUM = 4; break;
}
MessageBox.Show("成功提交卖出委托!");
}
//save and close
private void button4_Click(object sender, EventArgs e)
{
int[] priceStock = new int[5];
priceStock[0] = int.Parse(textBox1.Text);
priceStock[1] = int.Parse(textBox2.Text);
priceStock[2] = int.Parse(textBox3.Text);
priceStock[3] = int.Parse(textBox4.Text);
priceStock[4] = int.Parse(textBox5.Text);
int[] stockHold = new int[5];
stockHold[0] = int.Parse(tbstock0.Text);
stockHold[1] = int.Parse(tbstock1.Text);
stockHold[2] = int.Parse(tbstock2.Text);
stockHold[3] = int.Parse(tbstock3.Text);
stockHold[4] = int.Parse(tbstock4.Text);
//save
for (int i = 0; i < 5; i++)
{
try
{
//create database connection
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";
MySqlConnection con = new MySqlConnection(strcon);
//open database
con.Open();
//sql
MySqlCommand cmd;
cmd = con.CreateCommand();
cmd.Parameters.AddWithValue("@stockHold_i", stockHold[i]);
cmd.Parameters.AddWithValue("@phonenum", login.PHONENUM);
cmd.CommandText = "UPDATE `itravelin`.`customer` SET `STOCK" + i + "` = @stockHold_i WHERE (`CPHONE` = @phonenum);";
cmd.ExecuteNonQuery();
cmd.Parameters.AddWithValue("@stock_i", priceStock[i]);
cmd.CommandText = "UPDATE `itravelin`.`stock` SET `PRICE` = @stock_i where CNAME=\"stock" + i + "\";";
cmd.ExecuteNonQuery();
cmd.Parameters.AddWithValue("@balance", textBox30.Text);
cmd.CommandText = "UPDATE `itravelin`.`customer` SET `BALANCE` = @balance WHERE (`CPHONE` = @phonenum);";
cmd.ExecuteNonQuery();
cmd.Parameters.AddWithValue("@totalValue", textBox29.Text);
cmd.CommandText = "UPDATE `itravelin`.`customer` SET `TOTALVALUE` = @totalValue WHERE (`CPHONE` = @phonenum);";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex + "发生异常");
throw;
}
}
//close
this.Close();
}
//calcute how many stock you can buy
private void textBox9_TextChanged(object sender, EventArgs e)
{
string str = textBox9.Text;//avoid intParseError caused by empty textbox9
int balance;
int price=-1;
if (str == "") balance = 0;
else price = int.Parse(textBox9.Text);
balance = int.Parse(textBox30.Text);
int canBuy = balance / price;
textBox7.Text = Convert.ToString(canBuy);
}
}
}
register(注册过程)
using MySql.Data.MySqlClient;
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;
/* 电话号码不能重复
* 性别有位数限制
* 年龄有位数限制
*/
namespace traveling
{
public partial class register : Form
{
public register()
{
InitializeComponent();
}
private void okbtn_Click(object sender, EventArgs e)
{
Customer customer = new Customer();
//Console.WriteLine("1");
string name = Convert.ToString(nametxt.Text);
string password = Convert.ToString(passwordtxt.Text);
string age = Convert.ToString(agetxt.Text);
string id = Convert.ToString(idtxt.Text);
string sex = Convert.ToString(sextxt.Text);
string phone = Convert.ToString(phonetxt.Text);
string email = Convert.ToString(emailtxt.Text);
string totalvalue = Convert.ToString(1000000); //initial 1000000yuan
string balance = Convert.ToString(1000000); //initial 1000000yuan
if (FunctionDefine.Isempty(name))
{
MessageBox.Show("姓名不能为空!");
return;
}
if (FunctionDefine.Isempty(password))
{
MessageBox.Show("密码不能为空!");
return;
}
if (FunctionDefine.Isempty(id))
{
MessageBox.Show("身份证号不能为空!");
return;
}
if (FunctionDefine.Isempty(phone))
{
MessageBox.Show("手机号不能为空!");
return;
}
string strcon = "server=localhost;port=3306;database=itravelin;user=root;password=g66666666;";
MySqlConnection con = new MySqlConnection(strcon);
MySqlCommand cmd;
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "select * from itravelin.customer where cphone = " + phone + ";";//find this phonenum
string debugger = (string)cmd.ExecuteScalar();//if not find, return null
if (debugger != null) MessageBox.Show("此手机号已被注册!");
else
{
try
{
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@sex", sex);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@phone", phone);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@totalvalue", totalvalue);
cmd.Parameters.AddWithValue("@balance", balance);
cmd.CommandText = "insert into itravelin.customer values(@name,@age,@sex,@id,@phone,@password,@email,@totalvalue,@balance,\"0\",\"0\",\"0\",\"0\",\"0\");";//和上面七行无关顺序
int ret = cmd.ExecuteNonQuery();//debug execute two times
if (ret > 0) MessageBox.Show("注册成功!");
else MessageBox.Show("注册失败!");
}
catch (Exception)
{
MessageBox.Show("出现异常");
throw;
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}
}
更多推荐
已为社区贡献33条内容
所有评论(0)