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;
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 voidupdateData(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 voidbtnlogin_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 loginelseif(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 voidbtnregister_Click(object sender, EventArgs e){register selectregister = new register();selectregister.Show();}//startSimulationprivate voidbutton1_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 voidbutton2_Click(object sender, EventArgs e){this.Close();}//button_update dataprivate voidbutton3_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 voidupdateData(){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 voidbutton5_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 voidmytimer(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){case0: tbstock0.Text = Convert.ToString(int.Parse(tbstock0.Text)+ BUYNUM); logger.Text +="交易成功,股票序號0,成交價格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text +="\r\n";break;case1: tbstock1.Text = Convert.ToString(int.Parse(tbstock1.Text)+ BUYNUM); logger.Text +="交易成功,股票序號1,成交價格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text +="\r\n";break;case2: tbstock2.Text = Convert.ToString(int.Parse(tbstock2.Text)+ BUYNUM); logger.Text +="交易成功,股票序號2,成交價格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text +="\r\n";break;case3: tbstock3.Text = Convert.ToString(int.Parse(tbstock3.Text)+ BUYNUM); logger.Text +="交易成功,股票序號3,成交價格:"; logger.Text += Convert.ToString(BUYPRICE); logger.Text +="\r\n";break;case4: 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;}}elseif(WANTSELL == true){//if can sellif(priceStock[STOCKSELLNUM]>= SELLPRICE){//update dataswitch(STOCKSELLNUM){case0: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;case1: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;case2: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;case3: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;case4: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 voidcomboBox1_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 voidcomboBox2_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 staticint STOCKBUYNUM;//the num of stockpublic staticint BUYPRICE;//your pricepublic staticint BUYNUM;//how much you want to buypublic staticint STOCKSELLNUM;public staticint SELLPRICE;public staticint SELLNUM;//buy_clickprivate voidbutton1_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 voidbutton2_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 voidbutton4_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 voidtextBox9_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 voidokbtn_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();}}}}}}