Duplication when insert in teradata

General
N/A

Duplication when insert in teradata

Hello Experts,

I'm currently trying to create a program which is trying to get a directory i stored in a table in teradata then use the directory as a path for a unzipper of file, after i unzip the file i will save the directory name plus the zipfile name in a table again in teradata.

The problem is when i'm storing the directory plus the zipfile name it says duplicate in table... when i checked the table the directory was the only thing stored. the zipfile name connected is not stored.

Please try my code and please help me how to solve the duplication problem.

thanks you very much

-N

P.S
I tried using 2 different connections so as to try if there are changes with using one and seems no change..

CODE:
TdCommand cmd = new TdCommand();
TdDataAdapter tda = new TdDataAdapter();
TdDataReader read;
DataTable dt1 = new DataTable();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;

string dir = "";
string sqlpath = "select pathMcash from directory";//stored path in tdatabase

try
{

cmd.CommandText = sqlpath;
dt1.Load(cmd.ExecuteReader());

}
catch (TdException td)
{
MessageBox.Show("" + td);
}
//got path and save in dir as a string
dir = dt1.Rows[0][0].ToString();

string zxc = dir;
string[] fileNames = Directory.GetFiles(zxc); //getfiles
int rows = fileNames.Length;
int columns = 2;
string[,] fZipStatus = new string[rows, columns];
string status = "";
MessageBox.Show("Start unzipping");
//unzip
con.Dispose();
con.Close();

TdConnection con1 = new TdConnection();
TdConnectionStringBuilder conCsb1 = new TdConnectionStringBuilder();
TdCommand cmd1 = new TdCommand();
TdDataAdapter tda1 = new TdDataAdapter();
TdDataReader read1;

cmd1.Connection = con1;
cmd1.CommandType = CommandType.Text;

for (int x = 0; x < fileNames.Length; x++)
{
MessageBox.Show("Unzipping File : " + x);
int y = 0;
try
{
using (ZipFile zip = ZipFile.Read(fileNames[x]))
{

foreach (ZipEntry z in zip)
{
Directory.CreateDirectory("C:/unzipped");
string unzipdir = "C:/unzipped";
z.Extract(unzipdir, ExtractExistingFileAction.OverwriteSilently);
fZipStatus[x, y] = fileNames[x].ToString();

status = "extracted";
fZipStatus[x, y + 1] = status;
MessageBox.Show(fileNames[x].ToString() + " " + fileNames.Length.ToString());
}
}
}
catch (BadReadException f)
{
if (f.Message.Contains("ReadHeader"))
{
MessageBox.Show("File " + fileNames[x].ToString() + "shows an invalid extention");
}
else if (f.Message.Contains("Could not read"))
{
MessageBox.Show("File "+fileNames[x].ToString() +" is either Corrupted of no Data");
fZipStatus[x, y] = fileNames[x].ToString();
status = "unAbleToExtract";
fZipStatus[x, y + 1] = status;
}
else if (f.Message.Contains("Duplicate"))
{
MessageBox.Show("Duplicate");
fZipStatus[x, y] = fileNames[x].ToString();
status = "duplicate";
fZipStatus[x, y + 1] = status;
}
}

try
{
conCsb1.DataSource = "localhost";
conCsb1.Database = "TDUSER";
conCsb1.UserId = "abe";
conCsb1.Password = "abe";
con1.ConnectionString = conCsb1.ConnectionString;
con1.Open();
}
catch (Exception abs)
{
abs.Message.ToString();
}

//write to tera again

string sql = "INSERT INTO zipTag" +
"(zip_files,tag)" +
"VALUES ('" + fZipStatus[x, y] + "','" + fZipStatus[x, y + 1] + "')";

MessageBox.Show("Write Data in td : " + x);

try
{

cmd1.CommandText = sql;
read1 = cmd1.ExecuteReader();
read1.Close();
}
catch (TdException td)
{
MessageBox.Show("" + td);
}
MessageBox.Show("loaded data : " + x);
}
MessageBox.Show("Done Writting");
1 REPLY
Teradata Employee

Re: Duplication when insert in teradata

Hi,

It is a DB error .... can you share the DDL of the table you are trying to insert data into?

Additionally, a small suggestion .... its easier to understand code if somewhat aligned! :)

Regards,

MAC