C#控制台操作SQL server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
using System.Data;
using System.Web;
using System.Windows;
using System.Drawing;
using System.Collections;
using System.Management;
using System.Net.NetworkInformation;

namespace YX_UploadText_Data
{
    public class Program
    {
        public static String[] Cfg_Str = { "SN", "LineBody", "JobNumber", "Product_Name", "CPUID", "MEMORY", "Address_ID1", "Address_ID2","BIOS"};
        public static String[] Cfg_Values=new String[Cfg_Str.Length];
        public static List<String> Cfg_FileStr = new List<String>();
        public static String[] Section_Length=new String[2];
        public static String SN_BarCode = String.Empty;
        public static String SSD_BarCode = String.Empty;
        static int Main(string[] args)
        {
            Title();
            if (args.Length >= 1)
            {
                if (args[0] == @"/Uploading")
                {
                    if (ReadCfg(@"DataFile_Config.Config"))
                    {
                        if (ReadUplodingFile_Str())
                        {
                            if (Insert_Update_TestData(Cfg_FileStr[0], Cfg_FileStr[1], Cfg_FileStr[2], Cfg_FileStr[3],
                                Cfg_FileStr[4], Cfg_FileStr[5], Cfg_FileStr[6], Cfg_FileStr[7], Cfg_FileStr[8], "usp_Update_Insert_NBMainBoard_TestData"))
                            {
                                return 0;
                            }
                            else
                                return 1;
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;
                }
                else if (args[0] == @"/Uploading_NB")
                {
                    if (ReadCfg(@"DataFile_Config.Config"))
                    {
                        if (ReadUplodingFile_Str())
                        {
                            DataBase_NB MyDB_NB = new DataBase_NB("NB_SERVER","E_NETBOOK","sa","adminsystem");
                            if (MyDB_NB.Insert_Upadte_TestDataInDataBase(Cfg_FileStr[0], Cfg_FileStr[1], Cfg_FileStr[2], Cfg_FileStr[3],
                                Cfg_FileStr[4], Cfg_FileStr[5], Cfg_FileStr[6], Cfg_FileStr[7], Cfg_FileStr[8], "usp_Update_Insert_NBMainBoard_TestData"))
                            {
                                return 0;
                            }
                            else
                                return 1;
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;
                }
                else if (args.Length == 3 && args[0] == @"/TieYards_NB")
                {
                    DataBase_NB MyDB_NB = new DataBase_NB("NB_SERVER", "E_NETBOOK", "sa", "adminsystem");
                    if (MyDB_NB.Read_SN_SSD_BarCode_Section_Length(args[1], "usp_ReadBard_Code_Section_Length"))
                    {
                        SN_BarCode = ScanBardCode(MyDB_NB.BarCode_Section, Convert.ToInt32(MyDB_NB.BarCode_Length), "Please Scan SN Bar Code:");//请扫描SN条码
                        if (SN_BarCode != String.Empty)
                        {
                            DataBase_NB MyDB_NB1 = new DataBase_NB("NB_SERVER", "E_NETBOOK", "sa", "adminsystem");
                            if (MyDB_NB1.Read_SN_SSD_BarCode_Section_Length(args[1], "usp_ReadBard_Code_Section_Length"))
                            {
                                SSD_BarCode = ScanBardCode(MyDB_NB1.BarCode_Section, Convert.ToInt32(MyDB_NB1.BarCode_Length), "Please Scan SSD Bar Code:");//请扫描SN条码
                                if (SSD_BarCode != String.Empty)
                                {
                                    DataBase_NB MyDB_NB3 = new DataBase_NB("NB_SERVER", "E_NETBOOK", "sa", "adminsystem");
                                    if (MyDB_NB3.SN_SSD_BarCode_TieYards_Data(SN_BarCode, SSD_BarCode, "usp_Insert_Update_MB_SSD_TieYards_Data"))
                                        return 0;
                                    else
                                        return 1;
                                }
                                else
                                    return 1;
                            }
                            else
                                return 1;
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;
                }
                else if (args.Length == 3 && args[0] == @"/TieYards")
                {
                    if (Read_SN_SSD_Section_Length("usp_ReadBard_Code_Section_Length", args[1]))//判断SN长度和区间是否满足
                    {
                        SN_BarCode = ScanBardCode(Section_Length[0], Convert.ToInt32(Section_Length[1]), "Please Scan SN Bar Code:");//请扫描SN条码
                        if (SN_BarCode != String.Empty)
                        {
                            if (Read_SN_SSD_Section_Length("usp_ReadBard_Code_Section_Length", args[2]))//判断SSD长度和区间是否满足
                            {
                                SSD_BarCode = ScanBardCode(Section_Length[0], Convert.ToInt32(Section_Length[1]), "Please Scan SSD Bar Code:");//请扫描SN条码
                                if (SSD_BarCode != String.Empty)
                                {
                                    if (Insert_Update_SN_SSD_TieYards_Data(SN_BarCode, SSD_BarCode, @"usp_Insert_Update_MB_SSD_TieYards_Data"))
                                    {
                                        return 0;
                                    }
                                    else
                                        return 1;
                                }
                                else
                                    return 1;
                            }
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;

                }
                else if (args.Length == 2 && args[0] == @"/CrConnectNetWork")
                {
                    if (CreateNetWorkName(args[1]))
                        return 0;
                    else
                        return 1;
                }
                else
                    Help();
            }
            Help();
            return 1;
        }

        public static String ScanBardCode(String BardCode_Section,int BardCodeLength,String Prompt_Scan_String)//扫描条码
        {
            Title();
            String Temp = String.Empty;
            while (!Temp.Contains(BardCode_Section)||Temp.Length != BardCodeLength)
            {
                Title();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write(Prompt_Scan_String);
                Console.ResetColor();
                Temp = Console.ReadLine().ToUpper();
            }
            return Temp;
        }
        public static void Title()//设置标题
        {
            Console.Clear();//清屏
            Console.WriteLine("Program Version V1.00");
            Console.WriteLine("Property in Copyright Ferex ShenBo.");
            Console.WriteLine("--------------------------------------------------------------------------------");
        }
        public static void Help()//添加帮助信息
        {
            Title();
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(@"/Uploading    Uploading Test Data In DataBase!!");
            Console.WriteLine(@"/Uploading_NB Uploading Test Data In NB_SERVER DataBase!!");
            Console.WriteLine(@"/TieYards_NB  SN_BarCode And SSD_BarCode TieYards!!");
            Console.WriteLine(@"/TieYards  SN_BarCode And SSD_BarCode TieYards!!");
            Console.WriteLine(@"/CrConnectNetWork Create A Connectable Network");
            Console.WriteLine(@"Example_1:*.exe /Uploading");
            Console.WriteLine(@"Example_1:*.exe /Uploading_NB");
            Console.WriteLine(@"Example_2:*.exe /TieYards 'NV116_NS' 'NV116_SSD'");
            Console.WriteLine(@"Example_3:*.exe /CrConnectNetWork 'ServerName'");
            Console.WriteLine(@"Example_4:*.exe /TieYards_NB 'NV116_NS' 'NV116_SSD'");
            Console.ResetColor();
        }
        public static Boolean ReadUplodingFile_Str()//读取上传数据文件内容并存储于List中
        {
            Boolean Flag = false;
            try
            {
                foreach (String ff in Cfg_Values)
                {
                    String ffs=String.Empty;
                    ffs=ReadFileStr(ff);
                    if (ffs != null)
                    {
                        Cfg_FileStr.Add(ffs);
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Error Reading "+ff+" File Contents");
                        Console.ResetColor();
                        Flag = false;
                        return Flag;
                    }
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                Flag = false;
            }
            return Flag;
 
        }
        public static String ReadFileStr(String FileName)//读取文件内容
        {
            try
            {
                FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                String Temp = String.Empty;
                Temp = sr.ReadLine();
                sr.Close();
                return Temp;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
            }
            return null;

        }

        public static Boolean CreateNetWorkName(String ServerName)
        {
            Boolean Flag = false;
            Ping p = new Ping();//创建Ping对象
            try
            {
                PingReply pr = p.Send(ServerName);//向指定IP或者主机名的计算机发送ICMP协议的ping数据
                if (pr.Status == IPStatus.Success)//如果ping成功
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Network Connection Successful!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Network Connection Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                String FileName = DateTime.Now.ToString("yyyyMMddHHmm" + @".txt");
                WErrlog(FileName, ex.ToString());
                Console.WriteLine("Network Connection Failed!!");
                Console.ResetColor();
                Flag = false;
            }
            return Flag;
        }
        public static Boolean ReadCfg(String CfgFileName)//读取配置文件
        {
            Boolean Flag = false;
            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_Str = Temp.Split(new String[] {"="},StringSplitOptions.RemoveEmptyEntries);
                    int n = 0;
                    foreach (String i in Cfg_Str)
                    {
                        if (Array_Str[0] == i)
                        {
                            Cfg_Values[n] = Array_Str[1];
                            break;
                        }
                        n++;
                    }
                }
                Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                String FileName = DateTime.Now.ToString("yyyyMMddHHmm" + @".txt");
                WErrlog(FileName,ex.ToString());
            }
            return Flag;
        }
        public static Boolean Read_SN_SSD_Section_Length(String Stored_Procedure,String TypeName)//获取扫描条码长度和区间
        {
            Boolean Flag = false;
            try
            {
                WebReference.WebService1 ss = new WebReference.WebService1();
                Section_Length = ss.Read_XY_BN_BardCode_Section_Length(Stored_Procedure,TypeName);
                if (Section_Length[0] == null || Section_Length[1] == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Get Data Base SN And SSD Bar Code Section or Length Is null!!");
                    Console.ResetColor();
                    Flag = false;
                }
                else
                    Flag = true;
            }
            catch (Exception ex)
            {
                Flag = false;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                String FileName = DateTime.Now.ToString("yyyyMMddHHmm" + @".txt");
                WErrlog(FileName, ex.ToString());
            }
            return Flag;
        }
        public static Boolean Insert_Update_SN_SSD_TieYards_Data(String SN_BarCode, String SSD_BarCode, String Stored_Procedure)//SN+SSD绑码数据
        {
            Boolean Flag = false;
            try
            {
                int State = 0;
                WebReference.WebService1 ss = new WebReference.WebService1();
                State = ss.Insert_Update_YX_BN_MainBoard_TieYards_Test_Data(SN_BarCode,SSD_BarCode,Stored_Procedure);
                if (State == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Insert Or Update SN_BarCode And SSD_BarCode TieYards Succeed!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else if (State == 1 || State == 2)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Insert Or Update SN_BarCode And SSD_BarCode TieYards Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
                else if (State == 3)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Failed To Connect To DataBas!!");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Flag = false;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                String FileName = DateTime.Now.ToString("yyyyMMddHHmm" + @".txt");
                WErrlog(FileName, ex.ToString());
            }
            return Flag;
        }
        public static Boolean Insert_Update_TestData(String SN, String LineBody, String JobNumber, String Product_Name,
            String CPUID, String MEMORY, String Address_ID1, String Address_ID2, String BIOS,String Stored_Procedure)//插入测试数据
        {
            Boolean Flag = false;
            try
            {
                int State = 0;
                WebReference.WebService1 ss = new WebReference.WebService1();
                State = ss.Insert_Update_YX_BN_MainBoard_Test_Data(SN,LineBody,JobNumber,Product_Name,CPUID,MEMORY,Address_ID1,Address_ID2,BIOS,Stored_Procedure);
                if(State == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Insert Or Update NetBook Test Data Succeed!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else if(State == 1 || State == 2)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Insert Or Update NetBook Test Data Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
                else if (State == 3)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Failed To Connect To DataBas!!");
                    Console.ResetColor();
                    Flag=false;
                }

            }
            catch (Exception ex)
            {
                Flag = false;
                String FileName = DateTime.Now.ToString("yyyyMMddHHmm" + @".txt");
                WErrlog(FileName,ex.ToString());
            }
            return Flag;
        }

        public static Boolean WErrlog(String FileName,String Str)//生成LOG记录
        {
            Boolean Flag = false;
            try
            {
                FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(Str);
                sw.Close();
                Flag = true;
                return Flag;
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                Flag = false;
                return Flag;
            }
        }
    }
}
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Web;
using System.Threading;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Management;
using System.Net.NetworkInformation;

namespace YX_UploadText_Data
{
    public class DataBase_NB : Program
    {
        public String server;
        public String database;
        public String uid;
        public String pwd;
        String Conn_Str;
        public String BarCode_Section;
        public String BarCode_Length;

        public DataBase_NB(String server, String database, String uid, String pwd)
        {
            this.server = server;
            this.database = database;
            this.uid = uid;
            this.pwd = pwd;
            Conn_Str = "server=" + this.server + ";database=" + this.database + ";uid=" + this.uid + ";pwd=" + this.pwd;
        }

        public Boolean Read_SN_SSD_BarCode_Section_Length(String TypeTheName, String Stored_Procedure)//读取SN和SSD区间
        {
            Boolean Flag = false;
            SqlConnection sqlcon = new SqlConnection(this.Conn_Str);
            sqlcon.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcon);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@TypeTheName", TypeTheName);
                cmd.Parameters.Add("@BardCode", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@BardCode_Length", SqlDbType.VarChar, 5).Direction = ParameterDirection.Output;
                cmd.Parameters.Add("@rs", SqlDbType.Int).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                this.BarCode_Section = cmd.Parameters["@BardCode"].Value.ToString();
                this.BarCode_Section = cmd.Parameters["@BardCode_Length"].Value.ToString();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Read SN And SSD Bar Code TieYards Information Succeed!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Read SN And SSD Bar Code TieYards Information Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Failed To Connect To Server " + this.server + " Database" + this.database);
                Console.ResetColor();
                Flag = false;
            }
            finally
            {
                sqlcon.Close();
            }
            return Flag;
        }
        public Boolean SN_SSD_BarCode_TieYards_Data(String MB_SN, String SD_SN, String Stored_Procedure)//插入或更新绑码数据
        {
            Boolean Flag = false;
            //String Conn_Str = "server="+this.server+";database="+this.database+";uid="+this.uid+";pwd="+this.pwd;
            SqlConnection sqlcon = new SqlConnection(this.Conn_Str);
            sqlcon.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcon);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@MB_SN", MB_SN);
                cmd.Parameters.Add("@SD_SN", SD_SN);
                cmd.Parameters.Add("@rs",1);
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;
                cmd.ExecuteScalar();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Inser And Update SN And SSD Bar Code TieYards Data Succeed!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Inser And Update SN And SSD Bar Code TieYards Data Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Failed To Connect To Server " + this.server + " Database" + this.database);
                Console.ResetColor();
                Flag = false;
            }
            finally
            {
                sqlcon.Close();
            }
            return Flag;
        }
        public Boolean Insert_Upadte_TestDataInDataBase(String SN, String LineBody, String JobNumber, String Product_Name, String CPUID,
            String MEMORY, String Address_ID1, String Address_ID2, String BIOS, String Stored_Procedure)//更新数据至NB_SERVER Mainboard数据表
        {
            Boolean Flag = false;
            //String Conn_Str = "server=" + this.server + ";database=" + this.database + ";uid=" + this.uid + ";pwd=" + this.pwd;
            SqlConnection sqlcnn = new SqlConnection(this.Conn_Str);
            sqlcnn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@SN", SN);
                cmd.Parameters.Add("@LineBody", LineBody);
                cmd.Parameters.Add("@JobNumber", JobNumber);
                cmd.Parameters.Add("@Product_Name", Product_Name);
                cmd.Parameters.Add("@CPUID", CPUID);
                cmd.Parameters.Add("@MEMORY", MEMORY);
                cmd.Parameters.Add("@Address_ID1", Address_ID1);
                cmd.Parameters.Add("@Address_ID2", Address_ID2);
                cmd.Parameters.Add("@BIOS", BIOS);
                cmd.Parameters.Add("@rs",1);
                cmd.Parameters["@rs"].Direction = ParameterDirection.Output;
                cmd.ExecuteScalar();
                if ((int)cmd.Parameters["@rs"].Value == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Insert Or Update Test Data Succeed!!");
                    Console.ResetColor();
                    Flag = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Insert Or Update Test Data Failed!!");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.WriteLine("Failed To Connect To Server "+this.server+" Database"+this.database);
                Console.ResetColor();
                Flag = false;
            }
            finally
            {
                sqlcnn.Close();
            }
            return Flag;
        }

    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值