运行效果


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_LENGTHDELETE 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 infopublic void updateData(string phonenum){int totalValue;     //your stock worth + money leftint balance;       //money leftdouble profitRate;int[] holdBounds = new int[5];textBox1.Text = phonenum;try{//create database connectionstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//open databasecon.Open();//sqlstring 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 inprivate void btnlogin_Click(object sender, EventArgs e){//get inputstring userName = this.usernametxt.Text;string userPassword = this.passwordtxt.Text;//input legalif (FunctionDefine.Isempty(userName)){MessageBox.Show("用户名不能为空!");return;}if (FunctionDefine.Isempty(userPassword)){MessageBox.Show("密码不能为空!");}//traveller loginif (this.radiocustomer.Checked){Customer customer = new Customer(userPassword);customer.Setcphone(userName);customer.Setcpassword(userPassword);try{//connectstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//opencon.Open();//sqlstring sql = "select count(*) from itravelin.customer where CPHONE = '" + userName + "' and cpassword = '" + userPassword + "'";MySqlCommand com = new MySqlCommand(sql, con);//check pswif (Convert.ToInt32(com.ExecuteScalar()) > 0){MessageBox.Show("登录成功!");//update infolabel15.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 wrongelse{MessageBox.Show("用户名或密码错误!");}}catch (Exception ex){MessageBox.Show(ex.Message.ToString() + "打开数据库失败");}}//admin loginelse if (this.radioadmin.Checked){Administrator administrator = new Administrator(userPassword);administrator.Setusername(userName);administrator.Setpassword(userPassword);try{//connectstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//opencon.Open();//sqlstring sql = "select count(*) from itravelin.manager where username = '" + userName + "' and password = '" + userPassword + "'";MySqlCommand com = new MySqlCommand(sql, con);//check pswif (Convert.ToInt32(com.ExecuteScalar()) > 0){MessageBox.Show("登录成功!");//new winformMain_administrator main_administrator = new Main_administrator();//this.DialogResult = DialogResult.OK;main_administrator.ShowDialog();//this.Dispose();//this.Close();}//phonenum or password wrongelse{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();}//startSimulationprivate 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 closeprivate void button2_Click(object sender, EventArgs e){this.Close();}//button_update dataprivate void button3_Click(object sender, EventArgs e){string phonenum = textBox1.Text;int totalValue;     //your stock worth + money leftint balance;       //money leftdouble profitRate;int[] holdBounds = new int[5];try{//create database connectionstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//open databasecon.Open();//sqlstring 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 infopublic void updateData(){string phonenum = login.PHONENUM;int totalValue;     //your stock worth + money leftint balance;       //money leftint[] holdBounds = new int[5];try{//create database connectionstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//open databasecon.Open();//sqlstring 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 simulationprivate void button5_Click(object sender, EventArgs e){//initialize price from database readint[] priceStock = new int[5];for (int i = 0; i < 5; i++){try{//create database connectionstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//open databasecon.Open();//sqlstring 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 openpriceStock[i] = Convert.ToInt32(cmd.ExecuteScalar());}catch (Exception ex){MessageBox.Show(ex + "发生异常");throw;}}//show price in textboxtextBox1.Text = priceStock[0].ToString();textBox2.Text = priceStock[1].ToString();textBox3.Text = priceStock[2].ToString();textBox4.Text = priceStock[3].ToString();textBox5.Text = priceStock[4].ToString();//1textBox18.Text = textBox1.Text;textBox23.Text = textBox1.Text;//2textBox17.Text = textBox2.Text;textBox22.Text = textBox2.Text;//3textBox16.Text = textBox3.Text;textBox21.Text = textBox3.Text;//4textBox15.Text = textBox4.Text;textBox20.Text = textBox4.Text;//5textBox14.Text = textBox5.Text;textBox19.Text = textBox5.Text;Application.DoEvents();//timerSystem.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 textboxint[] 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);//randomRandom rd = new Random();//wait 1 secApplication.DoEvents();for (int i = 0; i < 5; i++){if (priceStock[i] > 200) priceStock[i] += rd.Next(-10, 4);//avoid price too highelse 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//1if (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;//2if (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;//3if (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;//4if (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;//5if (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 tradeif (WANTBUY == true){//if can buyif (priceStock[STOCKBUYNUM] <= BUYPRICE){//update dataswitch (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 sellif (priceStock[STOCKSELLNUM] >= SELLPRICE){//update dataswitch (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 += "你的持仓不足,无法卖出!";//debuglogger.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 += "你的持仓不足,无法卖出!";//debuglogger.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 += "你的持仓不足,无法卖出!";//debuglogger.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 += "你的持仓不足,无法卖出!";//debuglogger.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 += "你的持仓不足,无法卖出!";//debuglogger.Text += tbstock4.Text;logger.Text += "-";logger.Text += SELLNUM;logger.Text += "\r\n";WANTSELL = false;}break;}Application.DoEvents();}}}));}//buy_chooseprivate 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_chooseprivate 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 sellswitch (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 stockpublic static int BUYPRICE;     //your pricepublic static int BUYNUM;       //how much you want to buypublic static int STOCKSELLNUM;public static int SELLPRICE;public static int SELLNUM;//buy_clickprivate 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_clickprivate 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 closeprivate 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);//savefor (int i = 0; i < 5; i++){try{//create database connectionstring strcon = "server=localhost;port=3306;database=itravelin;user=root;password=111111;";MySqlConnection con = new MySqlConnection(strcon);//open databasecon.Open();//sqlMySqlCommand 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;}}//closethis.Close();}//calcute how many stock you can buyprivate void textBox9_TextChanged(object sender, EventArgs e){string str = textBox9.Text;//avoid intParseError caused by empty textbox9int 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 1000000yuanstring balance = Convert.ToString(1000000);     //initial 1000000yuanif (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 phonenumstring debugger = (string)cmd.ExecuteScalar();//if not find, return nullif (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 timesif (ret > 0) MessageBox.Show("注册成功!");else MessageBox.Show("注册失败!");}catch (Exception){MessageBox.Show("出现异常");throw;}finally{if (con.State == ConnectionState.Open){con.Close();}}}}}
}

