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

📄 admin.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 Admin : 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 Admin()
        {
            InitializeComponent();
            //SQL Server 登录机制
            //String  sConnString = "server=VS-LV;uid=sa;pwd=;database=Northwind";
            // Windows 安全登录机制
            String sConnString = "Data Source=localhost;Initial Catalog=WZGL_20052070;Integrated Security=True";
            //SQL语句
            String sSQL = "SELECT * FROM login";
            //创建一个数据库连接对象
            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 adName FROM login order by adName";
            GetCustomID();
        }

        private void button3_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 login";
            storescommand.Connection = scon;

            sa.SelectCommand = storescommand;

            DataSet ds = new DataSet();

            sa.Fill(ds, "admin");

            dataGridView1.DataSource = ds.Tables["admin"];
            MessageBox.Show("查询完成,请选择Admin,再按删除按钮", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        private void cxID_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 创建SQL命令对象
            SqlCommand sqlcmd = new SqlCommand(
                "SELECT * FROM login WHERE adName = @user",
                sqlConnection1);
            // 设置参数
            //sqlcmd.Parameters["@user"].Value = cbxID.Text;
            sqlcmd.Parameters.AddWithValue("@user", cbxID.Text);

            //SqlDataReader sdr;
            sqlConnection1.Open();

            sqlConnection1.Close();

            //   处理到 datagridview行的定位

            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 add_Click(object sender, EventArgs e)
        {

            cbxID.DropDownStyle = ComboBoxStyle.DropDown;
            cbxID.Text = "";
            bNewRecord = true;		
            string sqlStatement;
                sqlStatement = "INSERT INTO login(adName,adPwd) VALUES(" +
               
                    "'" +textBox1.Text + "'," +
                    "'" +textBox2.Text + "')";
                cbxID.Items.Add(textBox1.Text);
                int a = int.Parse(textBox2.Text);
                int b = int.Parse(textBox3.Text);
                if (a == b)
                {

                    // 创建SQL命令
                    SqlCommand sqlcmd = new SqlCommand(
                        sqlStatement,
                        sqlConnection1);
                    sqlDataAdapter1.Fill(dataSet1, "admin");
                    dataGridView1.DataSource = dataSet1.Tables["admin"];
                    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.Error);
                }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            SqlCommand sqlcmd = new SqlCommand(
                "DELETE FROM login WHERE adName=@user",
                sqlConnection1);
            sqlcmd.Parameters.AddWithValue("@user", 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 update_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 login";
            storescommand.Connection = scon;

            sa.SelectCommand = storescommand;

            DataSet ds = new DataSet();

            sa.Fill(ds, "admin");

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

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

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

        private void Admin_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

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