C#、SQL 查询+修改+删除+更新工具

本文介绍了一款基于C#的查询工具,详细解析了登录模块、选项块、查询导出模块及数据库操作模块的源代码。该工具涵盖登录验证、查询配置读取、数据查询与导出等功能,适用于多种数据库操作场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.登陆源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Threading;

namespace QueryingTool.V1._00
{
    public partial class Flogin : Form
    {
        public Flogin()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void Flogin_Load(object sender, EventArgs e)
        {
            this.textBox1.Focus();//焦点
            this.textBox1.SelectAll();//选中
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                if (textBox1.Text != "" && textBox1.Text != null)
                    textBox2.Focus();
                else
                {
                    textBox1.Focus();
                    textBox1.SelectAll();
                }
            }
        }

        private void button1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox1.Text == null)
            {
                textBox1.Focus();
                textBox1.SelectAll();
            }
            else if (textBox2.Text == "" && textBox2.Text== null)
            {
                textBox2.Focus();
                textBox2.SelectAll();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox1.Text == null)
            {
                textBox1.Focus();
                textBox1.SelectAll();
            }
            else if (textBox2.Text == "" && textBox2.Text == null)
            {
                textBox2.Focus();
                textBox2.SelectAll();
            }
            else
            {
                SqlServer_Use MyLoginQuerying = new SqlServer_Use("GPUSERVER", "E_LoginInfo", "sa", "adminsystem", "usp_QueryUser_Pwd_Info");
                if ((MyLoginQuerying.Querying_Login_UserName_Pwd(textBox1.Text.Trim(), textBox2.Text.Trim())) == true)
                {
                    this.Hide();//隐身登陆界面
                    if (textBox1.Text.ToUpper() == "ADMIN")
                    {
                        Boot By = new Boot();
                        By.StartPosition = FormStartPosition.CenterScreen;
                        By.Show();
                    }
                    else
                    {
                        Querying Qy = new Querying();
                        Qy.StartPosition = FormStartPosition.CenterScreen;
                        Qy.Show();
                    }        
                }
                else
                {
                    MessageBox.Show("输入的帐户或密码信息错误!!","系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    textBox2.Text = "";
                    textBox1.Text = "";
                    textBox1.Focus();
                    textBox1.SelectAll();
                }
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (textBox2.Text != "" && textBox2.Text != null)
             //   button1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(1);
        }
    }
}
 

2.选项块源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QueryingTool.V1._00
{
    public partial class Boot : Form
    {
        public Boot()
        {
            InitializeComponent();
        }

        private void Boot_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(1);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                this.Hide();
                Querying Mq = new Querying();
                Mq.StartPosition = FormStartPosition.CenterScreen;//居中显示
                Mq.Show();
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                this.Hide();
                ReworkItems Rq = new ReworkItems();
                Rq.StartPosition = FormStartPosition.CenterScreen;//居中显示
                Rq.Show();
            }
        }

    }
}
 

3.查询导出模块源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Web;

namespace QueryingTool.V1._00
{
    public partial class Querying : Form
    {
        public struct Config
        {
            public String Querying_ItemName;
            public String Querying_DATABASE;
            public String Querying_DATATABLE;
            public List<String> KeyItemName;   
        }

        List<Config> StoredConfigInfo = new List<Config>();
        public String QDATABASE;
        public String DATATABLE;
        public Querying()
        {
            InitializeComponent();
        }

        private void Querying_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(1);
        }