C#连接MySQL数据库 制作股票交易模拟程序相关推荐

  1. C语言ODBC连接MySQL数据库制作简易用户登录系统

    1.代码如下: #include "stdafx.h" #include <windows.h> #include <windowsx.h> #includ ...

  2. 使用SQLyog连接MySQL数据库

    [学习笔记]使用SQLyog连接MySQL数据库 一.使用SQLyog创建数据库用来管理学生信息 复制代码 1 #创建数据库student   2 DROP DATABASE IF EXISTS My ...

  3. QT五子棋游戏课设及源码(连接mysql数据库含打开并运行程序的教程)

    gdut大一下学期c++课设(得分:95) 实验报告及源码压缩包百度云下载: 链接:https://pan.baidu.com/s/1zO5ofMz09fiWihxCcZcFbg 提取码:ddav 首 ...

  4. java连接MySQL几种方法_Java连接MySQL数据库三种方法

    好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...

  5. .net连接mysql数据_.net连接MYSQL数据库的方法及示例!

    连接MYSQL数据库的方法及示例 方法一: 使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL 该组件为MYSQL为ADO.NET ...

  6. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  7. python用django连接mysql_三分钟了解Django如何连接Mysql数据库

    处理用户注册请求.Django连接MysqL数据库相关配置.数据库迁移命令: my_Dproject/app01/views.py    在views函数文件中添加register函数,来处理用户注册 ...

  8. eclipselink mysql_Eclipse连接MySQL数据库(傻瓜篇)

    Eclipse连接MySQL数据库(傻瓜篇) 本来不想写这么简单人文章,在百度上搜索我这个标题,完全符合标题的一大堆.但我按照那些文章捣鼓了很久,就是不行. 我的环境:MySQL:mysql-esse ...

  9. Linux JSP连接MySQL数据库

    Linux(Ubuntu平台)JSP通过JDBC连接MySQL数据库,与Windows平台类似,步骤如下: 下载 jdbc: mysql-connector-java-5.1.18.tar.gz 解压 ...

最新文章

  1. GoLang笔记-数组和切片,本质是就是长度不可变的可变的区别
  2. 元宇宙大比拼:苹果Apple, Facebook,微软,英伟达,iwemeta
  3. 简单计算机病毒黑屏,教大家一个黑屏小程序
  4. NSArray 与 NSMutableArray 的排序
  5. Spring - bean的lazy-init属性(懒加载)
  6. html绑定按键图片移动,如何使用JS实现用键盘控制图片移动呢?
  7. 【Java从入门到头秃专栏 】(一)学在Java语法之前
  8. 作者:王绍卿, 男, 中国人民大学信息学院博士生, CCF学生会员。
  9. Python入门学习笔记(7)
  10. 多用户用linux会很卡顿吗,新手学Linux系统,解决Linux系统卡顿的方法
  11. MapReduce分布式编程框架
  12. C3P0连接池的基本配置与使用
  13. 腾讯手机管家(pc版) for android,腾讯手机管家PC版for Android小技巧分享
  14. 开源 iOS 项目分类索引大全
  15. PHP操作redis详细讲解(转)
  16. redis-shiro session 共享subject中principal 为空
  17. C - char与wchar_t(TCHAR/WCHAR)之间的相互转换
  18. 巧用计算机方法,第四课 巧用计算器教案.doc
  19. python程序设计简明教程知识点_《Python 简明教程》读书笔记系列一 —— 基本语法...
  20. ProTICS包的介绍(根据生信技能树Jimmy老师分享的乳腺癌分子分型包资料整理)

热门文章

  1. 我国计算机网络事业发展,金标尺公考
  2. CanvasRenderingContext2D(渲染上下文对象)
  3. 两个大文件找出相同的一条记录
  4. EV3 直接命令 - 第 2 课 让你的 EV3 做点什么
  5. 趣谈设计模式 | 策略模式(Strategy):你还在使用冗长的if-else吗?
  6. Shell程序设计 | 文本处理工具 :正则表达式、grep、sed、awk
  7. Linux网络编程 | 多路复用I/O :select、poll、epoll、水平触发与边缘触发、惊群问题
  8. 课堂上传纸条如何防范中间人攻击?
  9. Go http client 连接池不复用的问题
  10. 短视频内容理解与生成技术在美团的创新实践