Select.cs
上传用户:nbcygd
上传日期:2008-07-25
资源大小:2566k
文件大小:9k
源码类别:

SQL Server

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.Sql;
  9. using System.Data.SqlClient;
  10. using System.Data.SqlTypes;
  11. namespace Super_market
  12. {
  13.     public partial class Select : Form
  14.     {
  15.         private SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
  16.         private SqlConnection con;
  17.         private SqlCommand cmd = new SqlCommand();
  18.         private SqlDataAdapter da;
  19.         private DataSet ds;
  20.         private SqlCommandBuilder d;
  21.         private string ss = "";
  22.         int x;
  23.         int y;
  24.         public Select()
  25.         {
  26.             InitializeComponent();
  27.             b.DataSource = ".";
  28.             b.InitialCatalog = "wkdl";
  29.             b.IntegratedSecurity = true;
  30.             con = new SqlConnection(b.ConnectionString);
  31.             this.skinEngine1.SkinFile = "vista1.ssk";
  32.         }
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             button1.Hide();
  36.             if (this.Text == "超级商品管理与查询")
  37.             {
  38.                 LoadDGV();
  39.             }
  40.             else
  41.             {
  42.                 button2.Enabled = false;
  43.                 button3.Enabled = false;
  44.                 button6.Enabled = false;
  45.                 textBox1.Enabled = false;
  46.                 textBox2.Enabled = false;
  47.                 textBox3.Enabled = false;
  48.                 comboBox1.Enabled = false;
  49.                 button5.Text = "经典查询";
  50.                 LoadDGV();
  51.                 MessageBox.Show("您是普通员工,部分功能受限!", "系统提示");
  52.             }
  53.         }
  54.         private void LoadDGV()
  55.         {
  56.             //cmd.CommandText = "select id as 商品编号,warename as 商品名称,price as 单价,entries as 已售出数量,keep as 保质期 from ware";
  57.             cmd.CommandText = "select id,warename,price,entries,keep from ware";
  58.             cmd.Connection = con;
  59.             da = new SqlDataAdapter(cmd);
  60.             ds = new DataSet();
  61.             da.Fill(ds);
  62.             dataGridView1.DataSource = ds.Tables[0];
  63.             dataGridView1.Columns[0].HeaderText = "商品编号";
  64.             dataGridView1.Columns[1].HeaderText = "商品名称";
  65.             dataGridView1.Columns[2].HeaderText = "单价";
  66.             dataGridView1.Columns[3].HeaderText = "已售出数量";
  67.             dataGridView1.Columns[4].HeaderText = "保质期(月)";
  68.             dataGridView1.RowHeadersVisible = false;
  69.         }
  70.         private void button4_Click(object sender, EventArgs e)
  71.         {
  72.             this.Dispose();
  73.         }
  74.         private void button1_Click(object sender, EventArgs e)
  75.         {
  76.             try
  77.             {
  78.                 da = new SqlDataAdapter();
  79.                 con.Open();
  80.                 da.UpdateCommand = con.CreateCommand();
  81.                 DataTable dt = new DataTable();
  82.                 dt = (DataTable)dataGridView1.DataSource;
  83.                 DataRow dr = dt.Rows[x];
  84.                 DataColumn dc = dt.Columns[y];
  85.                 da.UpdateCommand.CommandText = "update ware set " + dc.ColumnName + "='" + dr[y] + "' where [id]= " + dr["id"] + "";
  86.                 da.UpdateCommand.ExecuteNonQuery();
  87.                 ds.AcceptChanges();
  88.                 con.Close();
  89.                 MessageBox.Show("保存成功!", "系统提示");
  90.                 LoadDGV();
  91.             }
  92.             catch (Exception)
  93.             {
  94.                 MessageBox.Show("不能修改商品编号!", "系统提示");
  95.             }
  96.         }
  97.         private void button2_Click(object sender, EventArgs e)
  98.         {
  99.             ds.Tables[0].Rows[x].Delete();
  100.             d = new SqlCommandBuilder(da);
  101.             da.Update(ds);
  102.             MessageBox.Show("删除成功!");
  103.             LoadDGV();
  104.         }
  105.         private void button3_Click(object sender, EventArgs e)
  106.         {
  107.             Insert ins = new Insert();
  108.             ins.evt += new Insert.dele(LoadDGV);
  109.             ins.ShowDialog();
  110.         }
  111.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  112.         {
  113.             ss = comboBox1.SelectedItem.ToString();
  114.         }
  115.         private void button5_Click(object sender, EventArgs e)
  116.         {
  117.             if (IsNumeric(textBox3.Text.Trim()) || IsNumeric(textBox3.Text.Trim()))
  118.             {
  119.                 if (button5.Text == "经典查询")
  120.                 {
  121.                     SelectJD sejd = new SelectJD();
  122.                     sejd.ShowDialog();
  123.                 }
  124.                 else
  125.                 {
  126.                     string str = "select id,warename,price,entries,keep from ware where ";
  127.                     dataGridView1.Columns[0].HeaderText = "商品编号";
  128.                     dataGridView1.Columns[1].HeaderText = "商品名称";
  129.                     dataGridView1.Columns[2].HeaderText = "单价";
  130.                     dataGridView1.Columns[3].HeaderText = "已售出数量";
  131.                     dataGridView1.Columns[4].HeaderText = "保质期";
  132.                     string oth = "";
  133.                     string[] name = new string[4];
  134.                     for (int j = 0; j < name.Length; j++)
  135.                     {
  136.                         name[j] = "";
  137.                     }
  138.                     if (textBox1.Text.Length != 0)
  139.                     {
  140.                         name[0] = " id ='" + textBox1.Text.Trim() + "'";
  141.                     }
  142.                     if (textBox2.Text.Length != 0)
  143.                     {
  144.                         name[1] = " warename ='" + textBox2.Text + "'";
  145.                     }
  146.                     if (textBox3.Text.Length != 0)
  147.                     {
  148.                         name[2] = " price =" + textBox3.Text;
  149.                     }
  150.                     if (ss.Length != 0)
  151.                     {
  152.                         name[3] = " keep ='" + ss + "'";
  153.                     }
  154.                     for (int i = 0; i < name.Length; i++)
  155.                     {
  156.                         if (name[i].Length != 0)
  157.                         {
  158.                             str += name[i] + "and";
  159.                         }
  160.                     }
  161.                     if (str == "select id,warename,price,entries,keep from ware where ")
  162.                     {
  163.                         oth = "select id,warename,price,entries,keep from ware";
  164.                         MessageBox.Show("不能为空!");
  165.                         return;
  166.                     }
  167.                     else
  168.                     {
  169.                         oth = str.Substring(0, str.Length - 3);
  170.                     }
  171.                     //MessageBox.Show(oth);
  172.                     da = new SqlDataAdapter(oth, con);
  173.                     ds = new DataSet();
  174.                     da.Fill(ds);
  175.                     if (ds.Tables[0].Rows.Count == 0)
  176.                     {
  177.                         MessageBox.Show("没有查询结果", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  178.                         return;
  179.                     }
  180.                     dataGridView1.DataSource = ds.Tables[0];
  181.                 }
  182.             }
  183.             else
  184.             {
  185.                 MessageBox.Show("编号与单价只能为数字!");
  186.             }
  187.         }
  188.         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  189.         {
  190.             x = e.RowIndex;
  191.             y = e.ColumnIndex;
  192.             DLbh bbhh = DLbh.getIns();
  193.             string ssss = dataGridView1.Rows[x].Cells[0].Value.ToString();
  194.             bbhh.Num = int.Parse(ssss);
  195.         }
  196.         private void button8_Click(object sender, EventArgs e)
  197.         {
  198.             Update u = new Update();
  199.             u.evt += new Update.dele(LoadDGV);
  200.             u.ShowDialog();
  201.         }
  202.         private void 查询所有SToolStripMenuItem_Click(object sender, EventArgs e)
  203.         {
  204.             LoadDGV();
  205.         }
  206.         /// <summary>
  207.         /// 判断是否是数字
  208.         /// </summary>
  209.         /// <param name="str">字符串</param>
  210.         /// <returns></returns>
  211.         private bool IsNumeric(string str)
  212.         {
  213.             if (str == null || str.Length == 0)
  214.                 return false;
  215.             System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
  216.             byte[] bytestr = ascii.GetBytes(str);
  217.             foreach (byte c in bytestr)
  218.             {
  219.                 if (c < 48 || c > 57)
  220.                 {
  221.                     return false;
  222.                 }
  223.             }
  224.             return true;
  225.         }
  226.     }
  227. }