日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2024/2/28 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#连接MySQL数据库 制作股票交易模拟程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

運行效果


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数据库 制作股票交易模拟程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 中文字幕免费在线看线人 | 日本免费www| 亚洲最色网站 | 色射网| 一级特黄色 | 波多野结衣在线影院 | 伊人春色影院 | 日日草天天干 | 手机av片 | 久久亚洲精品小早川怜子 | 片多多在线观看 | 秘密基地动漫在线观看免费 | 成人a级网站 | 都市豪门艳霸淫美妇 | 成人在线高清 | 久久伊人成人 | 综合激情网 | 夜夜夜网站 | 午夜日韩视频 | 天天爽天天摸 | 久久黄色一级片 | 日本美女性生活视频 | 久久福利视频导航 | 欧美浪妇xxxx高跟鞋交 | а√天堂8资源在线官网 | 91av一区 | 免费在线播放毛片 | 国产精品99久久久久久久 | 国产伦精品一区二区三区精品 | 国产91精品露脸国语对白 | 免费看日产一区二区三区 | 三级a视频 | av一区二区三 | 日韩欧美www| 手机在线看a | 河北彩花av在线播放 | 多毛的亚洲人毛茸茸 | 91色国产| 成人看片网| 伊人avav| 国产淫视 | 亚洲巨乳av | 亚洲高h | 特级西西www444人体聚色 | 制服诱惑一区二区 | 欧美老司机 | 欧美日韩精品一区二区三区四区 | 亚洲一本在线观看 | 很黄很黄的网站 | 国产一区二区av | 美女三级网站 | 日本五十路在线 | 草逼免费视频 | 成人av资源站 | 日韩成人在线一区 | 在线观看成人免费 | 久久久中文| 中文字幕精品一区久久久久 | 黄色大片黄色大片 | 6080午夜伦理 | 中文字幕第88页 | 五月亚洲婷婷 | 一级片www | 国产呦小j女精品视频 | 97小视频 | 人妻av无码一区二区三区 | 亚洲婷婷在线视频 | 成人1区 | 午夜免费一区 | 视频在线观看视频 | 日本免费网站 | 99视频网 | 免费国产高清 | 九九热在线视频播放 | jizz中国女人高潮 | 日本成人黄色片 | 国产二区精品视频 | 久久五月综合 | 黄色影音 | www.在线观看视频 | 成人福利一区二区 | 一本色道久久88亚洲精品综合 | 成人欧美性| 久久国产精品久久国产精品 | 午夜h视频 | 97在线公开视频 | 国产精品视频一区二区在线观看 | 午夜小视频在线 | 国产精品不卡一区 | 日韩精品一 | 天天色宗合 | 欧美日韩国产黄色 | 91成人高清 | aaa在线视频| 欧美一区免费观看 | 91jk制服白丝超短裙大长腿 | 在线观看黄色免费网站 | 精品国产乱码一区二 | 亚洲国产一区二区三区四区 |