The following is the format of the data text file.
"#" Is a comment
"T:" Is join
"D:" is to delete the data in the table.
"C:" is to check data
Data and data are separated by tabs (data can be copied directly from the Enterprise Manager as a data source)
The following table lists the data to be added:
# Insert a user table
# Null: insert null
T: Users
John <TAB> John chan
Cake <TAB> Cake. lu
Jeff <TAB> jeff. hu
# CheckTables Data
# If null is used, check whether it is null. If skip is dropped, check whether it is null. If notnull is used, check whether it is null.
C: Users
John <TAB> John chan
Cake <TAB> Cake. lu
Jeff <TAB> jeff. hu
# Delete All Data
D: TAbleName
Call code:
Parameter 1: Path of the text file
Parameter 2: It is two constants in Helper (in fact, it can be an Enum, but I did not pay attention to it when writing)
Helper. ExecuteAction ("Data.txt", Helper. INSERT );
Parameter 3: Specifies the operation on that object (T: D: C: The name after T: Users, for example, Users)
Helper. ExecuteACtion ("Data.txt", Helper. Insert, "Users ")
The Code is as follows:
Using System;
Using System. Data;
Using System. IO;
Using System. Data. SqlClient;
Using Microsoft. ApplicationBlocks. Data;
Using NUnit. Framework;
Namespace TestCore
{
Public class Helper
{
Public const string DELETE = "D ";
Public const string INSERT = "T ";
Public const string CHECK = "C ";
Public const string ANNOTATE = "#";
Public static string ConnectionString
{
// Enter the returned connection string.
Get {return null ;}
}
Public static void ExecuteAction (string path, string action)
{
ExecuteAction (path, action, null );
}
Public static void ExecuteAction (string path, string action, string inputTable)
{
Console. WriteLine ("----------------" + action + "action is Start --------------------");
StreamReader reader = new StreamReader (path );
String line;
String table = null;
Try
{
While (true)
{
Line = reader. ReadLine ();
If (line = null)
Break;
If (line. Length = 0)
Continue;
String Key = line. Substring (0, 1 );
If (Key = action)
{
Table = line. Substring (2 );
If (action = DELETE)
{
If (CheckTable (inputTable, table ))
{
Delete (table );
}
Table = null;
}
Else
{
Continue;
}
}
Else if (table! = Null)
{
Switch (action)
{
Case INSERT:
If (CheckTable (inputTable, table ))
{
String [] datas = line. Split ();
AddData (datas, table );
}
Break;
Case CHECK:
If (CheckTable (inputTable, table ))
{
String [] checkDatas = line. Split ();
Check (checkDatas, table );
}
Break;
}
Table = null;
}
Else
{
Continue;
}
}
}
Finally
{
Console. WriteLine ("----------------" + action + "action is End --------------------");
Reader. Close ();
}
}
Private static bool CheckTable (string inputTable, string SettingTable)
{
If (inputTable = null)
Return true;
Return (inputTable = SettingTable );
}
Private static void Delete (string table)
{
String SQL = "delete from" + table;
Console. WriteLine (SQL );
Try
{
SqlHelper. ExecuteNonQuery (ConnectionString, CommandType. Text, SQL );
}
Catch (Exception e)
{
Throw new ApplicationException ("deletion error: SQL is" + SQL, e );
}
}
Private static void AddData (string [] data, string table)
{
SqlConnection conn = new SqlConnection (ConnectionString );
String SQL = "insert into" + table + "values ";
String temp = "(" + data [0] + "";
For (int I = 1; I <data. Length; I ++)
{
If (data [I]! = "Null ")
Temp + = "," + data [I] + "";
Else
Temp + = ", null ";
}
SQL + = temp + ")";
Conn. Open ();
Try
{
Console. WriteLine (SQL );
SqlCommand comm = new SqlCommand (SQL, conn );
Comm. ExecuteNonQuery ();
}
Catch (Exception e)
{
Throw new ApplicationException (table + ":" + SQL, e );
}
Finally
{
Conn. Close ();
}
}
Private static void Check (string [] data, string table)
{
String SQL = "select * from" + table;
Console. WriteLine (SQL );
DataSet ds = SqlHelper. ExecuteDataset (Helper. ConnectionString, CommandType. Text, SQL );
DataTable dt = ds. Tables [0];
If (dt. Rows. Count = 0)
Throw new ArgumentOutOfRangeException (SQL + "the returned structure is empty ");
Foreach (DataRow dr in dt. Rows)
{
For (int I = 0; I <data. Length; I ++)
{
If (data [I]. ToLower () = "skip ")
Contin