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

📄 usercontrol2.cs

📁 社区管理系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataBaseManagerLibrary;

namespace CaiWuControlLibrary1
{
   
        public partial class UserControl2 : UserControl
    {
        private DataBaseManager DBManager;
        private Int64 dealID, insertDealID;
        private int selectedIndex;

        public UserControl2()
        {
            InitializeComponent();
            String SqlConnectInfo = "server=localhost;database=test;uid=sa;pwd=''";
            DBManager = new DataBaseManager(SqlConnectInfo);
            reset();
        }
        public void setDataBaseManager(DataBaseManager DBManager){
            this.DBManager=DBManager;
        }
      //  public void setUserCategory(String userCategory) {
       //     this.userCategory = userCategory;
       // }
            private bool ErrorMessage(String msg)
            {
                MessageBox.Show(msg);
                return false;
            }
            private void dgvCW_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex == -1 || e.RowIndex >= dgvCW.Rows.Count - 1) return; //选择多行或选择列标签
                showDetailInfo(e.RowIndex);
                selectedIndex = e.RowIndex;
            }
            private void btCWsel_Click_1(object sender, EventArgs e)
        {
            String cmd;
            if (checkInput("Select") == false) return;  //用户输入不合法,此函数主要检查输入的格式错误
            cmd = generateSelectCommand();     //根据输入情况生成相应的命令
            DataTable dataTable = DBManager.ExecuteSelectCommand(cmd);
            if (dataTable == null)
            {  //数据库出错
                MessageBox.Show(DBManager.errorMessage);
                return;
            }
            else if (dataTable.Rows.Count == 0)
            {//没有搜索到对应信息,显示一个空的dgvZK
                dgvCW.DataSource = dataTable;
                return;
            }
            else
            {//至少找到一条信息,将信息显示到dgvZK,并将第一条信息详细列出
                dgvCW.DataSource = dataTable;
                showDetailInfo(0);
            }
        }
            private bool checkInput(String str)
            {
            tnCWnum.Text = tnCWnum.Text.Trim();//将多余的空格去掉  
              if (tnCWnum.Text != "")
            {
                try
                {
                    Int64.Parse(tnCWnum.Text);
                }
                catch
                {
                    MessageBox.Show("号码输入错误!");
                    return false;
                }
            }
            tnCWname.Text = tnCWname.Text.Trim();
            tnCWcount.Text = tnCWcount.Text.Trim();
            if (tnCWcount.Text != "")
            {
                try
                {
                    Int64.Parse(tnCWcount.Text);
                }
                catch
                {
                    MessageBox.Show("数目输入错误!");
                    return false;
                }
            }
           tnCWmon.Text = tnCWmon.Text.Trim();
            if (tnCWmon.Text != "") 
            {
                try
                {
                    float.Parse(tnCWmon.Text);
                }
                catch
                {
                    MessageBox.Show("金额输入错误!");
                    return false;
                }
            }
            tnCWtime.Text = tnCWtime.Text.Trim();
            if (tnCWtime.Text != "")
            {
                try
                {
                    Int64.Parse(tnCWtime.Text);
                }
                catch
                {
                    MessageBox.Show("次数输入错误!");
                    return false;
                }
            }
            if (str == "Insert" || str == "Modify")
            {//以下是在更新或插入时检查输入是否为空
                if (tnCWnum.Text == "") return ErrorMessage("财产编号为空");
                if (tnCWname.Text == "") return ErrorMessage("财产名称为空");
                if (tnCWcount.Text == "") return ErrorMessage("数量为空");
                if (tnCWmon.Text == "") return ErrorMessage("财产金额为空");
                if (tnCWtime.Text == "") return ErrorMessage("使用次数为空");
      
                if (tnCWdate.Text == "") return ErrorMessage("日期为空");

            } 
         
            return true;
        }
        private String generateSelectCommand()
        {
            String cmd = "Select 财产编号,财产名称,数量,财产金额,使用次数,增加日期from 账款信息 where 财产编号='" + tnCWnum.Text + "'";
            bool cmdChanged = false;

            if (tnCWname.Text != "")
                cmd = cmd + " and 财产名称='" + tnCWname.Text + "'";

            if (tnCWcount.Text != "")
                cmd = cmd + " and 数量='" + tnCWcount.Text + "'";

            if (tnCWmon.Text != "")
                cmd = cmd + " and 财产金额='" + tnCWmon.Text + "'";

            if (tnCWtime.Text != "")
                cmd = cmd + " and 使用次数='" + tnCWtime.Text + "'";

           

           if (tnCWdate.Text != "")
               cmd = cmd + "and 日期'" + tnCWdate.Text + "'";



            return cmd;
        }

        private String generateUpdateCommand()
        {
            String cmd = "";
            cmd = String.Format("update 财产信息 set 财产编号='{0}',财产名称='{1}',数量='{2}',财产金额='{3}',使用次数='{4}',", tnCWnum.Text, tnCWname.Text, tnCWcount.Text, tnCWmon.Text, tnCWtime.Text);
            cmd = cmd + String.Format("日期='{0}'" ,tnCWdate.Text);
            return cmd;
        }

        private String generateInsertCommand()
        {
            String cmd = "insert into 财产信息 values(";
            Int64 id = DateTime.Now.ToBinary();
            insertDealID = id;
            cmd = cmd + String.Format("'{0}','{1}','{2}','{3}','{4}'", id, tnCWnum.Text, tnCWname.Text, tnCWcount.Text, tnCWmon.Text);
            cmd = cmd + String.Format("'{0}'", tnCWdate.Text);

            return cmd;
        }

        private String generateDeleteCommand()
        {
            String cmd = String.Format("Delete from 财产信息 where 财产编号='{}'", dgvCW.Rows[selectedIndex].Cells["财产编号"].Value);
            return cmd;
        }
        
        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

            private void btCWdel_Click(object sender, EventArgs e)
            {
                String cmd = generateDeleteCommand();
                int affectRows = DBManager.Execute(cmd);
                if (affectRows == -1)
                {  //数据库出错
                    MessageBox.Show(DBManager.errorMessage);
                    return;
                }
                else if (affectRows == 0)
                {
                    MessageBox.Show("此数据已被其他登陆用户删除");
                }
                if (selectedIndex == dgvCW.Rows.Count - 2)
                {
                    dgvCW.Rows.Remove(dgvCW.Rows[selectedIndex]);
                    selectedIndex--;
                    if (selectedIndex == -1)
                    {//若dgvZK没有数据则重置界面
                        reset();
                        return;
                    }
                }
                else dgvCW.Rows.Remove(dgvCW.Rows[selectedIndex]);
                showDetailInfo(selectedIndex);
            }

            private void btCWadd_Click(object sender, EventArgs e)
            {
                String cmd;
                if (checkInput("Insert") == false) return;//用户输入错误

                cmd = generateSelectCommand();    //首先查找数据库中有没有相同内容的
                DataTable dataTable = DBManager.ExecuteSelectCommand(cmd);
                if (dataTable == null)
                {  //数据库出错
                    MessageBox.Show(DBManager.errorMessage);
                    return;
                }
                else if (dataTable.Rows.Count > 0)
                {
                    MessageBox.Show("数据库中已存在相同信息");
                    return;
                }

                //将信息添加到数据库
                cmd = generateInsertCommand();    //根据输入情况生成相应的命令
                MessageBox.Show(cmd);
                int affectedRows = DBManager.Execute(cmd);

                if (affectedRows == -1)
                {  //数据库出错
                    MessageBox.Show(DBManager.errorMessage);
                    return;
                }
                else if (affectedRows == 0)
                {//
                    MessageBox.Show("插入失败");
                    return;
                }
                else MessageBox.Show("插入成功");
                {//插入成功,
                    showDetailInfo(selectedIndex);//显示插入前的一行

                    /*DataRow row = new DataRow();
                    row.set
                    row.ItemArray={insertDealID,tnCWnum.Text};
                    dgvZKCell[] cell = new dgvZKCell[6];
                    for (int i = 0; i < 6; i++) cell[i] = new dgvZKCell();
                    row.Cells = cell;
                    row.Cells[0].Value=insertDealID;
                    row.Cells[1].Value=tnCWnum.Text;
                    row.Cells[2].Value=tnCWname.Text;
                    row.Cells[3].Value=int.Parse(tnCWcount.Text);
                    row.Cells[4].Value=tnCWmon.Text;
                    row.Cells[5].Value=cbZKstat.Text;
                    row.Cells[6].Value=dtZKdate.Text;

                    dgvZK.Rows.Add(row);
                    int idx = dgvZK.Rows.IndexOf(row);
                    selectedIndex = idx;
                    showDetailInfo(idx);
                  */
                }
            }

            private void btCWalter_Click(object sender, EventArgs e)
            {
                String cmd;
                if (checkInput("Modify") == false) return;  //用户输入不合法
                cmd = generateUpdateCommand();    //根据输入情况生成相应的命令

                int affectRows = DBManager.Execute(cmd);
                if (affectRows == -1)
                {  //数据库出错
                    MessageBox.Show(DBManager.errorMessage);
                    return;
                }
                else if (affectRows == 0)
                {//没有搜索到对应信息
                    MessageBox.Show("信息可能已被其他用户删除");
                    return;
                }
                else
                {//成功修改,更新dgvZK
                    DataGridViewRow row = dgvCW.Rows[selectedIndex];
                    row.Cells["财产编号"].Value = tnCWnum.Text;
                    row.Cells["财产名称"].Value = tnCWname.Text;
                    row.Cells["数量"].Value = tnCWcount.Text;
                    row.Cells["财产金额"].Value = tnCWmon.Text;
                    row.Cells["使用次数"].Value = tnCWtime.Text;
                    row.Cells["日期"].Value = tnCWdate.Text;
                }
            }

            private void showDetailInfo(int idx)
            {
                if (idx < 0) return;
                selectedIndex = idx;
                dealID = (Int64)dgvCW.Rows[idx].Cells["财产编号"].Value;//得到用户选择的行的主键
                DataTable dt = DBManager.ExecuteSelectCommand(String.Format("select * from 财产信息 where 财产编号='{}'", dealID));
                if (dt == null)
                {//数据库错误
                    MessageBox.Show(DBManager.errorMessage);
                    dealID = 0;
                    return;
                }
                else if (dt.Rows.Count == 0)
                {  //未找到此记录
                    MessageBox.Show("此记录可能已被其他用户删除");
                    dealID = 0;
                    return;
                }

                tnCWnum.Text = (Int32)dt.Rows[0]["财产编号"] + "";
                tnCWname.Text = (String)dt.Rows[0]["财产名称"];
                tnCWcount.Text = (Int32)dt.Rows[0]["数量"] + "";  //用这种方式将数字转换成字符串
                tnCWmon.Text = (Double)dt.Rows[0]["财产金额"] + "";
                tnCWtime.Text = (Int64)dt.Rows[0]["使用次数"] + "";
               
                tnCWdate.Text = (String)dt.Rows[0]["日期"];


                btCWalter.Enabled = true;//此时可以操作修改、删除数据
                btCWdel.Enabled = true;
            }
            public void visilize()
            {
                reset();    //将各个控件置为初始状态
                this.Visible = true;
            }

            public void disVisilize()
            {
                this.Visible = false;
            }

            private void reset()
            {
                dealID = 0;
                selectedIndex = -1;
                btCWalter.Enabled = false;
                btCWdel.Enabled = false;
                btCWpri.Enabled = false;
                tnCWnum.Text = "";     //清空TextBox及ComboBox
                tnCWname.Text = "";
                tnCWcount.Text = "";
                tnCWmon.Text = "";
                tnCWtime.Text = "";
                 tnCWdate.Text = "";

            }

            private void dgvCW_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {

            }
    }
}

⌨️ 快捷键说明

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