記一次數(shù)據(jù)庫大作業(yè)
目標(biāo):實(shí)現(xiàn)超市的進(jìn)貨,銷售,庫存基本功能。
工具:SQL server2008,VS2017
一.前期工作
創(chuàng)建supermarket數(shù)據(jù)庫,建表
這里要注意將supplierID和productID設(shè)置外鍵,具體設(shè)置:右鍵關(guān)系 -> 選關(guān)聯(lián)鍵就好了
其實(shí)最開始最好用SQL語言去創(chuàng)建表創(chuàng)建主鍵外鍵更好,我忘了就懶得改了
不熟悉SQL語言的話參考:sql server建庫建表(數(shù)據(jù)庫和數(shù)據(jù)表的常用操作) - Aquiet - 博客園
最好有張流程圖:

這些工作準(zhǔn)備好后,就可以摩拳擦掌大顯身手(其實(shí)就是各種copy修改代碼,因?yàn)槲姨肆薚.T)
二.Winform建立
打開VS新建Windows應(yīng)用窗體項(xiàng)目,創(chuàng)建完后就在本項(xiàng)目下添加想要的窗體就好了,下面是我添加的一些
關(guān)于form中控件的使用我就不作說明了,自行搜索吧...我就說幾個我認(rèn)為重要的代碼:
1.兩個窗體之間的轉(zhuǎn)換,比如我要從登錄界面進(jìn)入到管理系統(tǒng),雙擊登錄button
代碼:
管理系統(tǒng) fm2 = new 管理系統(tǒng)();
? ? ? ? ? ? this.Hide();
? ? ? ? ? ? if (fm2.ShowDialog(this) == DialogResult.Cancel)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.Show();
? ? ? ? ? ? }
另外,記住一定要加上一條using指令:? using System.Data.SqlClient;//引入命名空間
2.添加數(shù)據(jù),例如添加用戶
代碼:
private void input_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? bool input()//非空驗(yàn)證
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (textusername.Text.Trim().Equals(String.Empty))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("請輸入用戶名!", "提示");
? ? ? ? ? ? ? ? ? ? textusername.Focus();//獲取焦點(diǎn)
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (textpass.Text.Trim().Equals(String.Empty))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("請輸入密碼!", "提示");
? ? ? ? ? ? ? ? ? ? textpass.Focus();//獲取焦點(diǎn)
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? string 用戶名 = textusername.Text;
? ? ? ? ? ? string 密碼 = textpass.Text;
? ? ? ? ? ? //創(chuàng)建數(shù)據(jù)庫連接類的對象
? ? ? ? ? ? string connString = "Data Source=.;Initial Catalog=supermarket;Integrated Security=True";
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? conn.Open();
? ? ? ? ? ? //執(zhí)行con對象的函數(shù),返回一個SqlCommand類型的對象
? ? ? ? ? ? SqlCommand cmd = conn.CreateCommand();
? ? ? ? ? ? //拼寫語句
? ? ? ? ? ? cmd.CommandText = "insert into tb_user values('" + 用戶名 + "','" + 密碼 + "')";
? ? ? ? ? ? //增刪改用ExecuteNonQuery,會返回一個整型數(shù)字
? ? ? ? ? ? int count = cmd.ExecuteNonQuery();
? ? ? ? ? ? if (count > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("添加成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("添加失敗");
? ? ? ? ? ? }
? ? ? ? }
3.刪除數(shù)據(jù),比如供應(yīng)商信息
代碼:
string 供貨商ID = textBox8.Text;
? ? ? ? ? ? //創(chuàng)建數(shù)據(jù)庫連接類的對象
? ? ? ? ? ? string connString = "Data Source=.;Initial Catalog=supermarket;Integrated Security=True";
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? conn.Open();
? ? ? ? ? ? //執(zhí)行con對象的函數(shù),返回一個SqlCommand類型的對象
? ? ? ? ? ? SqlCommand cmd = conn.CreateCommand();
? ? ? ? ? ? //拼寫語句
? ? ? ? ? ? cmd.CommandText = "delete from supplier where supplierID=" + 供貨商ID;
? ? ? ? ? ? //增刪改用ExecuteNonQuery,會返回一個整型數(shù)字
? ? ? ? ? ? int count = cmd.ExecuteNonQuery();
? ? ? ? ? ? if (count > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("刪除成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("刪除失敗");
? ? ? ? ? ? }
? ? ? ? ? ? conn.Close();
?????? }
4.修改數(shù)據(jù),比如修改供應(yīng)商信息
代碼:
??????????? string 供貨商ID = textBox8.Text;
? ? ? ? ? ? string 供貨人 = textBox9.Text;
? ? ? ? ? ? string 供貨電話 = textBox10.Text;
? ? ? ? ? ? string 供貨地址 = textBox11.Text;
? ? ? ? ? ? string connString = "Data Source=.;Initial Catalog=supermarket;Integrated Security=True";//連接數(shù)據(jù)庫,'Data Source'后面的 . 代表本地,寫成(local)也可
? ? ? ? ? ? SqlConnection conn = new SqlConnection(connString);
? ? ? ? ? ? conn.Open();//打開數(shù)據(jù)庫
? ? ? ? ? ? SqlCommand cmd = conn.CreateCommand();
? ? ? ? ? ? cmd.CommandText = "update supplier set suppliername='" + 供貨人 + "',supplierphone='" + 供貨電話 + "',supplieraddress='" + 供貨地址 + "'where supplierID=" + 供貨商ID;
? ? ? ? ? ? //增刪改用ExecuteNonQuery,會返回一個整型數(shù)字
? ? ? ? ? ? int count = cmd.ExecuteNonQuery();
? ? ? ? ? ? if (count > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("修改成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("修改失敗");
? ? ? ? ? ? }
? ? ? ? ? ? conn.Close();
5.datagridview 查詢功能,比如輸入商品ID,點(diǎn)擊查詢就能在datagridview中顯示銷售情況
private void query_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? string con, sql;? //因?yàn)槲淖趾荛L所以定義字符串記下來
? ? ? ? ? ? con = "Server=.;Database=supermarket;Trusted_Connection=SSPI";
? ? ? ? ? ? sql = string.Format("select * from sale where productID='{0}'", textBox6.Text);
? ? ? ? ? ? SqlConnection cn = new SqlConnection(con);? //建立連接!
? ? ? ? ? ? cn.Open();? ? ? ? ? ? ? //打開數(shù)據(jù)庫
? ? ? ? ? ? SqlDataAdapter myda = new SqlDataAdapter(sql, con);? //用來對連接起來的數(shù)據(jù)庫進(jìn)行篩選
? ? ? ? ? ? DataSet myds = new DataSet();? ? ? ? ? //填充容器
? ? ? ? ? ? myda.Fill(myds, "sale");
? ? ? ? ? ? dataGridView1.DataSource = myds.Tables["sale"];
????????? }
還有銷售庫存的同步我沒有弄完,待續(xù)TT
12/16
6.銷售庫存同步,銷售多少商品此商品庫存數(shù)量就減少多少
在庫存查詢界面設(shè)計(jì)中可以多增加一個“更新”button,如下:
主要代碼:
string con = "Data Source=.;Initial Catalog=supermarket;Integrated Security=True";
? ? ? ? ? ? SqlConnection cn = new SqlConnection(con);? //建立連接!
? ? ? ? ? ? cn.Open();? ? ? ? ? ? ? //打開數(shù)據(jù)庫
? ? ? ? ? ? SqlCommand cmd = cn.CreateCommand();
? ? ? ? ? ? cmd.CommandText = "update inventory set invamount=invamount-(select saleamount from sale) where productID=" + textBox6.Text;
? ? ? ? ? ? int count = cmd.ExecuteNonQuery();
? ? ? ? ? ? if (count > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("更新查詢成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("更新查詢失敗");
? ? ? ? ? ? }
目前為止,數(shù)據(jù)的增刪改查還有銷售庫存同步功能就算全部實(shí)現(xiàn)啦,this is not the end.