Wednesday, April 3, 2013

Importing CSV file into SQLServer and create table dynamically

public void Main()
{

try{
// TODO: Add your code hereSqlConnection myADONETConnection = new SqlConnection();
string sDatabase = Dts.Variables["mDatabase"].Value.ToString();
// sDatabase = "MCUK";//Dts.Connections["Test1"].ConnectionString = "Data source=172.29.17.56;initial catalog=" + sDatabase + ";uid=sa;password=password1";Dts.Connections["Con"].ConnectionString = "Data source=86.54.116.59;initial catalog=" + sDatabase + ";Integrated Security=SSPI;";myADONETConnection = (
SqlConnection)(Dts.Connections["Con"].AcquireConnection(Dts.Transaction) as SqlConnection);
//MessageBox.Show(myADONETConnection.ConnectionString, "Test1");string line1 = "";//Reading file names one by onestring filenameonly1 = "TempCSVParticipation";
//string CompleteDirectory = Dts.Variables["mFilePath"].Value.ToString();//string ActualFileName = Path.GetFileName(CompleteDirectory);string SourceDirectory = Dts.Variables["mFilePath"].Value.ToString();
//SourceDirectory = @"S:\Production\MCUK\Extracts\201302\Participation";TrialBalanceLog("database-"+sDatabase, myADONETConnection);TrialBalanceLog(
"directory-" + SourceDirectory, myADONETConnection);
//MessageBox.Show(sDatabase);//MessageBox.Show(ActualFileName);//MessageBox.Show(SourceDirectory);string[] fileEntries = Directory.GetFiles(SourceDirectory);TrialBalanceLog(SourceDirectory, myADONETConnection);

foreach (string fileName in fileEntries){

//MessageBox.Show("f-" + fileName.ToString());TrialBalanceLog(fileName, myADONETConnection);

//if (fileName.ToString().ToUpper() == (SourceDirectory +'\\'+ ActualFileName).ToUpper())if (fileName.ToString().Contains("Actual_Participation")){
TrialBalanceLog(
"1-" + fileName, myADONETConnection);
//MessageBox.Show("1" + fileName);System.IO.StreamReader file2 = new System.IO.StreamReader(fileName);
//MessageBox.Show("2");string filenameonly = (((fileName.Replace(SourceDirectory, "")).Replace(".csv", "")).Replace("\\", ""));
//MessageBox.Show("3");line1 = (" IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]." + filenameonly1 + "') AND type in (N'U')) DROP TABLE [dbo]." + filenameonly1 + " Create Table dbo." + filenameonly1 + "([" + file2.ReadLine().Replace(",", "] NVARCHAR(4000), [") + "] NVARCHAR(4000))").Replace(".txt", ""); file2.Close();
//MessageBox.Show("4");SqlCommand myCommand = new SqlCommand(line1, myADONETConnection);
//MessageBox.Show(line1);myCommand.ExecuteNonQuery();
//MessageBox.Show("6");line1 = "BULK INSERT " + filenameonly1 + " FROM '" + fileName + "' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\\n' ) ";
//MessageBox.Show(line1);SqlCommand myCommand1 = new SqlCommand(line1, myADONETConnection);
//MessageBox.Show("8");myCommand1.CommandTimeout = 0;
//MessageBox.Show("9");myCommand1.ExecuteNonQuery();
//MessageBox.Show("10");//MessageBox.Show(fileName.ToString() + " Completed");}
}
Dts.TaskResult = (
int)ScriptResults.Success;}

catch (Exception e){

//MessageBox.Show("Error-" + e.Message);}
}

No comments:

Post a Comment