⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addstore.cs

📁 利用vs2008+sql2000开发的物资管理系统
💻 CS
字号:
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.SqlClient;

namespace login
{
    public partial class addstore : Form
    {
        SqlDataAdapter sqlDataAdapter1;
        //存取数据库的主要类
        SqlCommand sqlCommand1;
        //SQL语句处理的类
        SqlConnection sqlConnection1;
        // 表示是否处于插入新记录的状态
        private bool bNewRecord = false;
        DataSet dataSet1;
       
        // 获取所有客户的ID
        private void GetCustomID()
        {
            SqlDataReader sdr;
            sqlConnection1.Open();	// 打开连接
            sdr = sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
            cbxID.Items.Clear();
            while (sdr.Read())
            {
                // 把客户ID插入到组合框控件中
                cbxID.Items.Add(sdr.GetValue(0));
            }
            sdr.Close();			// 关闭SqlDataReader对象和数据库连接
           cbxID.SelectedIndex = 0;
        }
        public addstore()
        {
            InitializeComponent();
            // Windows 安全登录机制
            String sConnString = "Data Source=localhost;Initial Catalog=WZGL_20052070;Integrated Security=True";
            //SQL语句
            String sSQL = "SELECT * FROM outstore";
            //创建一个数据库连接对象
            sqlConnection1 = new SqlConnection(sConnString);
            sqlCommand1 = new SqlCommand(sSQL, sqlConnection1);

            //创建一个SqlDataAdapter对象
            sqlDataAdapter1 = new SqlDataAdapter(sSQL, sqlConnection1);
            // 创建一个DataSet对象
            dataSet1 = new DataSet();
            sqlDataAdapter1.Fill(dataSet1, "admin");
            dataGridView1.DataSource = dataSet1.Tables["admin"];
            sqlCommand1.CommandText = "SELECT pName FROM room order by pName";
            GetCustomID();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string sqlStatement;
            int c = int.Parse(textBox3.Text);
            int b = int.Parse(textBox6.Text);
            if (b >= c)
            {
                if (bNewRecord == true)
                {
                    sqlStatement = "INSERT INTO room(pName,producer,origin,quantity,qPrice,storage,storeTime) VALUES(" +
                      "'" + cbxID.Text + "'," +
                    "'" + textBox1.Text + "'," +
                    "'" + textBox2.Text + "'," +
                    "'" + textBox3.Text + "'," +
                    "'" + textBox4.Text + "'," +
                     "'" + comboBox1.Text + "'," +
                     "'" + textBox5.Text+ "')";

                }
                else
                {
                    sqlStatement = "UPDATE room  SET " +
                        "producer='" + textBox1.Text + "'," +
                        "origin='" + textBox2.Text + "'," +
                        "quantity='" + textBox3.Text + "'," +
                        "qPrice='" + textBox4.Text + "'," +
                        "storage='" + comboBox1.Text + "'," +
                        "storetime='" + textBox5.Text + "'" +
                        "WHERE pName = '" + cbxID.Text + "'";
                }
                SqlCommand sqlcmd = new SqlCommand( sqlStatement,sqlConnection1);
    
   
                sqlDataAdapter1.Fill(dataSet1, "admins");
                dataGridView1.DataSource = dataSet1.Tables["admins"];
                try
                {
                    sqlConnection1.Open();
                    int rowAffected = sqlcmd.ExecuteNonQuery();
                    if (rowAffected == 1 && bNewRecord == true)
                        cbxID.Items.Add(cbxID.Text);    
                    MessageBox.Show("添加成功", "",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("更新错误:" + ex.Message, "出现错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    sqlConnection1.Close();
                }
                if (bNewRecord == true)
                {

                    cbxID.DropDownStyle = ComboBoxStyle.DropDownList;
                    bNewRecord = false;
                    cbxID.SelectedIndex = cbxID.Items.Count - 1;
                }
            }
            else
                MessageBox.Show("已经超出最大可容纳量" , "",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
        }     

        private void button2_Click(object sender, EventArgs e)
        {
            //MainFrm mainform = new MainFrm();
            //mainform.Show();
            //this.Hide();
            this.Close();
        }

        private void cbxID_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            string a = "SELECT nowstorequantity FROM store WHERE storage = " + "'" + comboBox1.Text + "'";
            SqlCommand sqlcmd = new SqlCommand(a, sqlConnection1);
            SqlDataReader sdr;
            sqlConnection1.Open();
            sdr = sqlcmd.ExecuteReader();
            if (sdr.Read())
            {
                textBox6.Text = sdr[0].ToString();
            }

            sdr.Close();
            sqlConnection1.Close();

            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() == cbxID.Text)
                    dataGridView1.Rows[i].Selected = true;
            }
           
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (cbxID.SelectedIndex > 0)
                cbxID.SelectedIndex -= 1;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (cbxID.SelectedIndex < cbxID.Items.Count - 1)
                cbxID.SelectedIndex += 1;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            String constr = "Data Source=localhost; Initial Catalog=WZGL_20052070;Integrated Security=true";
            SqlConnection scon = new SqlConnection(constr);
            SqlDataAdapter sa = new SqlDataAdapter();
            SqlCommand storescommand = new SqlCommand();
            storescommand.CommandText = "select *from room";
            storescommand.Connection = scon;
            sa.SelectCommand = storescommand;
            DataSet ds = new DataSet();
            sa.Fill(ds, "admin");

            dataGridView1.DataSource = ds.Tables["admin"];
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SqlCommand sqlcmd = new SqlCommand(
                "DELETE FROM room WHERE pName=@ID",
                sqlConnection1);
            sqlcmd.Parameters.AddWithValue("@ID", cbxID.Text);
            try
            {
                sqlConnection1.Open();
                int rowAffected = sqlcmd.ExecuteNonQuery();
                if (rowAffected == 1)
                    cbxID.Items.RemoveAt(cbxID.SelectedIndex);
                MessageBox.Show("已经删除,请按更新", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("删除错误:" + ex.Message, "出现错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sqlConnection1.Close();
            }
            if (cbxID.SelectedIndex < cbxID.Items.Count - 1)
                cbxID.SelectedIndex += 1;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";      
            textBox4.Text = "";
            comboBox1.Text = "";
            textBox5.Text = "";
            textBox5.Text = "";
            textBox6.Text = ""; 
            cbxID.DropDownStyle = ComboBoxStyle.DropDown;
            cbxID.Text = "";
            bNewRecord = true;	
           
            
        }

        private void update_Click(object sender, EventArgs e)
        {
            toolStripButton1_Click(sender, e);
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            addstoreorder orders = new addstoreorder();
            orders.pName = cbxID.Text;
            orders.ShowDialog();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            cbxID.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            string a = "SELECT count(quantity) FROM room WHERE storage = " + "'" + comboBox1.Text + "'";
            
            
              SqlCommand sqlcmd = new SqlCommand(a, sqlConnection1);
                SqlDataReader sdr;
                sqlConnection1.Open();
                sdr = sqlcmd.ExecuteReader();
                if (sdr.Read())
                {
                    if (sdr[0].ToString() == "0")
                    {
                        MessageBox.Show("当前还没有商品入库,请输入1000", "",
                      MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                        MessageBox.Show("请按查看当前库存按钮", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                } sdr.Close();
                sqlConnection1.Close();

                        
                   
                }

        private void button4_Click(object sender, EventArgs e)
        {
            string f = "SELECT sum(quantity) FROM room WHERE storage = " + "'" + comboBox1.Text + "'";
            SqlCommand scon = new SqlCommand(f, sqlConnection1);
            SqlDataReader dr;
            sqlConnection1.Open();
            dr = scon.ExecuteReader();

            if (dr.Read())
            {
                int g = int.Parse(dr[0].ToString());
                int y = 1000 - g;
                textBox6.Text = y.ToString();
            }
            dr.Close();
            sqlConnection1.Close();
        }

        private void addstore_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
               

     }

}
        
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -