Select.cs
上传用户:nbcygd
上传日期:2008-07-25
资源大小:2566k
文件大小:9k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.Sql;
- using System.Data.SqlClient;
- using System.Data.SqlTypes;
- namespace Super_market
- {
- public partial class Select : Form
- {
- private SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
- private SqlConnection con;
- private SqlCommand cmd = new SqlCommand();
- private SqlDataAdapter da;
- private DataSet ds;
- private SqlCommandBuilder d;
- private string ss = "";
- int x;
- int y;
- public Select()
- {
- InitializeComponent();
- b.DataSource = ".";
- b.InitialCatalog = "wkdl";
- b.IntegratedSecurity = true;
- con = new SqlConnection(b.ConnectionString);
- this.skinEngine1.SkinFile = "vista1.ssk";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- button1.Hide();
- if (this.Text == "超级商品管理与查询")
- {
- LoadDGV();
- }
- else
- {
- button2.Enabled = false;
- button3.Enabled = false;
- button6.Enabled = false;
- textBox1.Enabled = false;
- textBox2.Enabled = false;
- textBox3.Enabled = false;
- comboBox1.Enabled = false;
- button5.Text = "经典查询";
- LoadDGV();
- MessageBox.Show("您是普通员工,部分功能受限!", "系统提示");
- }
- }
- private void LoadDGV()
- {
- //cmd.CommandText = "select id as 商品编号,warename as 商品名称,price as 单价,entries as 已售出数量,keep as 保质期 from ware";
- cmd.CommandText = "select id,warename,price,entries,keep from ware";
- cmd.Connection = con;
- da = new SqlDataAdapter(cmd);
- ds = new DataSet();
- da.Fill(ds);
- dataGridView1.DataSource = ds.Tables[0];
- dataGridView1.Columns[0].HeaderText = "商品编号";
- dataGridView1.Columns[1].HeaderText = "商品名称";
- dataGridView1.Columns[2].HeaderText = "单价";
- dataGridView1.Columns[3].HeaderText = "已售出数量";
- dataGridView1.Columns[4].HeaderText = "保质期(月)";
- dataGridView1.RowHeadersVisible = false;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- this.Dispose();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- da = new SqlDataAdapter();
- con.Open();
- da.UpdateCommand = con.CreateCommand();
- DataTable dt = new DataTable();
- dt = (DataTable)dataGridView1.DataSource;
- DataRow dr = dt.Rows[x];
- DataColumn dc = dt.Columns[y];
- da.UpdateCommand.CommandText = "update ware set " + dc.ColumnName + "='" + dr[y] + "' where [id]= " + dr["id"] + "";
- da.UpdateCommand.ExecuteNonQuery();
- ds.AcceptChanges();
- con.Close();
- MessageBox.Show("保存成功!", "系统提示");
- LoadDGV();
- }
- catch (Exception)
- {
- MessageBox.Show("不能修改商品编号!", "系统提示");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- ds.Tables[0].Rows[x].Delete();
- d = new SqlCommandBuilder(da);
- da.Update(ds);
- MessageBox.Show("删除成功!");
- LoadDGV();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Insert ins = new Insert();
- ins.evt += new Insert.dele(LoadDGV);
- ins.ShowDialog();
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- ss = comboBox1.SelectedItem.ToString();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- if (IsNumeric(textBox3.Text.Trim()) || IsNumeric(textBox3.Text.Trim()))
- {
- if (button5.Text == "经典查询")
- {
- SelectJD sejd = new SelectJD();
- sejd.ShowDialog();
- }
- else
- {
- string str = "select id,warename,price,entries,keep from ware where ";
- dataGridView1.Columns[0].HeaderText = "商品编号";
- dataGridView1.Columns[1].HeaderText = "商品名称";
- dataGridView1.Columns[2].HeaderText = "单价";
- dataGridView1.Columns[3].HeaderText = "已售出数量";
- dataGridView1.Columns[4].HeaderText = "保质期";
- string oth = "";
- string[] name = new string[4];
- for (int j = 0; j < name.Length; j++)
- {
- name[j] = "";
- }
- if (textBox1.Text.Length != 0)
- {
- name[0] = " id ='" + textBox1.Text.Trim() + "'";
- }
- if (textBox2.Text.Length != 0)
- {
- name[1] = " warename ='" + textBox2.Text + "'";
- }
- if (textBox3.Text.Length != 0)
- {
- name[2] = " price =" + textBox3.Text;
- }
- if (ss.Length != 0)
- {
- name[3] = " keep ='" + ss + "'";
- }
- for (int i = 0; i < name.Length; i++)
- {
- if (name[i].Length != 0)
- {
- str += name[i] + "and";
- }
- }
- if (str == "select id,warename,price,entries,keep from ware where ")
- {
- oth = "select id,warename,price,entries,keep from ware";
- MessageBox.Show("不能为空!");
- return;
- }
- else
- {
- oth = str.Substring(0, str.Length - 3);
- }
- //MessageBox.Show(oth);
- da = new SqlDataAdapter(oth, con);
- ds = new DataSet();
- da.Fill(ds);
- if (ds.Tables[0].Rows.Count == 0)
- {
- MessageBox.Show("没有查询结果", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- dataGridView1.DataSource = ds.Tables[0];
- }
- }
- else
- {
- MessageBox.Show("编号与单价只能为数字!");
- }
- }
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- x = e.RowIndex;
- y = e.ColumnIndex;
- DLbh bbhh = DLbh.getIns();
- string ssss = dataGridView1.Rows[x].Cells[0].Value.ToString();
- bbhh.Num = int.Parse(ssss);
- }
- private void button8_Click(object sender, EventArgs e)
- {
- Update u = new Update();
- u.evt += new Update.dele(LoadDGV);
- u.ShowDialog();
- }
- private void 查询所有SToolStripMenuItem_Click(object sender, EventArgs e)
- {
- LoadDGV();
- }
- /// <summary>
- /// 判断是否是数字
- /// </summary>
- /// <param name="str">字符串</param>
- /// <returns></returns>
- private bool IsNumeric(string str)
- {
- if (str == null || str.Length == 0)
- return false;
- System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
- byte[] bytestr = ascii.GetBytes(str);
- foreach (byte c in bytestr)
- {
- if (c < 48 || c > 57)
- {
- return false;
- }
- }
- return true;
- }
- }
- }