一、系統主頁面—Form1
系統運行加載頁面,主要包含三個功能,①登錄、②注冊、③退出系統
程序運行圖:
登錄功能,跳轉到登錄頁面
注冊功能,跳轉到注冊頁面
退出系統,程序結束運行
代碼如下:
using System;
using System.Windows.Forms;namespace fiber_yy
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){this.Hide();new login().Show();}private void button3_Click(object sender, EventArgs e){MessageBox.Show("see you again~");Application.Exit(); //退出進程}private void button2_Click(object sender, EventArgs e){this.Hide();new register().Show();}}
}
二、用戶注冊—register
統一創建數據庫fiber_yy
用戶注冊功能,用戶需要填寫賬號、密碼、性別、手機號
對應表名為yy_user,id為主鍵自增、username為賬號、password為密碼、sex為性別、phone為手機號
其中賬號和密碼長度為3-8,并且賬號不允許重復,性別通過選擇Checked方法來獲取用戶點擊性別,手機號為11位數
若輸入都符合要求,點擊用戶注冊按鈕,旁邊的label6-label9會顯示√,提示用戶輸入格式全部正確
若賬號重復會彈窗提示此賬號已存在~~~,點擊確定,賬號輸入框自動清除,其他輸入框保持原始記錄
若賬號、密碼、手機號長度不對也會給予相應的彈窗提示
若全部格式輸入成功,點擊用戶注冊,彈窗提示注冊成功!,點擊確定,自動跳轉Form1系統主頁面
返回主頁面按鈕會返回到From1系統主頁面
用戶注冊
當然,√也可以設置為紅色,這種外觀自己設置吧
代碼如下:
using System;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace fiber_yy
{public partial class register : Form{/*server 主機名稱Initial Catalog 數據庫名稱User ID=sa 默認不變pwd 數據庫密碼*/public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public register(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string sex="man";//默認值我設置為manlong phone_number=0;int account_number, password, sum=0;//若輸入信息不滿足要求sum會自加,最終判斷sum為0時表示一切都符合要求account_number = textBox1.Text.Length;password = textBox2.Text.Length;phone_number = long.Parse(textBox3.Text);if (account_number <= 3 || account_number >= 8){label6.Text = "賬號長度應該在3~8字符之間";sum++;}else{label6.Text = "校驗成功";}if (password <= 3 || password >= 8){label7.Text = "密碼長度應該在3~8字符之間";sum++;}else{label7.Text = "校驗成功";}if (radioButton1.Checked){sex = "man";label8.Text = "校驗成功";}else if (radioButton2.Checked){sex = "woman";label8.Text = "校驗成功";}else{label8.Text = "請選擇性別";}if (phone_number < 10000000000 || phone_number > 99999999999){label9.Text = "請輸入正確的手機號";sum++;}else{label9.Text = "校驗成功";}if (sum == 0){label6.Text = "√";label7.Text = "√";label8.Text = "√";label9.Text = "√";try{string sql = string.Format("select count(*) from yy_user where username='{0}'", textBox1.Text);SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();int a = (int)cmd.ExecuteScalar();//返回一個值,看用戶是否存在 1存在 0不存在StringBuilder strsql = new StringBuilder();if(a==1){MessageBox.Show("此賬號已存在~~~");textBox1.Text = "";label6.Text = "";label7.Text = "";label8.Text = "";label9.Text = "";}else{string INSERT_sql = string.Format("INSERT INTO yy_user VALUES ('{0}','{1}','{2}','{3}')", textBox1.Text.Trim(), textBox2.Text.Trim(), sex, textBox3.Text.Trim());SqlCommand INSERT_cmd = new SqlCommand(INSERT_sql, conn);int count = INSERT_cmd.ExecuteNonQuery();if (count > 0){MessageBox.Show("注冊成功!");conn.Close();this.Close();new Form1().Show();}else {MessageBox.Show("注冊失敗!");}}}catch(Exception ex){MessageBox.Show(ex.Message);}}}private void button2_Click(object sender, EventArgs e){this.Close();new Form1().Show();}}
}
三、用戶登錄—login
用戶輸入賬號和密碼以及驗證碼,驗證碼為隨機生成的5個隨機數
用戶登錄所用到的表為yy_user
用戶登錄
登錄成功跳轉到主頁面main_page
登錄的時候,同時會將用戶的信息以及登錄時間存入表yy_user_record中
yy_user_record表設計如下
username為登錄賬號、sex為賬號用戶的性別、phone為賬號用戶的手機號、time為此賬號登錄時間
沒有存儲密碼是為了安全起見,此記錄會在流水查詢功能中進行展示
代碼如下:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;namespace fiber_yy
{public partial class login : Form{public string name = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public string identification = null;public login(){InitializeComponent();}private void button1_Click(object sender, EventArgs e)//登錄{string sex = "";string phone = "";string day = DateTime.Now.ToLocalTime().ToString();//獲取當前時間string username = textBox1.Text;string password = textBox2.Text;string identify = textBox3.Text;if (username.Equals("") || password.Equals("") || identify.Equals(""))//不能為空{MessageBox.Show("提示:請輸入用戶名、密碼、驗證碼!", "警告");}else{string sqlSel = "select count(*) from yy_user where username = '" + username + "' and password = '" + password + "'";SqlCommand cmd = new SqlCommand(sqlSel, conn);conn.Open();if (Convert.ToInt32(cmd.ExecuteScalar()) > 0 )//賬號密碼正確{string sql = "select username,sex,phone from yy_user where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);SqlDataReader read = com.ExecuteReader();while (read.Read())//獲取yy_user表中的username,sex,phone{name = read["username"].ToString();sex = read["sex"].ToString();phone = read["phone"].ToString();}read.Close();if (identify==identification)//判斷驗證碼是否輸入正確{string INSERT_sql = string.Format("INSERT INTO yy_user_record VALUES ('{0}','{1}','{2}','{3}')", name,sex,phone, DateTime.Now.ToLocalTime());SqlCommand INSERT_cmd = new SqlCommand(INSERT_sql, conn);int count = INSERT_cmd.ExecuteNonQuery();if (count > 0){MessageBox.Show("登錄成功");conn.Close();new main_page().Show();this.Close();}else{MessageBox.Show("記錄用戶失敗");conn.Close();}}else {MessageBox.Show("驗證碼輸入錯誤");conn.Close();}}else{MessageBox.Show("請檢查賬號密碼");conn.Close();}}}private void pictureBox1_Click(object sender, EventArgs e)//驗證碼{Random r = new Random();string str = null;for (int i = 0; i < 5; i++){int n = r.Next(0, 10);str += n;//包括字符串在內}identification = str;Bitmap b = new Bitmap(100, 15);Graphics g = Graphics.FromImage(b);for (int i = 0; i < 5; i++){String[] fonts = { "宋體", "黑體", "隸書", "仿宋", "微軟雅黑" };//字體數組Color[] colors = { Color.Red, Color.Black, Color.Blue,Color.YellowGreen ,Color.Green };//顏色數組Font f = new Font(fonts[r.Next(0, 5)], 25, FontStyle.Bold);SolidBrush s = new SolidBrush(colors[r.Next(0, 5)]);//定義一個單獨的畫筆,使每個字符的顏色隨機Point p = new Point(i * 20, 0);//每個字符間隔20g.DrawString(str[i].ToString(), Font, s, p);}for (int a = 0; a < 5; a++){Point p1 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));Point p2 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));//線的兩點不能超過圖片的長和寬Pen pen = new Pen(Brushes.Cyan);//青色線段g.DrawLine(pen, p1, p2);}pictureBox1.Image = b;}private void button2_Click(object sender, EventArgs e){new Form1().Show();this.Close();}private void button3_Click(object sender, EventArgs e){new retrieve_password().Show();this.Close();}}
}
四、找回密碼—retrieve_password
找回密碼
輸入賬號和注冊時填寫的手機號即可進行校驗,校驗通過進行修改密碼,跳轉change_password修改密碼頁面
可以設置顯示驗證碼框
驗證通過跳轉change_password修改密碼頁面
代碼如下:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;namespace fiber_yy
{public partial class retrieve_password : Form{public string name = "";public string identification = null;public string phone = "";public string phone_db = "";public string username = "";public string username_db = "";public string identify = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public retrieve_password(){InitializeComponent();}private void pictureBox1_Click(object sender, EventArgs e){Random r = new Random();string str = null;for (int i = 0; i < 5; i++){int n = r.Next(0, 10);str += n;//包括字符串在內}identification = str;Bitmap b = new Bitmap(100, 15);Graphics g = Graphics.FromImage(b);for (int i = 0; i < 5; i++){String[] fonts = { "宋體", "黑體", "隸書", "仿宋", "微軟雅黑" };//字體數組Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.YellowGreen, Color.Green };//顏色數組Font f = new Font(fonts[r.Next(0, 5)], 25, FontStyle.Bold);SolidBrush s = new SolidBrush(colors[r.Next(0, 5)]);//定義一個單獨的畫筆,使每個字符的顏色隨機Point p = new Point(i * 20, 0);//每個字符間隔20g.DrawString(str[i].ToString(), Font, s, p);}for (int a = 0; a < 5; a++){Point p1 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));Point p2 = new Point(r.Next(0, b.Width), r.Next(0, b.Height));//線的兩點不能超過圖片的長和寬Pen pen = new Pen(Brushes.Cyan);//青色線段g.DrawLine(pen, p1, p2);}pictureBox1.Image = b;}private void button1_Click(object sender, EventArgs e){username = textBox1.Text;phone = textBox2.Text;identify = textBox3.Text;if (identify == identification)//判斷驗證碼是否輸入正確{conn.Open();string sql = "select username,phone from yy_user where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);SqlDataReader read = com.ExecuteReader();while (read.Read())//獲取yy_user表中的username,sex,phone{username_db = read["username"].ToString();phone_db = read["phone"].ToString();}read.Close();if(phone_db==phone){conn.Close();MessageBox.Show("校驗通過");new change_password(username).Show();this.Close();}else {conn.Close();MessageBox.Show("注冊手機號不對");}}else{conn.Close();MessageBox.Show("驗證碼輸入錯誤");}}private void button2_Click(object sender, EventArgs e){new login().Show();this.Hide();}}}
五、修改密碼—change_password
修改yy_user表中賬號對應的密碼
點擊確定,自動跳轉至登錄頁面login
代碼如下:
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace fiber_yy
{public partial class change_password : Form{public string name = "";public string password = "";public string password1 = "";public string username = "";public static string str_conn = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";SqlConnection conn = new SqlConnection(str_conn);public change_password(){InitializeComponent();}public change_password(string yy)//接收登錄用戶的賬號信息{InitializeComponent();username = yy;}private void button1_Click(object sender, EventArgs e){password = textBox1.Text;password1 = textBox2.Text;conn.Open();if (password.Length <= 3 || password.Length >= 8){label3.Text = "密碼長度應該在3~8字符之間";conn.Close();}else if(password != password1){label3.Text = "兩次輸入密碼不一致";conn.Close();}else if(password==password1){string sql = "update yy_user set password = '"+ password + "' where username = '" + username + "'";SqlCommand com = new SqlCommand(sql, conn);com.ExecuteNonQuery();//返回值為操作的條數MessageBox.Show("修改成功");conn.Close();new login().Show();this.Close();}}}
}
六、主頁面—main_page
主頁面也就起到跳轉的作用
代碼如下:
using System;
using System.Windows.Forms;namespace fiber_yy
{public partial class main_page : Form{public main_page(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){MessageBox.Show("退出成功");this.Close();new Form1().Show();}private void button2_Click(object sender, EventArgs e){this.Close();new warehousing().Show();}private void button3_Click(object sender, EventArgs e){this.Close();new shipment().Show();}private void button4_Click(object sender, EventArgs e){this.Close();new query().Show();}private void button5_Click(object sender, EventArgs e){this.Close();new flow_query().Show();}}
}
七、入庫管理功能—warehousing
所用到yy_textile表
入庫管理頁面warehousing
代碼如下:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace fiber_yy
{public partial class warehousing : Form{public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";public warehousing(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){OpenFileDialog ofdlgTest = new OpenFileDialog();ofdlgTest.Filter = "";ofdlgTest.Multiselect = false; //設置不可以選擇多個文件//顯示文件打開對話框DialogResult result = ofdlgTest.ShowDialog();//選擇打開按鈕的時候,將文件名顯示到文本框中if (result == DialogResult.OK) //判斷是否打開文件{this.textBox11.Text = ofdlgTest.FileName;pictureBox1.Image = Image.FromFile(ofdlgTest.FileName);}}private void button1_Click(object sender, EventArgs e){try{string path = textBox11.Text;FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //將指定路徑的圖片添加到FileStream類中BinaryReader br = new BinaryReader(fs);//通過FileStream對象實例化BinaryReader對象byte[] imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));//將圖片轉為二進制數據//Save(imgBytesIn);//調用(自己寫的一個方法)SqlConnection conn = new SqlConnection(constr);conn.Open();string name = textBox1.Text;int number = int.Parse(textBox2.Text);float warp_density = float.Parse(textBox3.Text);float weft_density = float.Parse(textBox4.Text);float warp_linear_density = float.Parse(textBox5.Text);float weft_linear_density = float.Parse(textBox6.Text);string material = textBox7.Text;float square_meter_weight = float.Parse(textBox8.Text);float width_of_cloth = float.Parse(textBox9.Text);float horse_length = float.Parse(textBox10.Text);string organization = textBox12.Text;int stock = int.Parse(textBox13.Text);SqlCommand cmd = new SqlCommand("insert into yy_textile (name,number,warp_density,weft_density,warp_linear_density,weft_linear_density,material,square_meter_weight,width_of_cloth,horse_length,picture,organization,stock) " +"values(@name,@number,@warp_density,@weft_density,@warp_linear_density,@weft_linear_density,@material,@square_meter_weight,@width_of_cloth,@horse_length,@picture,@organization,@stock);", conn); //SQL語句cmd.Parameters.Add("@name", SqlDbType.VarChar);cmd.Parameters["@name"].Value = name;cmd.Parameters.Add("@number", SqlDbType.Int);cmd.Parameters["@number"].Value = number;cmd.Parameters.Add("@warp_density", SqlDbType.Float);cmd.Parameters["@warp_density"].Value = warp_density;cmd.Parameters.Add("@weft_density", SqlDbType.Float);cmd.Parameters["@weft_density"].Value = weft_density;cmd.Parameters.Add("@warp_linear_density", SqlDbType.Float);cmd.Parameters["@warp_linear_density"].Value = warp_linear_density;cmd.Parameters.Add("@weft_linear_density", SqlDbType.Float);cmd.Parameters["@weft_linear_density"].Value = weft_linear_density;cmd.Parameters.Add("@material", SqlDbType.VarChar);cmd.Parameters["@material"].Value = material;cmd.Parameters.Add("@square_meter_weight", SqlDbType.Float);cmd.Parameters["@square_meter_weight"].Value = square_meter_weight;cmd.Parameters.Add("@width_of_cloth", SqlDbType.Float);cmd.Parameters["@width_of_cloth"].Value = width_of_cloth;cmd.Parameters.Add("@horse_length", SqlDbType.Float);cmd.Parameters["@horse_length"].Value = horse_length;cmd.Parameters.Add("@picture", SqlDbType.Image);cmd.Parameters["@picture"].Value = imgBytesIn;cmd.Parameters.Add("@organization", SqlDbType.VarChar);cmd.Parameters["@organization"].Value = organization;cmd.Parameters.Add("@stock", SqlDbType.Int);cmd.Parameters["@stock"].Value = stock;cmd.ExecuteNonQuery();conn.Close();MessageBox.Show("圖片上傳成功");}catch {MessageBox.Show("請核對輸入信息");}}private void button3_Click(object sender, EventArgs e){new main_page().Show();this.Close();}}
}
八、出庫管理功能—shipment
出庫管理功能,用戶通過查詢數據庫中織物信息,通過織物唯一編號來查看織物信息,輸入出庫數量即可出庫。此時的出庫信息將會被記錄到數據表yy_textile_record中,用戶可以通過流水查詢功能進行查看。并且,若織物庫存小于100,則檢索時會進行庫存不足提示
涉及到出庫記錄表yy_textile_record
用于記錄什么時候哪個賬號,該賬號手機號是多少,出庫了哪個織物多少庫存信息
代碼如下:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;namespace fiber_yy
{public partial class shipment : Form{private DataSet dsall;public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";private SqlDataAdapter mDataAdapter;public login login = new login();public shipment(){InitializeComponent();try{string username = login.name;MessageBox.Show(username);SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);mDataAdapter = new SqlDataAdapter("SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' FROM yy_textile", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];}catch{MessageBox.Show("讀取失敗,請檢查是否存在該織物");}}private void button1_Click(object sender, EventArgs e){try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);mDataAdapter = new SqlDataAdapter("SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' FROM yy_textile", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];}catch{MessageBox.Show("讀取失敗");}}private void button2_Click(object sender, EventArgs e)//查看織物信息{try{string number = textBox1.Text;byte[] MyData = new byte[0];string sql = "SELECT stock FROM yy_textile WHERE number='" + number + "'";SqlConnection conn = new SqlConnection(constr);SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();string account = cmd.ExecuteScalar().ToString();int a = int.Parse(account);if (a > 100){SqlConnection conn2 = new SqlConnection(constr);conn2 = new SqlConnection(constr);mDataAdapter = new SqlDataAdapter("SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' FROM yy_textile where number='" + number + "'", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];SqlConnection conn1 = new SqlConnection(constr);conn1.Open();SqlCommand cmd1 = new SqlCommand();cmd1.Connection = conn1;cmd1.CommandText = "select * from yy_textile where number='" + number + "'";SqlDataReader sdr = cmd1.ExecuteReader();sdr.Read();object o = sdr["picture"];MyData = (byte[])sdr["picture"];//讀取第一個圖片的位流MemoryStream memoryStream = null;memoryStream = new MemoryStream(MyData);pictureBox1.Image = Image.FromStream(memoryStream);//將圖片賦給pictureBox1控件MessageBox.Show("讀取成功");}else {conn.Close();MessageBox.Show("庫存不足100請及時補充!!!");}}catch{MessageBox.Show("讀取失敗 over");}}private void button3_Click(object sender, EventArgs e)//出庫{string time = DateTime.Now.ToLocalTime().ToString();try{string sql1;string number = this.textBox1.Text;int count = int.Parse(textBox2.Text);SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);string sql = "SELECT number FROM yy_textile WHERE number='" + number + "'";SqlCommand cmd = new SqlCommand(sql, conn);conn.Open();string amount = cmd.ExecuteScalar().ToString();int a = int.Parse(amount);if (a>count) {sql1 = "update yy_textile set stock = stock - '" + count + "' where number='" + number + "'";mDataAdapter = new SqlDataAdapter(sql1, conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];MessageBox.Show("出庫成功!!!");string username = "";string sql2 = "SELECT TOP 1 username from yy_user_record order by id desc";SqlCommand com = new SqlCommand(sql2, conn);SqlDataReader read = com.ExecuteReader();while (read.Read())//獲取yy_user表中的username,sex,phone{username = read["username"].ToString();MessageBox.Show(username);}read.Close();string INSERT_sql = string.Format("INSERT INTO yy_textile_record VALUES ('{0}','{1}','{2}','{3}')", number, count, username, DateTime.Now.ToLocalTime());SqlCommand INSERT_cmd = new SqlCommand(INSERT_sql, conn);int count1 = INSERT_cmd.ExecuteNonQuery();if (count1 > 0){MessageBox.Show("記錄纖維出庫信息成功!");conn.Close();}else{MessageBox.Show("記錄纖維出庫信息失敗");conn.Close();}}else {MessageBox.Show("庫存不足無法出庫");conn.Close();}}catch (Exception ex) { MessageBox.Show(ex.Message); }finally{ }}private void button4_Click(object sender, EventArgs e){new main_page().Show();this.Close();}}
}
九、庫存查詢—query
庫存查詢所用數據表為yy_textile和七中表一樣,查詢表中的指定數據信息
代碼如下:
using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;namespace fiber_yy
{public partial class query : Form{private DataSet dsall;public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";private SqlDataAdapter mDataAdapter;public query(){InitializeComponent();comboBox1.Items.Add("根據面料名稱查詢");comboBox1.Items.Add("根據品號查詢");}private void button1_Click(object sender, EventArgs e){try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);conn.Open();mDataAdapter = new SqlDataAdapter("SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' FROM yy_textile", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];conn.Close();}catch {MessageBox.Show("讀取失敗,請檢查是否存在該織物");}}private void button2_Click(object sender, EventArgs e){new main_page().Show();this.Close();}private void button3_Click(object sender, EventArgs e){try{string model = comboBox1.SelectedItem.ToString();string command = textBox1.Text;byte[] MyData = new byte[0];SqlConnection conn = new SqlConnection(constr);conn.Open();SqlCommand cmd = new SqlCommand();cmd.Connection = conn;if (model == "根據面料名稱查詢"){cmd.CommandText = "select * from yy_textile where name='" + command + "'";model = "SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' from yy_textile where name='" + command + "'";}if (model == "根據品號查詢"){cmd.CommandText = "select * from yy_textile where number='" + command + "'";model = "SELECT name AS '紡織品名稱',number AS '紡織品品號',warp_density AS '經密度' ,weft_density AS '緯密度',warp_linear_density AS '經紗線密度',weft_linear_density AS '緯紗線密度',material AS '原料成分',square_meter_weight AS '平方米重量',width_of_cloth AS '幅寬',horse_length AS '匹長',organization AS '所用組織',stock AS '庫存量' from yy_textile where number='" + command + "'";}SqlDataReader sdr = cmd.ExecuteReader();sdr.Read();object o = sdr["picture"];MyData = (byte[])sdr["picture"];//讀取第一個圖片的位流MemoryStream memoryStream = null;memoryStream = new MemoryStream(MyData);pictureBox1.Image = Image.FromStream(memoryStream);//將圖片賦給pictureBox1控件MessageBox.Show("讀取成功");conn = new SqlConnection(constr);mDataAdapter = new SqlDataAdapter(model, conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];conn.Close();}catch{MessageBox.Show("讀取失敗,請檢查是否存在該織物");}}}
}
十、流水查詢—flow_query
流水查詢功能主要包括用戶登錄信息的查詢以及所有出庫紡織品信息,用到yy_user_record表和yy_textile_record表
代碼如下:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace fiber_yy
{public partial class flow_query : Form{private DataSet dsall;public string constr = "server=CY-20190824RMES;Initial Catalog=fiber_yy;User ID=sa;pwd=beyond";private SqlDataAdapter mDataAdapter;public flow_query(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);conn.Open();mDataAdapter = new SqlDataAdapter("SELECT username AS '用戶',sex AS '性別',phone AS '手機號',time AS '登錄時間' FROM yy_user_record", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");MessageBox.Show(dsall.ToString());dataGridView1.DataSource = dsall.Tables["hard"];conn.Close();}catch{MessageBox.Show("讀取失敗");}}private void button2_Click(object sender, EventArgs e){string account = textBox1.Text;try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);conn.Open();mDataAdapter = new SqlDataAdapter("SELECT username AS '用戶',sex AS '性別',phone AS '手機號',time AS '登錄時間' FROM yy_user_record WHERE username='" + account + "'", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView1.DataSource = dsall.Tables["hard"];conn.Close();}catch{MessageBox.Show("讀取失敗");}}private void button3_Click(object sender, EventArgs e){try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);conn.Open();mDataAdapter = new SqlDataAdapter("SELECT username AS '用戶',number AS '纖維品號',stock AS '取貨數量',time AS '出貨時間' FROM yy_textile_record", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");MessageBox.Show(dsall.ToString());dataGridView2.DataSource = dsall.Tables["hard"];conn.Close();}catch{MessageBox.Show("讀取失敗");}}private void button4_Click(object sender, EventArgs e){string number = textBox2.Text;try{SqlConnection conn = new SqlConnection(constr);conn = new SqlConnection(constr);conn.Open();mDataAdapter = new SqlDataAdapter("SELECT username AS '用戶',number AS '纖維品號',stock AS '取貨數量',time AS '出貨時間' FROM yy_textile_record WHERE number='" + number + "'", conn);dsall = new DataSet();mDataAdapter.Fill(dsall, "hard");dataGridView2.DataSource = dsall.Tables["hard"];conn.Close();}catch{MessageBox.Show("讀取失敗");}}}
}
后續仍會進行優化…
未完待續…