c# From 查询检查SQL SERVER数据工具

68 篇文章 1 订阅

1.C# SOURCE CODE:

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.Data.SqlClient;
using System.IO;
using System.Threading;
using System.Timers;
using System.Web;
//using System.Timers;

namespace AginTestScanProgram
{
    public partial class Form1 : Form
    {
        public static String[] KeyValue = { "NULL","NULL","NULL","NULL","NULL","NULL"};
        public static String[] Smple = { "SNLEN", "SERVER", "DATABASE", "UID", "PWD", "StoredProcedure" };
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CfgProcess MyCfg = new CfgProcess("config.ini");//读取配置
            timer1.Enabled = true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        public class CfgProcess : Form1 {
            public CfgProcess(String CfgFileName)
            {
                try
                {
                    FileStream fs = new FileStream(CfgFileName,FileMode.Open,FileAccess.Read);
                    StreamReader sr = new StreamReader(fs,Encoding.Default);
                    String Temp = String.Empty;
                    while ((Temp = sr.ReadLine()) != null)
                    {
                        String[] Array = Temp.Split(new String[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                        int n = 0;
                        foreach (String str in Smple)
                        {
                            if (Array[0].Trim() == str)
                            {
                                KeyValue[n] = Array[1].Trim();
                            }
                            n++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(CfgFileName+" Config Read Error:\n"+ex.ToString());
                    Environment.Exit(1);
                }
            }      
        }

        public class SqlServerDataBaseProcess:Form1{
            private String SERVER;
            private String DATABASE;
            private String UID;
            private String PWD;
            private String StoredProcedure;

            public SqlServerDataBaseProcess(String SERVER, String DATABASE, String UID, String PWD, String StoredProcedure)//初始化类私有变量
            {
                this.SERVER = SERVER;
                this.DATABASE = DATABASE;
                this.UID = UID;
                this.PWD = PWD;
                this.StoredProcedure = StoredProcedure;
            }

            public bool ConnectionSqlServerDataBaseFindTestData(String Client_SN)
            {
                bool Flag = false;
                SqlConnection conn = new SqlConnection("server="+this.SERVER+";database="+this.DATABASE+";uid="+this.UID+";pwd="+this.PWD);
                conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    MessageBox.Show(this.SERVER+" Server "+this.DATABASE+" DataBase Connection Failed!!");
                    Flag = false;
                    return Flag;
                }
                SqlCommand cmd = new SqlCommand(this.StoredProcedure,conn);//设置命令行启动
                cmd.CommandType = CommandType.StoredProcedure;//启动数据库存储过程
                cmd.Parameters.Add("@Client_SN",Client_SN);//参数1
                cmd.Parameters.Add("@rs",1);//参数2
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//启动输出返回
                cmd.ExecuteScalar();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    conn.Close();
                    Flag = true;
                }
                return Flag;
            }
        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.textBox1.Text.ToString().Length == Convert.ToInt32(KeyValue[0]))
            {
                SqlServerDataBaseProcess MySqlServerDataBaseProcess = new SqlServerDataBaseProcess(KeyValue[1],KeyValue[2],KeyValue[3],KeyValue[4],KeyValue[5]);//连接数据库
                if (MySqlServerDataBaseProcess.ConnectionSqlServerDataBaseFindTestData(textBox1.Text.ToString().Trim()) == true)
                {
                    label3.Text = "PASS";
                    label3.ForeColor = Color.Green;
                    label2.Text = "已完成老化测试!!";
                    label2.ForeColor = Color.Green;
                }
                else
                {
                    label3.Text = "FAIL";
                    label3.ForeColor = Color.Red;
                    label2.Text = "未完成老化测试,请返回进行老化测试!!";
                    label2.ForeColor = Color.Red;
                }
                textBox1.Text = "";
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
 

2.Create Sql Server 查询存储过程 Source code:

USE E_AgeTestData
GO
IF EXISTS(SELECT *FROM SYS.OBJECTS WHERE name='usp_BurnInTest_Data_SN_Found')
DROP PROC usp_BurnInTest_Data_SN_Found
GO

CREATE PROC usp_BurnInTest_Data_SN_Found
    @Client_SN VARCHAR(50),
    @rs int output
as
    SELECT @rs=count(*) FROM BurnInTest_Data WHERE @Client_SN=Client_SN
    IF @@ERROR>0
    BEGIN
        SET @rs=1
    END
    ELSE IF @rs>0
    BEGIN
        SET @rs=0
    END
    ELSE IF @rs<=0
    BEGIN
        SET @rs=1
    END

    RETURN @rs
GO

 

3.效果图:

图2:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值