1.C# Source code:
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.Diagnostics;
using System.Data;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Timers;
namespace CompleteMachine_DataBaseInsert
{
public class Program
{
public static String[] KeyValue = {"NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL"};
public static String[] Smple = { "Client_SN", "Plant_SN", "LineBody", "JobNumber", "Product_Name", "Mac_Address", "Bios_Info", "System_Info", "CPU_Chip_Info"
,"Memory_Size_Info","EMMC_Info","Date_Time","SERVER","DATABASE", "UID", "PWD", "StoredProcedure"};
static int Main(string[] args)
{
CfgProcess MyCfg = new CfgProcess("config.ini");//读取配置文件
if (MyCfg.TestDataTextReaderFull() == false) return 1;//执行配置信息填充
TestDataInsertSqlDataBase MyTestDataInsert = new TestDataInsertSqlDataBase(KeyValue[0],KeyValue[1],KeyValue[2],KeyValue[3],KeyValue[4],KeyValue[5],
KeyValue[6],KeyValue[7],KeyValue[8],KeyValue[9],KeyValue[10],KeyValue[11],KeyValue[12],KeyValue[13],KeyValue[14],KeyValue[15],KeyValue[16]);
if (MyTestDataInsert.InsertTestDataInputSqlDataBase()==false) return 1;
return 0;
}
}
public class CfgProcess : Program{//配置处理
public CfgProcess(String FileName)
{
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)
{
String[] Array = Temp.Split(new String [] {"="},StringSplitOptions.RemoveEmptyEntries);
int n = 0;
foreach (string str in Smple)
{
if (str == Array[0].Trim())
{
KeyValue[n] = Array[1].Trim();
break;
}
n++;
}
}
sr.Close();
fs.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(FileName + " Config File Read Or Dispose Err!!");
Console.WriteLine(ex.ToString());
Console.ForegroundColor = ConsoleColor.White;
Environment.Exit(1);//返回1退出
}
String NowDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
KeyValue[11] = NowDateTime;//写入当前时间字符串
}
public bool TestDataTextReaderFull()//读取文本并填充入字符串
{
bool Flag = false;
for (int i = 0; i <= 10; i++)
{
TextProcess Tx = new TextProcess(KeyValue[i]);
int Len = Tx.GetTextLen();
if (Len > 2)
KeyValue[i] = Tx.GetTextStr();
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(KeyValue[i]+" Test Data File Read Inforomation Is Null!");
Console.ForegroundColor = ConsoleColor.White;
Flag = false;
return Flag;
}
}
Flag = true;
return Flag;
}
}
public class TestDataInsertSqlDataBase:Program{//测试数据插入数据库
private String Client_SN;
private String Plant_SN;
private String LineBody;
private String JobNumber;
private String Product_Name;
private String Mac_Address;
private String Bios_Info;
private String System_Info;
private String CPU_Chip_Info;
private String Memory_Size_Info;
private String EMMC_Info;
private String Date_Time;
private String SERVER;
private String DATABASE;
private String UID;
private String PWD;
private String StoredProcedure;
public TestDataInsertSqlDataBase(String Client_SN, String Plant_SN, String LineBody, String JobNumber, String Product_Name, String Mac_Address, String Bios_Info,
String System_Info, String CPU_Chip_Info, String Memory_Size_Info, String EMMC_Info, String Date_Time, String SERVER, String DATABASE, String UID,
String PWD, String StoredProcedure)
{
this.Client_SN = Client_SN;
this.Plant_SN = Plant_SN;
this.LineBody = LineBody;
this.JobNumber = JobNumber;
this.Product_Name = Product_Name;
this.Mac_Address = Mac_Address;
this.Bios_Info = Bios_Info;
this.System_Info = System_Info;
this.CPU_Chip_Info = CPU_Chip_Info;
this.Memory_Size_Info = Memory_Size_Info;
this.EMMC_Info = EMMC_Info;
this.Date_Time = Date_Time;
this.SERVER = SERVER;
this.DATABASE = DATABASE;
this.UID = UID;
this.PWD = PWD;
this.StoredProcedure = StoredProcedure;
}
public bool InsertTestDataInputSqlDataBase()//数据插入数据库与处理
{
bool Flag = false;
try
{
SqlConnection conn = new SqlConnection("server="+this.SERVER+";database="+this.DATABASE+";uid="+this.UID+";pwd="+this.PWD);
conn.Open();
if (conn.State != ConnectionState.Open)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Sql DataBase Connection Fail!");
Console.ForegroundColor = ConsoleColor.White;
Flag = false;
return Flag;
}
SqlCommand cmd = new SqlCommand(this.StoredProcedure, conn);//设置命令行启动
cmd.CommandType = CommandType.StoredProcedure;//启动数据库存储过程
cmd.Parameters.Add("@Client_SN",this.Client_SN);//设置参数1
cmd.Parameters.Add("@Plant_SN",this.Plant_SN);//设置参数2
cmd.Parameters.Add("@LineBody",this.LineBody);//设置参数3
cmd.Parameters.Add("@JobNumber",this.JobNumber);//设置参数4
cmd.Parameters.Add("@Product_Name",this.Product_Name);//设置参数5
cmd.Parameters.Add("@Mac_Address",this.Mac_Address);//设置参数6
cmd.Parameters.Add("@Bios_Info",this.Bios_Info);//设置参数7
cmd.Parameters.Add("@System_Info",this.System_Info);//设置参数8
cmd.Parameters.Add("@CPU_Chip_Info",this.CPU_Chip_Info);//设置参数9
cmd.Parameters.Add("@Memory_Size_Info",this.Memory_Size_Info);//设置参数10
cmd.Parameters.Add("@EMMC_Info",this.EMMC_Info);//设置参数11
cmd.Parameters.Add("@Date_Time",this.Date_Time);//设置参数12
cmd.Parameters.Add("@rs",1);//设置参数13
cmd.Parameters["@rs"].Direction = ParameterDirection.Output;//启动输出返回
cmd.ExecuteScalar();
if ((int)cmd.Parameters["@rs"].Value == 0)
{
conn.Close();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Insert Test DataBase Successfully!");
Console.ForegroundColor = ConsoleColor.White;
Flag = true;
}
else
{
conn.Close();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed Insert test Data Into DataBase!");
Console.ForegroundColor = ConsoleColor.White;
Flag = false;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Sql DataBase Data Connection Or Process Fail");
Console.WriteLine(ex.ToString());
Console.ForegroundColor = ConsoleColor.White;
Flag = false;
}
return Flag;
}
}
public class TextProcess{//文本处理
private String TextStr;
private int TextLen;
public TextProcess(String FileName)
{
try
{
FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
this.TextStr = sr.ReadLine();
this.TextLen = (this.TextStr).Length;
sr.Close();
fs.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Text Read Or Open Error!!");
Console.WriteLine(ex.ToString());
Environment.Exit(1);//错误退出并返回1
}
}
public String GetTextStr()
{
return this.TextStr;
}
public void SetTextStr(String TextStr)
{
this.TextStr = TextStr;
}
public int GetTextLen()
{
return this.TextLen;
}
public void SetTextLen(int TextLen)
{
this.TextLen = TextLen;
}
}
}
2.Sql server Create Base source code:
USE MASTER
GO
IF EXISTS(SELECT * FROM sysdatabases WHERE name="E_CompleteMachine")
DROP DATABASE E_CompleteMachine
GO
CREATE DATABASE E_CompleteMachine
ON PRIMARY
(
NAME='E_CompleteMachine_data',
FILENAME='D:\TESTDATA\E_CompleteMachine_data.mdf',
SIZE=5MB,
MAXSIZE=500000MB
)
LOG ON
(
NAME='E_CompleteMachine_log',
FILENAME='D:\TESTDATA\E_CompleteMachine_log.ldf',
SIZE=5MB,
FILEGROWTH=0
)
USE E_CompleteMachine
GO
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='CompleteMachineData')
DROP TABLE CompleteMachineData
CREATE TABLE CompleteMachineData
(
NO INT IDENTITY(1,1) NOT NULL,
Client_SN VARCHAR(50) NOT NULL,
Plant_SN VARCHAR(50) NOT NULL,
LineBody VARCHAR(12) NOT NULL,
JobNumber VARCHAR(8) NOT NULL,
Product_Name VARCHAR(50) NOT NULL,
Mac_Address VARCHAR(13) NOT NULL,
Bios_Info VARCHAR(50) NOT NULL,
System_Info VARCHAR(100) NOT NULL,
CPU_Chip_Info VARCHAR(50) NOT NULL,
Memory_Size_Info VARCHAR(50) NOT NULL,
EMMC_Info VARCHAR(50) NOT NULL,
Date_Time VARCHAR(50) NOT NULL
)
GO
USE E_CompleteMachine
GO
ALTER TABLE CompleteMachineData
ADD CONSTRAINT PK_Client_SN PRIMARY KEY(Client_SN),
CONSTRAINT UK_Client_SN UNIQUE(Client_SN),
CONSTRAINT UK_Plant_SN UNIQUE(Plant_SN),
CONSTRAINT UK_Mac_Address UNIQUE(Mac_Address)
GO
3. SQL SERVER 创建存储过程 Source code:
USE E_CompleteMachine
GO
IF EXISTS(SELECT * FROM SYS.OBJECTS WHERE name='usp_Insert_CompleteMachine')
DROP PROC usp_Insert_CompleteMachine
GO
CREATE PROC usp_Insert_CompleteMachine
@Client_SN VARCHAR(50),
@Plant_SN VARCHAR(50),
@LineBody VARCHAR(12),
@JobNumber VARCHAR(8),
@Product_Name VARCHAR(50),
@Mac_Address VARCHAR(13),
@Bios_Info VARCHAR(50),
@System_Info VARCHAR(100),
@CPU_Chip_Info VARCHAR(50),
@Memory_Size_Info VARCHAR(50),
@EMMC_Info VARCHAR(50),
@Date_Time VARCHAR(50),
@rs int output
AS
INSERT INTO CompleteMachineData(Client_SN,Plant_SN,LineBody,JobNumber,Product_Name,Mac_Address,Bios_Info,System_Info,CPU_Chip_Info,Memory_Size_Info,
EMMC_Info,Date_Time)VALUES(@Client_SN,@Plant_SN,@LineBody,@JobNumber,@Product_Name,@Mac_Address,@Bios_Info,@System_Info,@CPU_Chip_Info,@Memory_Size_Info,@EMMC_Info,@Date_Time)
IF @@ERROR>0
BEGIN
SET @rs=1
END
ELSE
BEGIN
SET @rs=0
END
RETURN @rs
GO