using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace jiesuanxitong
{
public partial class Form1 : Form
{
public SqlConnection conn;
public Form1()
{
conn = new SqlConnection("Data Source=.;Initial Catalog=depot;User ID=sa;Password=1234");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string my_t = textBox1.Text.ToString().Trim();
if (my_t != "")
{
string a = "";
string b = "";
int c = 0;
string sql = "select * from depot where dep_tiao='" + my_t + "'";
SqlCommand myCommand = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
a = myReader.GetString(1);
b = myReader.GetString(2);
c = Convert.ToInt32(myReader.GetInt32(3));
}
conn.Close();
if (this.HasCode(a))
{
UpdateOrder(a);
}
else
{
conn.Close();
Insert(a, b, c);
}
}
else
{
MessageBox.Show("请输入条形码!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Insert(string sno, string sname, int ssex)
{
string a = sno;
string b = sname;
int c = ssex;
if (c == 0)
{
MessageBox.Show("对不起!本店没有此商品》", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlCommand myCommand = new SqlCommand("insert into depotl (dep_tiao,dep_name,dep_value,dep_sum) values ('" + a + "','" + b + "'," + c + "," + 1 + ")", conn);
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
shuaxin();
}
}
private void shuaxin()
{
SqlCommand myCommand = new SqlCommand("select dep_tiao,dep_name,dep_value,dep_sum from depotl", conn);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = myCommand;
conn.Close();
DataSet ds = new DataSet();
sda.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
string sql = "select sum(dep_value*dep_sum) from depotl";
SqlCommand myCommand = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
label3.Text = myReader.GetInt32(0).ToString().Trim();
} conn.Close();
}
else
{
MessageBox.Show("还未购买任何物品!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
SqlCommand myCommand = new SqlCommand("delete from depotl ", conn);
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
shuaxin();
label3.Text = "0";
textBox1.Text = "";
}
/// <summary>
/// 判断数据库里面有没有 编号为:code的数据
/// </summary>
/// <param name="code">条形码</param>
/// <returns></returns>
public bool HasCode(string code)
{
string sql = "select * from depotl where dep_tiao='" + code + "'";
SqlCommand myCommand = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
bool tag = myReader.Read();
conn.Close();
if (tag)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 如果有,则将数据更新
/// </summary>
/// <param name="code">条形码</param>
public void UpdateOrder(String code)
{
SqlCommand myCommand = new SqlCommand("update depotl set dep_sum=dep_sum+1 where dep_tiao='" + code + "'", conn);
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
shuaxin();
}
}
}