        private void Querying_Load(object sender, EventArgs e)
        {
            if (ConfigRead_Stored() == true)
            {

            }
            else
            {
                MessageBox.Show("Query.Config配置文件读取出错!!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        public void Errlog(String Err_Str)//生成Log记录
        {
            FileStream fs = new FileStream("Err.log",FileMode.Create,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Err_Str);
            sw.Close();
            fs.Close();
        }

        public Boolean ConfigRead_Stored()
        {
            Boolean Flag = false;
            try
            {
                FileStream fs = new FileStream("Query.Config",FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                String Temp = String.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    if (Temp[0] != '#')
                    {
                        String[] ArrayStr = Temp.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (ArrayStr.Length >= 4)
                        {
                            Config My_Cfg = new Config();
                            My_Cfg.Querying_ItemName = ArrayStr[0].Trim();//查询项目名称
                            My_Cfg.Querying_DATABASE = ArrayStr[1].Trim();//查询数据库
                            My_Cfg.Querying_DATATABLE = ArrayStr[2].Trim();//查询数据表
                            List<String> KeyItemName = new List<String>();
                            for (int n = 3; n < ArrayStr.Length; n++)
                                KeyItemName.Add(ArrayStr[n].Trim());
                            My_Cfg.KeyItemName=KeyItemName;//添加选择关键项
                            comboBox1.Items.Add(ArrayStr[0].Trim());
                            StoredConfigInfo.Add(My_Cfg);
                        }
                    }
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                this.Errlog(ex.ToString());
            }
            return Flag;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text != "" && comboBox1.Text != null)
            {
                foreach (Config cc in StoredConfigInfo)
                {
                    if (cc.Querying_ItemName == comboBox1.Text.Trim())
                    {
                        this.QDATABASE = cc.Querying_DATABASE;
                        this.DATATABLE = cc.Querying_DATATABLE;
                        foreach (String ii in cc.KeyItemName)
                            comboBox2.Items.Add(ii);
                        comboBox1.Enabled = false;
                        comboBox2.Enabled = true;
                        comboBox2.Focus();
                        button3.Enabled = true;
                    }
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.Text != null && comboBox2.Text != "")
            {
                if (comboBox2.Text == "*")
                {
                    comboBox2.Enabled = false;
                    button1.Enabled = true;
                    button1.Focus();
                }
                else
                {
                    comboBox2.Enabled = false;
                    textBox1.Enabled = true;
                    textBox1.Focus();
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                textBox1.Enabled = false;
                button1.Enabled = true;
                button1.Focus();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String Stored_Procedure = String.Empty;
            if (comboBox2.Text == "*")
                Stored_Procedure = "SELECT * FROM " + this.DATATABLE;
            else if (comboBox2.Text == "Date_Time")
                Stored_Procedure = "SELECT * FROM " + this.DATATABLE + " WHERE Date_Time like " + @"'" + textBox1.Text.Trim() + @"%'";
            else
                Stored_Procedure = "SELECT * FROM " + this.DATATABLE + " WHERE " + comboBox2.Text.Trim() + "=" + @"'" + textBox1.Text.Trim() + @"'";
            SqlServer_Use My_Querying = new SqlServer_Use("GPUSERVER",this.QDATABASE,"sa","adminsystem",Stored_Procedure);
            if (My_Querying.DataBaseQuerying_SpecifyTheData(dataGridView1, label1) == false)
            {
                MessageBox.Show("数据查询出错!!", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (dataGridView1.Rows.Count > 1)
                {
                    button1.Enabled = false;
                    button2.Enabled = true;
                    button3.Enabled = true;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Reset(1);
        }

        private void Reset(int Items)
        {
            if (Items == 1)
            {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                textBox1.Text = "";
                textBox1.Enabled = false;
                comboBox2.Items.Clear();
                comboBox2.Text = "";
                comboBox1.Text = "";
                comboBox1.Enabled = true;
                comboBox1.Focus();
                dataGridView1.DataSource = null;//清空数据表
            }
        }

        public static Boolean ExportExcel_DefineTheForm(string fileName, DataGridView myDGV)//按定义的表格导出数据
        {
            Boolean Flag=false;
            if (myDGV.Rows.Count > 0)//存在测试数据
            {
                String saveFileName = "";
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = fileName;
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0)
                {
                    return true;
                }
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的系统未安装Excel", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
                //写入标题
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
                }
                //写入数值
                for (int r = 0; r < myDGV.Rows.Count; r++)
                {
                    for (int i = 0; i < myDGV.ColumnCount; i++)
                    {
                        worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                        Flag = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message, "系统提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                        return false;
                    }
                }
                xlApp.Quit();
                GC.Collect();//强行销毁
                MessageBox.Show("导出文件成功", "提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("报表为空,无表格需要导出", "提示", MessageBoxButtons.OK);
                return true;
            }
            return Flag;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            String FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            if (ExportExcel_DefineTheForm(FileName, this.dataGridView1) == true)
            {
                button1.Enabled = false;
                button3.Enabled = false;
                comboBox1.Enabled = true;
            }
        }
    }
}

3.删除\更新\添加

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace QueryingTool.V1._00
{
    public partial class ReworkItems : Form
    {
        public struct Config
        {
            public String Item_Name;
            public String RDATABASE;
            public String RDATATABLE;
            public List<String> KeyItemName;
        }

        List<Config> StoredConfigInfo = new List<Config>();
        public String RDATABASE;
        public String RDATATABLE;
        public List<ComboBox> MyCb=new List<ComboBox>();
        public List<TextBox> MyTb=new List<TextBox>();
        public List<Button> MyBt=new List<Button>();
        public ReworkItems()
        {
            InitializeComponent();
        }

        private void ReworkItems_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(1);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                InitControl(1);
                if (ReadCfg("Rework.Config") == true)
                {
                    comboBox1.Enabled = true;
                }
                else
                {
                    label10.Text = "读取Rework.Config配置出错!!";
                    label10.ForeColor = Color.Red;
                }
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                InitControl(1);
                comboBox4.Enabled = true;
                comboBox4.Items.Add("更新MAC前缀信息");
                comboBox4.Items.Add("更新老化时间");
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true)
            {
                InitControl(1);
                comboBox6.Items.Add("增加MAC前缀信息");
                comboBox6.Items.Add("增加老化时间数据");
            }
        }

        private void ReworkItems_Load(object sender, EventArgs e)
        {
            MyCb.Add(comboBox1);
            MyCb.Add(comboBox2);
            MyCb.Add(comboBox3);
            MyCb.Add(comboBox4);
            MyCb.Add(comboBox5);
            MyCb.Add(comboBox6);
            MyCb.Add(comboBox7);
            MyCb.Add(comboBox8);
            MyCb.Add(comboBox9);

            MyTb.Add(textBox1);
            MyTb.Add(textBox2);
            MyTb.Add(textBox3);
            MyTb.Add(textBox4);

            MyBt.Add(button1);
            MyBt.Add(button2);
            MyBt.Add(button3);
        }

        public Boolean ReadCfg(String FileName)
        {
            Boolean Flag = false;
            try
            {
                FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                String Temp = String.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    if (Temp[0] != '#')
                    {
                        String[] Array_Str = Temp.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        Config MyC = new Config();
                        MyC.Item_Name = Array_Str[0].Trim();
                        MyC.RDATABASE = Array_Str[1].Trim();
                        MyC.RDATATABLE = Array_Str[2].Trim();
                        List<String> TemL = new List<string>();
                        for (int n = 3; n < Array_Str.Length; n++)
                        {
                            TemL.Add(Array_Str[n].Trim());
                        }
                        MyC.KeyItemName = TemL;
                        StoredConfigInfo.Add(MyC);
                        comboBox1.Items.Add(Array_Str[0].Trim());
                    }
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                this.Errlog(ex.ToString());
                Flag = false;
            }
            return Flag;
        }

        public void Errlog(String Err_Str)
        {
            FileStream fs = new FileStream("Err.log",FileMode.Create,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(sw);
            sw.Close();
            fs.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox2.Enabled = true;
            comboBox2.Focus();
            comboBox2.Text = "";
            comboBox1.Enabled = false;
            label10.Text = "";

            foreach (Config ss in StoredConfigInfo)
            {
                if (comboBox1.Text == ss.Item_Name.Trim())
                {
                    this.RDATABASE = ss.RDATABASE;
                    this.RDATATABLE = ss.RDATATABLE;
                    foreach (String ii in ss.KeyItemName)
                        comboBox2.Items.Add(ii);
                }
            }
        }

        public void InitControl(int Items)
        {
            int Item = 0;
            if (radioButton1.Checked == true && Items == 1) Item = 1;
            else if (radioButton2.Checked == true && Items == 1) Item = 4;
            else if (radioButton3.Checked == true && Items == 1) Item = 6;
            int ss = 1;
            foreach (ComboBox cc in MyCb)
            {
                if (Item != ss)
                {
                    cc.Items.Clear();
                    cc.Text = "";
                    cc.Enabled = false;
                }
                else
                {
                    cc.Text = "";
                    cc.Enabled = true;
                }
                ss++;
            }
            foreach (TextBox tt in MyTb)
            {
                tt.Text = "";
                tt.Enabled = false;
            }
            foreach (Button bb in MyBt)
                bb.Enabled = false;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Enabled = false;
            textBox1.Text = "";
            textBox1.Enabled = true;
            textBox1.Focus();
            label10.Text = "";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (textBox1.Text != "" && textBox1.Text != null)
                {
                    button1.Enabled = true;
                    textBox1.Enabled = false;
                    button1.Focus();
                }
                else
                {
                    label10.Text = "删除栏->录入关键值项内容不能为空!!";
                    label10.ForeColor = Color.Red;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String Stored_Procedure=String.Empty;
            if (comboBox2.Text.Trim() == "Date_Time")
                Stored_Procedure = "DELETE " + this.RDATATABLE + " WHERE " + comboBox2.Text.Trim() + " like " + @"'" + textBox1.Text.Trim() + @"%'";
            else
                Stored_Procedure = "DELETE " + this.RDATATABLE + " WHERE " + comboBox2.Text.Trim() + "=" + @"'" + textBox1.Text.Trim() + @"'";
            SqlServer_Use My_Del = new SqlServer_Use("GPUSERVER",this.RDATABASE,"sa","adminsystem",Stored_Procedure);
            if (My_Del.TestData_Operate()== true)
            {
                label10.ForeColor = Color.Green;
                label10.Text = My_Del.ResString;
                InitControl(1);
            }
            else
            {
                label10.ForeColor = Color.Red;
                label10.Text = My_Del.ResString;
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            label10.Text = "";
            comboBox3.Items.Clear();
            if (comboBox4.Text.Trim() == "更新MAC前缀信息")
            {
                this.RDATABASE = "E_AgeTestData";
                this.RDATATABLE = "Mac_Section";
                comboBox3.Items.Add("TradeName");
                comboBox3.Items.Add("Address_Section");
            }
            else if (comboBox4.Text.Trim() == "更新老化时间")
            {
                this.RDATABASE = "E_AgeTestData";
                this.RDATATABLE = "Agin_SetTime";
                comboBox3.Items.Add("Trade_Name");
                comboBox3.Items.Add("AginTime");
            }
            comboBox4.Enabled = false;
            comboBox3.Enabled = true;
            comboBox3.Focus();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                comboBox8.Items.Clear();
                if (textBox2.Text != null && textBox2.Text != "")
                {
                    String Stored_Procedure=String.Empty;
                    Stored_Procedure="SELECT "+comboBox7.Text.Trim()+" FROM "+this.RDATATABLE;
                    SqlServer_Use MyRKy = new SqlServer_Use("GPUSERVER", this.RDATABASE, "sa", "adminsystem", Stored_Procedure);
                    if (MyRKy.ReadKeyValues() == true)
                    {
                        foreach (String kk in MyRKy.KeyValues)
                            comboBox8.Items.Add(kk.Trim());
                        textBox2.Enabled = false;
                        comboBox8.Enabled = true;
                        comboBox8.Focus();
                    }
                    else
                    {
                        label10.ForeColor = Color.Red;
                        label10.Text = MyRKy.ResString;
                    }
                }
                else
                {
                    label10.ForeColor = Color.Red;
                    label10.Text = "更新栏->录入设置值内容能不为空!!";
                }
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
            textBox2.Text = "";
            textBox2.Focus();
            comboBox3.Enabled = false;
            if (comboBox3.Text.Trim() == "TradeName")
            {
                comboBox7.Text = "Address_Section";
            }
            else if (comboBox3.Text.Trim() == "Address_Section")
            {
                comboBox7.Text = "TradeName";
            }
            else if (comboBox3.Text.Trim() == "Trade_Name")
            {
                comboBox7.Text = "AginTime";
            }
            else if (comboBox3.Text.Trim() == "AginTime")
            {
                comboBox7.Text = "Trade_Name";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String Stored_Procedure = String.Empty;
            if (comboBox3.Text == "AginTime")
            {
                Stored_Procedure = "UPDATE " + this.RDATATABLE + " SET " + comboBox3.Text.Trim() + "=" + textBox2.Text.Trim() + " where " + comboBox7.Text.Trim() + "=" + @"'" + comboBox8.Text.Trim() + @"'";
            }
            else if (comboBox7.Text == "AginTime")
            {
                Stored_Procedure = "UPDATE " + this.RDATATABLE + " SET " + comboBox3.Text.Trim() + "=" + @"'" + textBox2.Text.Trim() + @"'" + " where " + comboBox7.Text.Trim() + "=" + comboBox8.Text.Trim();
            }
            else
            {
                Stored_Procedure = "UPDATE " + this.RDATATABLE + " SET " + comboBox3.Text.Trim() + "=" + @"'" + textBox2.Text.Trim() + @"'" + " where " + comboBox7.Text.Trim() + "=" + @"'" + comboBox8.Text.Trim() + @"'";
            }
            SqlServer_Use MyUp = new SqlServer_Use("GPUSERVER", this.RDATABASE, "sa", "adminsystem", Stored_Procedure);
            if (MyUp.TestData_Operate() == true)
            {
                label10.ForeColor = Color.Green;
                label10.Text = MyUp.ResString;
                InitControl(1);
            }
            else
            {
                label10.ForeColor = Color.Red;
                label10.Text = MyUp.ResString;
            }
        }

        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox8.Enabled = false;
            button2.Enabled = true;
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox6.Enabled = false;
            label10.Text = "";
            if (comboBox6.Text.Trim() == "增加MAC前缀信息")
            {
                comboBox5.Text="TradeName";
                comboBox9.Text="Address_Section";
                this.RDATABASE = "E_AgeTestData";
                this.RDATATABLE = "Mac_Section";
            }
            else if (comboBox6.Text.Trim() == "增加老化时间数据")
            {
                comboBox5.Text = "Trade_Name";
                comboBox9.Text = "AginTime";
                this.RDATABASE = "E_AgeTestData";
                this.RDATATABLE = "Agin_SetTime";
            }
            textBox3.Text = "";
            textBox3.Enabled = true;
            textBox3.Focus();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                label10.Text = "";
                if (textBox3.Text != "" && textBox3.Text != null)
                {
                    textBox3.Enabled = false;
                    textBox4.Text = "";
                    textBox4.Enabled = true;
                    textBox4.Focus();
                }
                else
                {
                    label10.ForeColor = Color.Red;
                    label10.Text = "添加栏->录入键值_1内容能不为空!!";
                }
            }
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                label10.Text = "";
                if (textBox4.Text != "" && textBox4.Text != null)
                {
                    button3.Enabled = true;
                    textBox4.Enabled = false;
                    button3.Focus();
                }
                else
                {
                    label10.ForeColor = Color.Red;
                    label10.Text = "添加栏->录入键值_2内容能不为空!!";
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            label10.Text = "";
            String Stored_Procedure = String.Empty;
            if (comboBox9.Text.Trim() == "AginTime")
            {
                Stored_Procedure="INSERT INTO "+this.RDATATABLE+@" VALUES("+@"'"+textBox3.Text.Trim()+@"'"+","+textBox4.Text.Trim()+@")";
            }
            else
            {
                Stored_Procedure = "INSERT INTO " + this.RDATATABLE + @" VALUES(" + @"'" + textBox3.Text.Trim() + @"'" + "," + @"'"+textBox4.Text.Trim() + @"'"+@")";
            }
            SqlServer_Use MyIn = new SqlServer_Use("GPUSERVER", this.RDATABASE, "sa", "adminsystem", Stored_Procedure);
            if (MyIn.TestData_Operate() == true)
            {
                label10.ForeColor = Color.Green;
                label10.Text = MyIn.ResString;
                InitControl(1);
            }
            else
            {
                label10.ForeColor = Color.Red;
                label10.Text = MyIn.ResString;
            }
        }
    }
}
 

4.共用数据库连接模块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.Web;
using System.Drawing;

namespace QueryingTool.V1._00
{
    public class SqlServer_Use:Querying
    {
        private String SERVER;
        private String DATABASE;
        private String UID;
        private String PWD;
        private String Stored_Procedure;
        public String ResString;
        public List<String> KeyValues;

        public SqlServer_Use(String SERVER, String DATABASE, String UID, String PWD, String Stored_Procedure)
        {
            this.SERVER = SERVER;
            this.DATABASE = DATABASE;
            this.UID = UID;
            this.PWD = PWD;
            this.Stored_Procedure = Stored_Procedure;
        }

        public Boolean ReadKeyValues()//读取数据库键值
        {
            Boolean Flag = false;
            String Conn_Str = String.Empty;
            Conn_Str = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
            SqlConnection conn = new SqlConnection(Conn_Str);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(this.Stored_Procedure, conn);//设置存储过程
                SqlDataReader reader = cmd.ExecuteReader();//读取执行过程
                List<String> Temp = new List<string>();
                while (reader.Read())
                {
                    Temp.Add(reader[0].ToString());
                }
                this.KeyValues = Temp;
                //ResString = "执行完成!!";
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                this.Errlog(ex.ToString());
                ResString = "连接" + this.SERVER + " 数据库出错!!";
            }
            return Flag;
        }
        public Boolean TestData_Operate()
        {
            Boolean Flag = false;
            String Conn_Str = String.Empty;
            Conn_Str = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
            SqlConnection conn = new SqlConnection(Conn_Str);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(this.Stored_Procedure,conn);//设置存储过程
                SqlDataReader reader = cmd.ExecuteReader();//读取执行过程
                ResString = "执行完成!!";
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                this.Errlog(ex.ToString());
                ResString = "连接" + this.SERVER + " 数据库出错!!";
            }
            return Flag;
        }

        public Boolean Querying_Login_UserName_Pwd(String UserName, String UserPwd)//查询登录信息
        {
            Boolean Flag = false;
            String Conn_Str = String.Empty;
            Conn_Str = "server=" + this.SERVER + ";database=" + this.DATABASE + ";uid=" + this.UID + ";pwd=" + this.PWD;
            SqlConnection conn = new SqlConnection(Conn_Str);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(this.Stored_Procedure,conn);//设置存储过程
                cmd.CommandType = CommandType.StoredProcedure;//启动数据库存储过程
                cmd.Parameters.Add("@User_Name", UserName);
                cmd.Parameters.Add("@User_Pwd", UserPwd);
                cmd.Parameters.Add("@rs",1);//设置返回值
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//启动输出返回
                cmd.ExecuteScalar();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    conn.Close();
                    Flag = true;
                }
                else
                {
                    conn.Close();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                this.Errlog(ex.ToString());
                Flag = false;
            }
            return Flag;
        }

        public Boolean DataBaseQuerying_SpecifyTheData(DataGridView aa,Label Querying_IndexNumber)//查询测试数据
        {
            Boolean Flag = false;
            String Conn_Str = "server="+this.SERVER+";database="+this.DATABASE+";uid="+this.UID+";pwd="+this.PWD;
            SqlConnection conn = new SqlConnection(Conn_Str);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(this.Stored_Procedure, conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "cs");
                aa.DataSource = ds.Tables[0];
                aa.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                aa.ReadOnly = true;
                aa.DefaultCellStyle.SelectionBackColor = Color.GreenYellow;
                Querying_IndexNumber.Text = "已查询到数据:" + (aa.Rows.Count - 1).ToString() + "条";
                Flag = true;
            }
            catch (Exception ex)
            {
                this.Errlog(ex.ToString());
                Flag = false;
            }
            finally
            {
                conn.Close();
            }
            return Flag;
        }
        /*public void Errlog(String Err_Str)
        {
            FileStream fs = new FileStream("Err.log",FileMode.Create,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Err_Str);
            sw.Close();
            fs.Close();
        }*/
    }
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值