FrmGetRosterSQLServer.cs 6.52 KB
using GeneralLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SunVoteARSPPT
{
    internal partial class FrmGetRosterSQLServer : Form
    {
        internal FrmGetRosterSQLServer()
        {
            InitializeComponent();
        }

        DBOper DBOperation = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            DBOperation = new DBOper();
            cboMode.Items.Clear();
            cboMode.Items.Add("SQL Server");
            cboMode.Items.Add("Windows");
            //cboMode.Items.Add("集成的Windows");
            cboMode.SelectedIndex = 0;

            btnSelect.Enabled = false;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            DBOperation.CloseConn();
        }

        private void cboMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUid.Enabled = txtPwd.Enabled = (cboMode.SelectedIndex < 2);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Stopwatch t = Stopwatch.StartNew();
            Action<object> a = (obj) =>
            {
                try
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        this.Cursor = Cursors.WaitCursor;
                        btnConnect.Enabled = cboMode.Enabled = false;

                        dgvClass.DataSource = null;
                        dgvData.DataSource = null;

                        if (cboMode.SelectedIndex == 0)
                            DBOperation.InitConnStrSQLServerSQL(txtServer.Text, txtDataBase.Text, txtUid.Text, txtPwd.Text);
                        if (cboMode.SelectedIndex == 1)
                            DBOperation.InitConnStrSQLServerWin(txtServer.Text, txtDataBase.Text, txtUid.Text, txtPwd.Text);
                        else if (cboMode.SelectedIndex == 2)
                            DBOperation.InitConnStrSQLServerWinSSPI(txtServer.Text, txtDataBase.Text);
                    });

                    if (DBOperation.OpenConn())
                    {
                        this.Invoke((MethodInvoker)delegate
                        {
                            ShowClass();
                        });
                    }
                    //this.Text = t.ElapsedMilliseconds + "";
                }
                catch (Exception ex)
                {
                    SystemLog.WriterLog(ex);
                }
                this.Invoke((MethodInvoker)delegate
                {
                    this.Cursor = Cursors.Default;
                    btnConnect.Enabled = cboMode.Enabled = true;
                });
            };
            a.BeginInvoke(this, ar => a.EndInvoke(ar), null);
        }
        private void ShowClass()
        {
            try
            {
                dgvClass.DataSource = null;

                string sql = "select * from HZ_Base_Station";
                DataSet ds = DBOperation.OpenDataSet(sql);
                if ((ds == null) || (ds.Tables.Count < 1))
                    return;

                var tab = ds.Tables[0];
                dgvClass.DataSource = tab;

                for (int i = 0; i < tab.Rows.Count; i++)
                {
                    dgvClass.Rows[i].Tag = tab.Rows[i]["bjid"] + "";
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex, "ShowClass");
            }
        }

        DataTable DataTableRoster = null;
        private void btnNameList_Click(object sender, EventArgs e)
        {
            try
            {
                dgvData.DataSource = null;
                if (dgvClass.SelectedCells.Count < 1)
                    return;

                string classID = dgvClass.SelectedCells[0].OwningRow.Tag + "";
                string sql = "select HZ_Student_Keybroad.KID as KeyID,HZ_Student.xm as SName,HZ_Student.kh as SKH from" +
                " HZ_Student_Keybroad left join HZ_Student on HZ_Student_Keybroad.sid=HZ_Student.sid" +
                " where HZ_Student.bjid='" + classID + "'" +
                " order by HZ_Student.kh";
                DataSet ds = DBOperation.OpenDataSet(sql);
                if ((ds == null) || (ds.Tables.Count < 1))
                    return;

                DataTableRoster = ds.Tables[0];
                dgvData.DataSource = ds.Tables[0];

                int rows = DataTableRoster.Rows.Count;
                int cols = DataTableRoster.Columns.Count;

                btnSelect.Enabled = ((rows > 0) && (cols > 0));
            }
            catch (Exception ex)
            {
                SystemLog.WriterLog(ex);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            int rows = DataTableRoster.Rows.Count;
            int cols = DataTableRoster.Columns.Count;

            if ((rows < 1) || (cols < 1))
                return;

            if (MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString("rbSunVoteARS", "WarningInfo1", "此操作将清空现有名单!是否继续?"),
                        GlobalInfo.SysLanguage.LPT.ReadString("rbSunVoteARS", "Warning", "警告"), MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.No)
            {
                return;
            }

            Dictionary<string, string> DicCol = new Dictionary<string, string>();
            DicCol.Add("KeyID", "键盘编号");
            DicCol.Add("SName", "姓名");
            DicCol.Add("SKH", "考号");            
            string[,] aryData = new string[rows + 1, cols];
            for (int n = 0; n < DataTableRoster.Columns.Count; n++)
            {
                string colName = DataTableRoster.Columns[n].ColumnName;
                if (DicCol.ContainsKey(colName))
                    colName = DicCol[colName];
                aryData[0, n] = colName;
            }
            for (int i = 0; i < DataTableRoster.Rows.Count; i++)
            {
                for (int n = 0; n < DataTableRoster.Columns.Count; n++)
                {
                    int row = i + 1;
                    aryData[row, n] = DataTableRoster.Rows[row][n] + "";
                }
            }
            this.Close();

            new FrmVoterList().ShowImportList("", aryData);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}