1.NetWorkDataBase.cs Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
using System.Threading;
using System.Web;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Data.SqlClient;
using System.Net.NetworkInformation;
using System.Net;
namespace Test_Data_Querying_UploadingV1._00
{
public class NetWorkDataBase:Program
{
public String server;
public String database;
public String uid;
public String pwd;
public String SqlConn_Str;
public NetWorkDataBase(String server, String database, String uid, String pwd)
{
this.server = server;
this.database = database;
this.uid = uid;
this.pwd = pwd;
this.SqlConn_Str = "server="+this.server+";database="+this.database+";uid="+this.uid+";pwd="+this.pwd;
}
public Boolean Insert_Update_NetWork_Interfaces(String SN, String LineBody, String JobNumber, String Product_Name, String Chip_Informaton,
String Mac1_Address, String Mac2_Address, String Mac3_Address, String Mac4_Address, String Mac5_Address, String Mac6_Address, String Stored_Procedure)//插入或更新网卡测试数据
{
Boolean Flag = false;
SqlConnection sqlcon = new SqlConnection(SqlConn_Str);
sqlcon.Open();
try
{
SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcon);
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("@Chip_Informaton", Chip_Informaton);
cmd.Parameters.Add("@Mac1_Address", Mac1_Address);
cmd.Parameters.Add("@Mac2_Address", Mac2_Address);
cmd.Parameters.Add("@Mac3_Address", Mac3_Address);
cmd.Parameters.Add("@Mac4_Address", Mac4_Address);
cmd.Parameters.Add("@Mac5_Address", Mac5_Address);
cmd.Parameters.Add("@Mac6_Address", Mac6_Address);
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 SN="+SN+" Test Data In Server=" + this.server + ",database=" + this.database + "Succeed!!");
Console.ResetColor();
Flag = true;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Insert Or Update SN="+ SN+" Test Data In Server=" + this.server + ",database=" + this.database + "Failed!!");
Console.ResetColor();
Flag = false;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.WriteLine("Connection Server=" + this.server + ",database=" + this.database + " DataBase Failed!!");
Console.ResetColor();
Flag = false;
}
finally
{
sqlcon.Close();
}
return Flag;
}
public Boolean Querying_SN_Mac_TieYards(String SN, String Mac_Address,String Stored_Procedure)//查询SN和MAC绑码数据
{
Boolean Flag = false;
SqlConnection sqlcon = new SqlConnection(SqlConn_Str);
sqlcon.Open();
try
{
SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SN", SN);
cmd.Parameters.Add("@Mac_Address", Mac_Address);
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("Querying SN="+SN+" Mac_Address="+Mac_Address+" Test Data In Server=" + this.server + ",database=" + this.database + "Succeed!!");
Console.ResetColor();
Flag = true;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Querying SN=" + SN + " Mac_Address=" + Mac_Address + " Test Data In Server=" + this.server + ",database=" + this.database + "Failed!!");
Console.ResetColor();
Flag = false;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.WriteLine("Connection Server=" + this.server + ",database=" + this.database + " DataBase Failed!!");
Console.ResetColor();
Flag = false;
}
finally
{
sqlcon.Close();
}
return Flag;
}
public Boolean Insert_Update_Mac_SN_TieYards(String SN, String Mac_Address, int Items,String Stored_Procedure)//插入或更新MAC+SN绑码数据
{
Boolean Flag = false;
SqlConnection sqlcon = new SqlConnection(SqlConn_Str);
sqlcon.Open();
try
{
SqlCommand cmd = new SqlCommand(Stored_Procedure,sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SN",SN);
cmd.Parameters.Add("@Mac_Address", Mac_Address);
cmd.Parameters.Add("@Items", Items);
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 SN="+SN+" Mac_Address="+Mac_Address+" Test Data In Server=" + this.server + ",database=" + this.database + "Succeed!!");
Console.ResetColor();
Flag = true;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Insert Or Update SN="+SN+" Mac_Address="+Mac_Address+" Test Data In Server=" + this.server + ",database=" + this.database + "Failed!!");
Console.ResetColor();
Flag = false;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.WriteLine("Connection Server="+this.server+",database="+this.database+" DataBase Failed!!");
Console.ResetColor();
Flag = false;
}
finally
{
sqlcon.Close();
}
return Flag;
}
}
}
2.Program.cs Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.Windows;
using System.Collections;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net;
namespace Test_Data_Querying_UploadingV1._00
{
public class Program
{
public static String[] NetWorkCfg_Args = { "SN", "LineBody", "JobNumber", "Product_Name", "Chip_Informaton", "Mac1_Address", "Mac2_Address" ,
"Mac3_Address","Mac4_Address","Mac5_Address","Mac6_Address"};
public static String[] NetWorkDataFileName;
public static List<String> NetWorkValues=new List<String>();
static int Main(string[] args)
{
if (args.Length == 2)
{
try
{
if (GetCfgDataFileName(Convert.ToInt32(args[1]), "NetWorkInterfacse.config"))
{
if (ReadDataFileContent(Convert.ToInt32(args[1])))
{
String Isn = NetWorkValues[0];
if (args[0] == @"/Q")
{
Title();
for(int i=5;i<5+Convert.ToInt32(args[1]);i++)
{
NetWorkDataBase MyNwd = new NetWorkDataBase(@"SERVER2\SERVER2", "E_NETWORK_Card", "sa", "adminsystem");
if (!MyNwd.Querying_SN_Mac_TieYards(Isn, NetWorkValues[i], "usp_Querying_SN_Mac"))
return 1;
}
return 0;
}
else if (args[0] == @"/ISM")
{
Title();
for (int i = 5; i < 5 + Convert.ToInt32(args[1]); i++)
{
NetWorkDataBase MyInUpNwd = new NetWorkDataBase(@"SERVER2\SERVER2", "E_NETWORK_Card", "sa", "adminsystem");
if (!MyInUpNwd.Insert_Update_Mac_SN_TieYards(Isn, NetWorkValues[i], i - 4, "usp_Insert_Update_Mac_SN"))
return 1;
}
return 0;
}
else if (args[0] == @"/IUNT")
{
Title();
NetWorkDataBase MyInUpTsDt = new NetWorkDataBase(@"SERVER2\SERVER2", "E_NETWORK_Card", "sa", "adminsystem");
if (MyInUpTsDt.Insert_Update_NetWork_Interfaces(NetWorkValues[0], NetWorkValues[1], NetWorkValues[2], NetWorkValues[3], NetWorkValues[4],
NetWorkValues[5], NetWorkValues[6], NetWorkValues[7], NetWorkValues[8], NetWorkValues[9], NetWorkValues[10], "usp_Insert_Update_NetWorkCard_TestData"))
return 0;
else
return 1;
}
else
{
help();
return 1;
}
}
return 1;
}
else
return 1;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.ResetColor();
return 1;
}
}
help();
return 1;
}
public static Boolean ReadDataFileContent(int num)//读取数据文件的内容
{
Boolean Flag = true;
foreach (String ff in NetWorkDataFileName)
{
FileStream fs = new FileStream(ff,FileMode.Open,FileAccess.Read);
try
{
StreamReader sr = new StreamReader(fs, Encoding.Default);
String Temp = String.Empty;
Temp = sr.ReadLine();
if (Temp != null && Temp != "")
NetWorkValues.Add(Temp);
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Read Data " + ff + " File Info Is Empty!!");
Console.ResetColor();
Flag = false;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.WriteLine("Read Data " + ff + " File Info Err!!");
Console.ResetColor();
Flag = false;
}
finally
{
fs.Close();
}
}
if (num < 6)
{
for (int x = 0; x < 6 - num; x++)
NetWorkValues.Add("NULL");
}
return Flag;
}
public static Boolean GetCfgDataFileName(int n,String FileName)//读取配置文件中数据文档路径
{
Boolean Flag = false;
int totalline = 5;
totalline += n;
NetWorkDataFileName = new String[totalline];
FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
try
{
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);
for (int i = 0; i < totalline; i++)
{
if (Array_Str[0] == NetWorkCfg_Args[i])
NetWorkDataFileName[i] = Array_Str[1];
}
}
Flag = true;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.ToString());
Console.WriteLine("Read Config "+FileName+" File Info Err!!");
Console.ResetColor();
Flag = false;
}
finally
{
fs.Close();
}
return Flag;
}
public static void Title()//标头信息
{
Console.Clear();//清屏
Console.WriteLine("Program Version V1.00 Create Date:2019.04.11");
Console.WriteLine("Property in Copyright Ferex ShenBo.");
Console.WriteLine("--------------------------------------------------------------------");
}
public static void help()//帮助信息
{
Title();
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(@"/Q Querying DataBase Sn And Mac TieYards Data");
Console.WriteLine(@"/ISM Insert Sn And Mac TieYards Data");
Console.WriteLine(@"/IUNT Insert Or Update NetWork Card Interfaces");
Console.WriteLine(@"Example_1:*.exe /Q 4 NetWorkConfig.config");
Console.WriteLine(@"Example_2:*.exe /ISN 4 NetWorkConfig.config");
Console.WriteLine(@"Example_3:*.exe /IUNT 4 NetWorkConfig.config");
Console.ResetColor();
}
}
}